Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

#274 String null check + equals(constant) can be simplified

open
nobody
None
5
2013-02-05
2012-08-10
Patrick
No

I often see the idiom:

if(someString != null && someString.equals("some_constant")) {
...
}

This can be rewritten without loss of functionality as:

if("some_constant".equals(someString)) {
...
}

...saving a single check against null since if in the example the variable 'someString' was null, then the latter "some_constant".equals(someString) will always be false just as someString != null will be false in the former. FindBugs-2.0.1 does not currently report any kind of message for the former construct.

-- Patrick Baggett

Discussion

  • Patrick
    Patrick
    2012-08-10

     
    Attachments