Skip to main content

Posts

Showing posts from March, 2014

Redneck conditional anti-pattern

Sometimes I encounter weird looking if statements. By weird, I'm thinking of multiple negations all over the code base. Just a few days ago together with my colleague we found them very exotic during debug of an external library. From this point on, I'll call the following anti-pattern redneck negation or "I ain't not going to do that".



 So let's take a look at a snippet:
if(!some_flag_that_means_off) { //handle negative scenario where we change the flag value } else { //handle positive scenario where we also change the flag value } //decide upon my flag The simpler version would be an extracted method with:
if(some_condition) { //return positive } else { //return negative } Now the code is easier to read and simpler to maintain.

State of mind You might be wondering how someone writes this or how we end up with this type of multiple negation codes. It is easy to judge people, but the reality is that this might have been done at some crazy pres…