Gremlins in your code

Mordechai Danielov
2 min readSep 1, 2020

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:

  1. 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.
  2. 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.
  3. 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.

--

--

Mordechai Danielov

Mordechai Danielov has been working with software for many years. He specializes in databases and moving data around.