println it

Software blog about tools, builds and making it all work

Multi-level ternary operator

Love one. Can’t be easily debugged, probably .. But reads very nicely:

String getScmClass()
    ( this.@scmClass == 'svn' ) ? 'hudson.scm.SubversionSCM'  :
    ( this.@scmClass == 'git' ) ? 'hudson.plugins.git.GitSCM' :

Still, standard mapping is usually better – it allows to dump all known options:

Map    scmClasses = [ svn : 'hudson.scm.SubversionSCM',
                      git : 'hudson.plugins.git.GitSCM' ]
String getScmClass()
  def    scmClass = scmClasses[ this.@scmClass ]
  assert scmClass, "Unknown [${this.@scmClass}]. Known classes are ${scmClasses.keySet()}"


Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>