Create Your Own DSL
Join the DZone community and get the full member experience.
Join For FreeIn software development I encounter problems with creating user interfaces for manage logical chains. More complex chain of logic cause more difficult to develop, test, and debug resulting interface. That interfaces are hard to understand without help of man, who participated in the design.
Usually, it looks like that:
Instead of complex interface is to try to use DSL. This require a little more skill from the end user but in return it will eliminate the overloaded ui. It provide a more flexible control over logic at lower labor costs. A much easier debugging, testing, and auto-testing.
Consider the example of pethouse. We have the following classes:
class Food { Integer size } class Pet { String name Long age Closure hello } @DSLRoot("pet_house") class PetHouse { String name Pet master @DSLTypeHint(Pet) List<Pet> pet @DSLTypeHint(Food) Map<String, Food> food }
To create pethouse we need one screen to PetHouse
and two dialogue to Pet
and Food
. The difficulties begin with the behavior of hello
method for each pet. How many screens needed if we want to pet shouted "Hello!" only when there is enough food for it? And if we want to check whether a particular type of food?
All this logic is easy to describe in the DSL (groovy):
pet_house { name "My pet house" food { cat_food {size 12} } pet { name "Cat" hello { if (food["cat_food"].size > 0) { return "Hello" } else { return "I need food!!!" } } } }
Plain and simple.
There is DSLParser
with specification to help write your own DSL. Also, it requires Reflections
which can be replaced simply.
DSLParser (github) (include spec)
Published at DZone with permission of Alexey Kutuzov. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
What Is JHipster?
-
Building the World's Most Resilient To-Do List Application With Node.js, K8s, and Distributed SQL
-
AI and Cybersecurity Protecting Against Emerging Threats
-
Rule-Based Prompts: How To Streamline Error Handling and Boost Team Efficiency With ChatGPT
Comments