Booleans are the bread and butter of decision making in most modern programming languages and in Kotlin this is no exception. I will discuss with you some ways to clean up your boolean logic and you will explore some best practices in giving good positive names to our binary friends.
Double negations are not not bad.
You should not use double negations in your code because they are hard to read and understand. They also significantly increase the amount of bugs and errors that can hide in your program. I invite you to try the following exercise to better understand why Double negatives form a problem.
Which numbers will be printed to the console?
Most programmers take longer and make more mistakes interpreting the code that uses double negations compared to the code where less negations are used. This is why you should always try to write boolean variables and parameters in their positive form.
Here is the solution. Did you get it right?
Parameters and variables are not the only places where problematic usage of double negations can be found. Methods, that have a boolean as return type, suffer from the same problems.
Will be less clear and should be replaced with:
Try to start positive
If you really need the negative form of a method consider providing the positive part first and let the negative form call the positive form. Most of the time it is easier to implement the positive form anyway. Letting the negative form call the positive form has the added benefit of avoiding duplication, which limits the amount of code that has to be touched when requirements change.
Clean up existing code
Often you have to deal with code that has already been written by you or an other person. As per the Boy Scout Rule we should Leave our code better than we found it.
Let’s say you found this piece of code that deals with boxes containing integers.
Which steps would you take to improve this code?
You can use the Kotlin Playground to test the code in your browser.
Make sure you check if your code still compiles and behaves the same way after each step.
Step 1: Introduce positive form.
Step 2: Replace each double negative with a call to the new function.
Step 3: If negative form is only used in the positive form use the Inline Function Refactor method to move to merge the body of the negative form (isNotEmpty()) to the positive form (isEmpty()).
Step 4: Clean up the positive form a little further
Instead of using a method named isNotEmpty to see whether a box is full it would probably make more sense to introduce a method called isFull. Often you have to search a little longer for names that better convey the right meaning.
There’s no reason to use complex and confusing double negatives in your code. So do us all a favor. Don’t not avoid double negations.