Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java coding standards and multiple IF's

I have the following lines of code:

if(
    checker.this()==false ||
    checker.that()==false ||
    checker.what()==true||
    checker.cool()==false ||
    checker.damm()==true
    (...)
  )
{
    option = Option.FALSE;
}

With about 20 checks that must be performed. I've found this to be the most 'visual-tolerable' form of writing this if with multiple OR sequence but I'm not yet satisfied. Is there a coding standard for this?

Thanks.

like image 500
Frankie Avatar asked Mar 11 '26 03:03

Frankie


1 Answers

The closest thing to a coding standard around this is Steve McConnel, whose authoritative book "Code Complete" recommends that complex conditions are factored into their own method, even if they are only used once. This allows for the name of the method to descibe what is happening.

if (checkValid(checker)) {...}

private boolean checkValid(Checker checker) {...}

checkValid is not a good name, of course, and should be replaced with something more descriptive. In this particular case you may want to make the check method part of "checker" object.

You should also avoid "something==true" and "something==false", and use "something" and "!something". This process is helped if you give the boolean methods appropriate names, like "isOpen()", "isEmpty()", rather than "open()" and "empty()". "checker.isOpen() && !checker.isEmpty()" is perfectly clear to read.

like image 150
DJClayworth Avatar answered Mar 12 '26 17:03

DJClayworth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!