When people first start writing code involving conditionals, they
tend to write a lot of redundant code. Below are some examples of how
to write the same conditional expression in two different ways. The
form on the left includes redundant code. The form on the right is
the preferred form, since it expresses the correct idea more concisely.
Simple Programming Style |
Advanced Programming Idiom |
---|---|
if (condition1 == true) { expression1; } |
First form is redundant.if (condition1) { expression1; } if (condition1) already means "if condition1 is true".
|
if (condition1 == false) { expression1; } |
if (!condition1) { expression1; } First form is redundant. |
if (condition1) { return true; } else { return false; } |
We are just returning the result of the condition.return condition1; |
if (condition1) { expression1; return true; } else { expression1; return false; } |
We are just returning the value of the conditional and doing the same action regardless of its result. Therefore, we should store the result in a local variable.boolean result = condition1; expression1; return result; |
if (condition1) { expression1; } else { // do nothing } |
We are permitted to omit the “do nothing” else clause.if (condition1) { expression1; } |
if (condition1) { // do nothing } else { expression1; } |
if (!condition1) { expression1; } We want to do something if the condition is false. |
if (condition1 && condition2) { expression1; } else if (condition1 && !condition2) { expression1; } |
if (condition1) { expression1; } We are evaluating |
boolean result; result = expression1; |
boolean result = expression1; Applies if the value of the variable is set only once and
at the same level (not in any nested execution blocks like
|
boolean result = expression1; return result; |
return expression1; Applies if the evaluation/declaration of the variable occurs right before the result is returned. |
The examples in the above table are instances of some more general simplifications involving boolean expressions and conditionals .
It is important to indent code properly and consistently in order
for the code to be easy for others to read and understand. Three
acceptable indentation styles are shown below. The form on the left
is preferred since it is more compact and quite readable. (Note also
that the version on the left has greater indentation. This helps eyes
tired from many hours of staring at code.)
expression1; if (condition1) { expression2; if (condition2) { expression3; } else if (condition3) { expression4; } else { expression5; } expression6; } else if (condition4) { expression7; } else { expression8; } expression9; |
expression1; if (condition1) { expression2; if (condition2) { expression3; } else if (condition3) { expression4; } else { expression5; } expression6; } else if (condition4) { expression7; } else { expression8; } expression9; |
expression1; if (condition1) { expression2; if (condition2) { expression3; } else if (condition3) { expression4; } else { expression5; } expression6; } else if (condition4) { expression7; } else { expression8; } expression9; |