Return Boolean Expressions Directly

 class​ Astronaut {
 
  String name;
 int​ missions;
 
 boolean​ isValid() {
»if​ (missions < 0 || name == ​null​ || name.trim().isEmpty()) {
 return​ ​false​;
  } ​else​ {
 return​ ​true​;
  }
  }
 }

Next, let’s look at another way you can cut clutter in your code. In this case, we don’t need the if statement to pursue our goals. Let’s find out why.

You can see a typical validation method in the snippet. The method checks a couple of attributes of an object, an integer and a String.

The integer attribute refers to a number of missions. This number shouldn’t be negative.

The String attribute shouldn’t be null; otherwise we risk that a NullPointerException crashes the execution at some point.

Additionally, it should not be empty, because every Astronaut should have an actual name. The call to name.trim() removes all kinds of preceding or trailing space characters, such as white spaces or tabs. If there’s something left after the call to trim(), it’ll be an actual sequence of characters.

There’s no functional error in the code. The problem, as is so often the case, is that the code is more complex and less readable than it could be. More precisely, the if statement is entirely unnecessary. It’s clutter code that only distracts from the actual semantics.

Let’s see what we can do about it! The key is the return type of our method: boolean. Because of this, we don’t have to wrap everything in an if statement—we can return the value right away, like this:

 class​ Astronaut {
 
  String name;
 int​ missions;
 
 boolean​ isValid() {
»return​ missions >= 0 && name != ​null​ && !name.trim().isEmpty();
  }
 }

This change removes a level of indentation and branching. The method is now very concise and much easier to read!

We condensed the five lines of the if statement into a single line of code. To retain the same semantics, we had to apply Boolean arithmetic to the condition. Essentially, we applied De Morgan’s laws[18] and negated the condition. Here’s a quick definition of these famous laws, which you’ll need often:

 !A && !B == !(A || B) ​// true
 !A || !B == !(A && B) ​// true

There are times when the condition is more complex than this one. If this is the case, you should think about breaking it into smaller chunks. Our advice is to capture parts of a condition with variables that have meaningful names. Consider the following example:

 boolean​ isValid() {
 boolean​ isValidMissions = missions >= 0;
 boolean​ isValidName = name != ​null​ && !name.trim().isEmpty();
 return​ isValidMissions && isValidName;
 }

As a rule of thumb, you should consider such a simplification if you combine more than two different conditions. If you need parts of the condition elsewhere, consider putting them into separate methods, as explained in Simplify Boolean Expressions.

Note that the solution depicted here only works with boolean return types.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.59.227.82