Ok I am using Sonar to check code quality. It’s telling me this simple method is causing two Major warnings.
public static String formatString(String string) {
if(string==null) {
return null;
}
string = string.trim();
string = string.toUpperCase();
return string;
}
Where I can understand the warning due to direct access to the parameter. As you can see this method pretty much does very little. Removes whitespace and puts it in caps. However what is the problem since it in the end returns a string. The creation of a value holder string seems excess to requirements given the overhead in creating a string.
So my question is what I’m doing bad coding and if so why?
I do not think that you are doing anything wrong: although the whole code can be folded to a single line like this
I know people who prefer to see it on multiple lines. The compiler should figure out all necessary optimizations related two writing back to
string“unnecessarily”, so I think you’re good.