I am using Ruby based DSL’s for a number of tasks in my current project. But one day it struck me – am I using the right tool for the job, could there be a better, more simple solution?
Some DSL’s I use are simply declarative and look something like this: quiz “Let’s ask stuff” do question “What is 1+1?” do alternative “2″, :correct => true alternative “3″ end question “Vad is 2+3″ do alternative “1″ alternative “Infinity” alternative “Don’t know” alternative “5″, :correct => true end end
I find that quite readable, but it requires me to maintain code that handle the DSL. So I figured, what if I used YAML? It is simple, and using it would mean less code to maintain.
After doing some experimenting, this is what I came up with: quiz: Let’s ask stuff questions: - question: What is 1+1 alternatives: - text: 2 correct: true – text: 3 - question: What is 2+3 alternatives: - text: 1 - text: “Infinity” - text: “Don’t know” - text: 5 correct: true
In my opinion, the YAML is not as readable as the Ruby DSL. I also find it more error prone as those dashes are sort of tricky.
XML?
With the correct schema, errors can be avoided, but it is just too much text in there, it gets heavy and verbose.
The verdict was to stay with the Ruby DSL’s – their readability and flexibility are well worth the effort of maintaining a separate parser – which is quite short really.