July 2013
Intermediate to advanced
370 pages
8h 27m
English
Fluency is another characteristic of a DSL. It helps make code readable and naturally flowing. It’s not easy to design for fluency, but we should do it for our users. We’ll now discuss some examples of fluency and explore a few ways to write loops in Groovy:
| CreatingDSLs/FluentLoops.groovy | |
| | // Traditional Looping |
| | for(int i = 0; i < 10; i++) { |
| | println(i); |
| | } |
| | // Groovy ways |
| | for(i in 0..9) { println i } |
| | |
| | 0.upto(9) { println it } |
| | |
| | 10.times { println it } |
All the previous loops produce the same result. Groovy provides fluency for looping, among other things. Fluency is not restricted to Groovy, though. EasyMock (which inspired the Groovy mock library) exhibits fluency in setting up the mock expectations in Java: ...
Read now
Unlock full access