Re:A DSL in 5 Languages: Ruby, Python, PHP, C#, Java
by jeswin on Tuesday August 03, 2010 @02:13PM
(#1572092)
Fluent interfaces are cool, but they throw away the semantics of the programming language like meaningful return values.
The DSL in the article is a good example of this problem:
OrderId.StartsWith("a2d")......
So the StartsWith() returns a TransactionSearch, instead of a bool as you would expect. Once you do this often enough, you really get objects full of state-information, rather than methods with return values.
An alternative to doing this is using Expression Trees, but somewhat harder (and not without faults)
It would look like this:
Two benefits come to mind:
1. StartsWith() can return a bool as you would expect.
2. You can use StartsWith() on strings, like OrderId
--- I am not sure how you pulled this off in the DSL example (is OrderId a custom type, since you can't use Extension methods there?)
The biggest drawback of course is:
1. Harder - You will need to parse the Expression Tree.
The DSL in the article is a good example of this problem:
So the StartsWith() returns a TransactionSearch, instead of a bool as you would expect. Once you do this often enough, you really get objects full of state-information, rather than methods with return values.An alternative to doing this is using Expression Trees, but somewhat harder (and not without faults) It would look like this:
Two benefits come to mind: 1. StartsWith() can return a bool as you would expect. 2. You can use StartsWith() on strings, like OrderId --- I am not sure how you pulled this off in the DSL example (is OrderId a custom type, since you can't use Extension methods there?)The biggest drawback of course is: 1. Harder - You will need to parse the Expression Tree.