BriCode Thoughts from Brian Holt

11Feb/103

Java Checkstyle saying “Expression can be simplified”

Okay, seriously, can we be any more vague? That's like erecting a building only to have you client tell you that you should simplify it. I won't repeat the phrases I uttered after I saw that was the error that Checkstyle was giving me. The description given by the Checkstyle website isn't much better either. So what are you really suppose to do then? What does it all mean?

The part of code I got on was:

while (scan.hasNext() && found == false) {

I figured it must mean that Checkstyle doesn't like the && or || operators and so I worked to remedy that only to no avail. Turns out the that Checkstyle isn't a fan of using the == operator in conjunction with boolean variables.

In plain English: don't use
if(blnVar == false)
if(blnVar == true)
but instead use
if(!blnVar)
if(blnVar)

That will clear up the Checkstyle error. Personally, I find the "correct" results to be more confusing, if anything. I think it's a rather arbitrary, but such is the life of a CS student.

Comments (3) Trackbacks (0)
  1. Don’t you read the ! as not? I read it as not found (!found) or found (found). Since its already a boolean, comparing a boolean might actually take more processing time (although the compiler may just simplify it).

    That’s not to say that the error is vague at best.

  2. I am agree with if(blnVar == false) is more readable that if(!blnVar). Modern compiler should optimize a simple sentence like blnVar == false.

    Regards ;)

  3. Maybe it is true that if(blnVar == false) is more readable that if(!blnVar), but I’d say that the problem then is that the variable name is not a good one. Compare with if(found) and if(!found); much more readable than if(found == true) and if(found == false).


Comments are closed.

No trackbacks yet.