Search code examples
grailsgroovyrefactoring

Is it a good practice to use StringUtils in groovy, in that case?


I've just integrated a new devteam, which is using grails. I'm new to grails/groovy too, so the question is possibly stupid, but I need some advice.

The legacy code I'm manipulating is using a lot of StringUtils from apache, and I can't find a good point why they're doing that. I've suggested to use groovy truth instead, and avoid importing an unnecesseary class, but they keep on not correcting existing code, and using it in new code.

So, my question is : are there any advantages I did'nt see, for that use case ?

Example of code :

if (StringUtils.isNotEmpty(params.invoiceEventId)) {
 invoiceEvent = InvoiceEvent.findById(params.invoiceEventId)
}

Thanks for your wisdom


Solution

  • Look at the source code of both solutions:

    StringUtils

        public static boolean isNotEmpty(final CharSequence cs) {
            return !isEmpty(cs);
        }
    
        public static boolean isEmpty(final CharSequence cs) {
            return cs == null || cs.length() == 0;
        }
    

    and StringGroovyMethods:

        public static boolean asBoolean(CharSequence string) {
            return string.length() > 0;
        }
    

    The methods are almost identical. The only difference, is that null-check in Groovy is done outside.

    Ergo, there's no need to use the long call to the 3rd party library in Groovy or Grails.