Gremlins in your code
It happens to the best of us, we all get lazy.
But then something strange starts happening when we want to cover up our laziness, because we don’t want to seem .. er — lazy. :)
Programming languages are logical. All possible code pathways can be calculated ahead of time. So there should be no surprises.
Yet there are.
- QA failed your change
- I see, how did this variable get this value? Makes no sense. Gremlins.
sure it does. We just don’t want to admit it. Or too lazy to think it through.
Even worse, we start to create “just in case” patches.
- I know, I’ll just put this if statement right here — just in case. There. I’ve done it!
Let’s make one thing clear. There are no gremlins. At least not in programming languages. How can we avoid getting unexpected results from our code? Here are few ideas:
- make it explicit — if a code pathways is going to be triggered by a certain condition, make it explicit. Don’t hide it in “else” or in reverse logic statements.
- keep it readable — even if you have huge conditions assign them to local variable and form your condition with those variables so that it’s very clear what is being tested.
- keep it simple — break your code up in several classes or functions if you have to, but keep each unit as simple as possible. No one should get a headache from reading your code.