Join the DZone community and get the full member experience.Join For Free
i recently came across some dsls which had some defects. let's look at a sample i quickly hacked together with xtext :
here are two recommendations i'd like to give when designing a textual dsl:
- don't use uppercase letters in the keywords of your dsl, unless you either really like them. in the sample dsl, the keywords ( element , endelement , child , endchild all start with an uppercase letter, which makes it cumbersome to edit the dsl script (you need to press the shift key a lot). back in the days when we didn't have syntax highlighting, uppercase keywords made a lot of sense as they helped to draw a better distinction between the keywords, the variables and the constants (strings and numbers) of a program. nowadays, it is hard to find an ide which does not offer syntax highlighting, so this argument does not hold any more.
- do not use noisy begin ... end syntax constructions. they do not increase readability of your dsl script and they add to the amount of code to be typed.
here is an improved version of the dsl script:
by removing the endelement and endchild keywords, we have improved readability a lot. using lowercase keywords has lowered the effort you need to invest to key in the dsl script. and - honestly - don't we all like to be lazy at times? after all, one of the goals of dsls is to make your work easier and more efficient.
Opinions expressed by DZone contributors are their own.