Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h2><strong>Boolean variables should be used only for Boolean logic. In all other cases, use enumerations.</strong></h2> <hr> <p>Boolean variables are used to store data that can only take on two possible values. The problems that arise from using them are frequently overlooked:</p> <ul> <li>Programmers often cannot correctly identify when some piece of data should only have two possible values</li> <li>The people who instruct programmers what to do, such as program managers or whomever writes the specs that programmers follow, often cannot correctly identify this either</li> <li>Even when a piece of data is correctly identified as having only two possible states, that guarantee may not hold in the future. </li> </ul> <p>In these cases, using Boolean variables leads to confusing code that can often be prevented by using enumerations. </p> <p><strong>Example</strong></p> <p>Say a programmer is writing software for a car dealership that sells only cars and trucks. The programmer develops a thorough model of the business requirements for his software. Knowing that the only types of vehicles sold are cars and trucks, he correctly identifies that he can use a boolean variable inside a Vehicle class to indicate whether the vehicle is a car or a truck. </p> <pre><code>class Vehicle { bool isTruck; ... } </code></pre> <p>The software is written so when <code>isTruck</code> is true a vehicle is a truck, and when <code>isTruck</code> is false the vehicle is a car. This is a simple check performed many times throughout the code. </p> <p>Everything works without trouble, until one day when the car dealership buys another dealership that sells motorcycles as well. The programmer has to update the software so that it works correctly considering the dealership's business has changed. It now needs to identify whether a vehicle is a car, truck, or motorcycle, three possible states. </p> <p>How should the programmer implement this? <code>isTruck</code> is a boolean variable, so it can hold only two states. He could change it from a boolean to some other type that allows many states, but this would break existing logic and possibly not be backwards compatible. The simplest solution from the programmer's point of view is to add a new variable to represent whether the vehicle is a motorcycle. </p> <pre><code>class Vehicle { bool isTruck; bool isMotorcycle; ... } </code></pre> <p>The code is changed so that when <code>isTruck</code> is true a vehicle is a truck, when <code>isMotorcycle</code> is true a vehicle is a motorcycle, and when they're both false a vehicle is a car. </p> <p><strong>Problems</strong> </p> <p>There are two big problems with this solution:</p> <ul> <li>The programmer wants to express the type of the vehicle, which is one idea, but the solution uses two variables to do so. Someone unfamiliar with the code will have a harder time understanding the semantics of these variables than if the programmer had used just one variable that specifies the type entirely. </li> <li>Solving this motorcycle problem by adding a new boolean doesn't make it any easier for the programmer to deal with such situations that happen in the future. If the dealership starts selling buses, the programmer will have to repeat all these steps over again by adding yet another boolean. </li> </ul> <p>It's not the developer's fault that the business requirements of his software changed, requiring him to revise existing code. But using boolean variables in the first place made his code less flexible and harder to modify to satisfy unknown future requirements (less "future-proof"). When he implemented the changes in the quickest way, the code became harder to read. Using a boolean variable was ultimately a premature optimization. </p> <p><strong>Solution</strong> </p> <p>Using an enumeration in the first place would have prevented these problems. </p> <pre><code>enum EVehicleType { Truck, Car } class Vehicle { EVehicleType type; ... } </code></pre> <p>To accommodate motorcycles in this case, all the programmer has to do is add <code>Motorcycle</code> to <code>EVehicleType</code>, and add new logic to handle the motorcycle cases. No new variables need to be added. Existing logic shouldn't be disrupted. And someone who's unfamiliar with the code can easily understand how the type of the vehicle is stored. </p> <p><strong>Cliff Notes</strong></p> <p>Don't use a type that can only ever store two different states unless you're absolutely certain two states will always be enough. Use an enumeration if there are any possible conditions in which more than two states will be required in the future, <em>even if a boolean would satisfy existing requirements</em>. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload