... It is now the present.

I've started making a conscious effort to reduce if-statements in my code. When I write an if-statement, I at least consider ways of replacing the functionality with things like maps and polymorphism.

I discovered there's a whole community around this concept:

The Anti-If Campaign

There are also lots of posts on stack overflow about polymorphism replacing conditionals. One thing that sometimes comes up is that SmallTalk, the most successful vaporware ever, has no if-statement. Instead, it has an ifTrue method on the Boolean class. Which means you can write code like this:

(x < y) ifTrue: [ 'Yes, x is less than y!' printNl ]

I'm not sure that's any better, actually, but my point is: I'm not the only one thinking about this.

And it is fun to think about, but what about in practice? In practice there's the problem of...

Does my boss care?

You can't impress your boss by writing beautiful code. You can only impress your boss by writing code that does what it's supposed to. Often times you'll trace a bug to a nasty, if-ridden mess. It's your job to fix it. Probably you're being pressured to fix it quickly. Also the code works most of the time.... Maybe the bug only shows up on iPads and not iPhones.... You don't want your fix to accidentally break other devices do you? What's one more if-statement in a sea of if-statements?

if([UIDevice currentDevice].userInterfaceIdiom==UIUserInterfaceIdiomPad)
{
    // My code to fix the bug.
}

So it comes down to either:

  1. Do a large change that reorganizes the code to use less if-statements.
  2. Do a small change that fixes the problem but adds to the mess.

It's a classic choice between slow-but-sustainable and fast-but-crappy. In the midst of it, you choose the second thing, because it seems less risky. And then it eats at you. You fixed the bug, but you didn't fix the code. And you know that the next sad fuck that looks at it won't have a clue what to do.

Well, that next sad fuck might be me, and I don't want you to compromise.

I want you to go to your boss and say "I will NOT fix code by making it worse"

I want you to STOP WRITING IF STATEMENTS

GET UP RIGHT NOW, GO TO YOUR CODE, FACTOR OUT A BUNCH OF IF STATEMENTS, AND MAKE A PULL REQUEST

STOP WRITING IF STATEMENTS

STOP!