send(String from, String to, String subject, String body) {
println "sender ${from}"
println "sender ${to}"
println "sender ${subject}"
println "sender ${body}"
}
typical java call would be
send("john@example.com", "mike@example.com", "greetings", "Hello Goodbye")
Too many strings, don't you think? I have to check java docs all the time I'm writing this method.
Groovy allows us to pass parameters as map without []. If we pass map into method groovy set it to the first parameter. Let's look into other example
send from:"john@example.com",
to:"mike@example.com",
subject:"greetings",
body:"Hello Goodbye"
Much more readable as for me. And method will looks likesend(params) {
println "sender ${params.from}"
println "sender ${params.to}"
println "sender ${params.subject}"
println "sender ${params.body}"
}

Yes, it's a pain in Java, named parameters it's cool feature.
But, there are several solation how to solve this issue:
1)
String from, to, subject, body;
send(from="john@example.com", to="mike@example.com", subject="greetings", body="Hello Goodbye")
2) use map
send(new HashMap {{
put("from", "john@example.com");
put("to", "mike@example.com");
put("subject", "greetings");
put("body", "Hello Goodbye");}}) {
...
}
3) Implement Fluent API
Mail.from("john@example.com").to("mike@example.com").subject("greetings").body("Hello Goodbye").send();
I prefer 2) and 3)
#3 (Builder) is the first choice for me, really love it. I think I will write about groovy builder soon.
But I think we can find way to write in DSL style in all languages, unfortunately most devs still prefer usign tons of params
Maybe I was not really fair when didn't mention builder in original post. Will try to fix this in future
With named parameters you get the advantage of compiler checks.
I doubt there is any simple way to make sure at compile time that the Builder was supplied with all the required params. And by simple way I mean going without tons of builder-specific interfaces.
Love Builder though, when it comes to anything more complex than passing parameters.
Hmm it looks like your website ate my first comment (it was
super long) so I guess I'll just sum it up what I submitted and say, I'm
thoroughly enjoying your blog. I as well am an aspiring
blog blogger but I'm still new to everything. Do you have any points for newbie blog writers? I'd definitely appreciate it.
My blog Full Report