Great Conditional Logic

It is amazing some of the relics that can be found while code reviewing old code. Sometimes developers do things without thinking and make some great code. I am of course referring to some really funny code such as what I found a few minutes ago. I am always annoyed when boolean values are checked explicitly in conditional statements like the following code.

if (IsValid == true)

This bothers me because the true is completely useless. I would read this code as "If is valid is true", and that is kind of silly when I could say "If is valid".

if (IsValid)

This is just part of the annoyance in the code. Now I've cleaned it up a bit here, because it didn't really have a variable named IsValid. It was actually a variable named edit. We were not sure if edit meant that someone was allowed to edit or if it was in edit mode or what.

This was just slightly annoying. The really amazing part was in the else of this if statement. It had a great bit of else if logic. The code looked overall like this.

if (edit == true)
    // Lines of code here
else if (edit == false)
    // Lines of code here

I now wonder what possible other condition is there? In the else is there another state for edit? It is not a reference type, it is a value type so it can never be null. Just got to be careful of boolean operators there are so many other values for them other than true and false.

I hope everyone enjoyed this fun little code snippet.

Comments