We all know that many developers have difficulty in dealing with built-in concepts like dates and times, and that for and switch statements don't necessarily have to be used with each other. However, validating a piece of input is usually more straightforward. You compare what you got to what was expected.

Mathieu was tasked with migrating some Groovy scripts. While the technical migration was fairly easy, he found it necessary to rewrite certain portions of the input validation routines. For example, the task of validating the month portion of a date string seemed straightforward enough...

  def MONTHLIST = "JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC" 
  /* ... */ 
  def MonthOk = MONTHLIST.contains(date.substring(1, 4).toUpperCase())

This works well for months like ANF and POC.

Of course, this was written by the same highly skilled person who decided that you needed scratch variables, a try-catch block and throwing an exception to see if something was null...

def noNullString(str) { 
  def rc = str 
  try { 
      def x = rc.trim(); 
  } 
  catch (NullPointerException e) { 
      rc = "" 
  } 
  return rc; 
}

I guess we should all count our blessings that the developer of that code limited himself to strings; imagine what he might have done with a boolean?!

[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!