Fork me on GitHub

Friday, 13 January 2012

Awesomeness of groovy named parameters

,
How do you call typical method with few parameters. Lets assume we have method that sends email with signature
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 like
send(params) {
  println "sender ${params.from}"
  println "sender ${params.to}"
  println "sender ${params.subject}"
  println "sender ${params.body}"
}

5 comments to “Awesomeness of groovy named parameters”

  • 13 January 2012 19:01
    Orest Ivasiv says:

    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)

    delete
  • 13 January 2012 19:07

    #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

    delete
  • 13 January 2012 19:10

    Maybe I was not really fair when didn't mention builder in original post. Will try to fix this in future

    delete
  • 13 January 2012 21:21

    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.

    delete
  • 22 May 2013 06:11
    Anonymous says:

    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

    delete
 

Grygoriy Mykhalyuno Copyright © 2011 -- Template created by O Pregador -- Powered by Blogger