Thursday 22 October 2009

Util Methods does not Work

Dear Junior
When writing the validation logic for the username
public boolean isValid() {
  return username.matches("[a-z]+");
}
I suddenly heard a distant screaming: “Why did you not use the util method for validation?” Ehhrr … sorry … which method? Ohhh, over there … in the se.xyz.services.util.stringutils package there is a util class StringValidationUtil with a validation method.
public class StringValidationUtil {
  static public boolean logincheck(String username) {
    if (!(username.length() > 0)) return false;
    for(int i=0; i<username.length();i++)
      if(!Character.isLowerCase(username.charAt(i)))
        return false;
    return true;
  }
}
I am sorry, I guess I just didn’t find it.


It is strange that I did not find it, because it is actually called as part of account creation when registering a new user. Did I not look for it properly?


Well, it is also strange that I did not find the method in se.xyz.utils.security.AccountTransformUtils, because there you can find
static public boolean okNewUsername(String username) {
  boolean result = true;
  if (username.length() == 0) result = false;
  for(int i=0; i<username.length();i++)
    result = result && Character.isLowerCase(username.charAt(i));
  return result;
}
That method is by the way also called, as part of the check when someone wants to change username. Did I not look properly for that either?


And of course there are some more methods in se.xyz.accmgm.AccountUtil and in the ever-present se.xyz.util.Util that all basically do the same thing - check that an account name has the proper form.


My real-life record was a util class that contained five different implementations of checking that a string was a date on the format “YYYYmmDD” – and between those implementations, there where subtle differences when handling some strange cases. By the way, there where also three more different implementations in another slightly differently named util class as well.


So, how come this multitude of util methods? They are simply not found! And the programmer in need for validating that there are only lowercase letters in the string at hand will probably look for the needed method for ten to thirty seconds, where after she will implement it herself – after all it is not that difficult. Then, to make “my nice method helpful for everybody else” it is moved to some util class.


As a side-note, you can note that most util methods are ‘static’. To me ‘static’ in an oo-program means “homeless”. Those methods could reside equally well in any other class. And residing in some obscure hide-away package does not make them easy to find.


For a method to be used, it must be in the middle of the road where the programmer is going. That is what object-orientation is good at, the methods are hung up on the data you have in your hands, so the methods are easy to find.
But unfortunately, static util methods do not work that way. They simply do not work.


Yours


Dan