Fluency

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:

 

Get Programming Groovy 2 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.