String is evil
Von Carsten
Over the last days I had to fight with legacy sourcecode again. It is always hard to figure out what other developers meant years ago when the code was written. But it gets even harder when every single parameter is a String. It’s annoying and slows you down, because you always have to look up which format the String should be. Is it maybe an Integer? Or some kind of flag? Or is there more information encoded in this String and do you have to rebuild a strange ASCII format filled with information to call that method?
Truth is, you don’t know. You just want to call a method called addToAcl(String role)
, but what is that parameter? The name of a role? Or the longer display name of the role? Or is it the UID of the role? How is the UID represented?
I have to deal with legacy sourcecode all the time, sometimes it’s years old. Often the developers are not available anymore. And there is a lot of string-passing around, integer return codes and other awful things I try to avoid whenever I write code. It’s not always possible to avoid it myself, but that doesn’t stop me ranting about it here on my blog. 😉 I came upon so many methods were I couldn’t tell what I need to pass by reading the method and parameter names. I had to open the method source, look up what it wanted to do with the parameter and then give the right value to the method.
Stephan wrote a good article about evil Strings over at codemonkeyism, so I won’t give another example here. But if you are tempted to add a integer parameter or a String parameter to any method, ask yourself: Will The Next Guy[tm] know what this parameter means? Or should I use a real object here?