It seems like you are using an ad blocker. To enhance your experience and support our website, please consider:

  1. Signing in to disable ads on our site. Our users don't see ads
  2. Or, Sign up if you don't have an account yet
  3. Or, disable your adblocker by doing this:
    • Click on the adblocker icon in your browser's toolbar.
    • Select "Pause on this site" or a similar option for lancecourse.com.

Mirage of good code

Welcome programmers. Glad to have you on this post. Our today's topic is a phenomenon I have noticed with software development since some time now.

In many occasions, you would notice that your software works -or should I say seems to work. But, strange enough, you had a bad code.

I called that the mirage of good code.

enter image description here

The mirage is an optical phenomenon observed in very hot areas. The Heat produces an effect of a mirror on the floor or in the air giving an impressing of having waters. It occurs especially in the desert and dry plains. It's deceptive.

When you write a code which gives you the impression of being correct, it's a mirage of code. This can happen both in huge and small projects. I have been observing that phenomenon in my daily coding exercises. I wanted to understand what could be the cause of such behavior. I use to ask myself this:

  • Can computers run processes independently?
  • Can they run a process correctly once and fail the next time?

This might sound not right to many. Because you would tell me to go for unit tests. I do know that. I am sure there are many people like me out there who don't always go for unit test on all types of projects. I will assume here we will put unit tests as secondary. Besides, I believe that, even with a good unit test system set, this phenomenon can still be noticed. Even within the unit system itself.

When does it happen?

When you find yourself saying the following, note that you are facing a mirage of good code:

  • I don't know how, but it works
  • The program worked and now stopped
  • It worked in the first compilation
  • It was working yesterday, I don't know why today it isn't working
  • etc

What is the cause

I can't say, for now, what exactly causes this phenomenon. I can't neither confirm if it's going to be the same with everyone or with every case. Here are some few reasons I have noticed at my side:

1. Lack of focus

This is very considerable. When the developer does not have full focus on his code, he can make moves which he/she is not supposed to at that moment. This can even make you believe seeing something you didn't see -a good and working code.

2. Introduction of unwanted characters

Several times I found myself dozing with my hands on the keyboard. When that happens, we lose control of the hands for a flashy second. During that short moment of unconsciousness, our fingers can hit a key introducing a strange character.

That character can be added to a variable name, a class or a method name, inside a condition. In this case, you might not notice it until that variable is used. I believe this is a special case for dynamically typed programming languages. Java or C would have warned you of an undeclared variable. With compiled programming languages, you would have few chances of getting away with such errors.

What happens is that since this character or set of characters are introduced unconsciously, the developer would not think of fixing a problem. He would not look back and forth. The first idea is to continue where he/she was.

With this, you might go ahead and start running your code without suspecting any issue.

While I used the case of sleep, this can happen on many other occasions. Like when you are talking or even singing while coding. We usually tend to write the words from our mouth. It might also be that you drop the code for a while and came back to it.

3. Illusive perception

The third reason I've noticed is the illusion of seeing something thinking of another. This case is very common with me particularly. At many occasions I find myself writing something like $_SERVER['client'] thinking I am writing $_SESSION['client'].

In a sea of code, you need to look twice before you can notice what is going on.

4. Unused code

Yes, it happens that you can have an unused code. an unused code is a code which is supposed to run when a given condition is fulfilled. Until that condition occurs, that section of your code sleeps. Look at this:

<?php

if ( 1000 > count($users) ) {
    // run a big logic here
} else {
    // until then, keep this logic sleeping
}

Your mirage can be in the else block. When the condition is rare in your system, it might take a lot of time to notice what exactly goes on there. An example can be when you expect an IPN from PayPal. In case it doesn't come then the else block runs. It's very rare to see that situation happen. But, we can't exclude the possibility. You might not know what goes on there until that day comes. That's when you say tell your boss: hmm, the system was running all the time. I don't understand what happened.

Recently I worked on a multi-level network marketing system. The system was moving its users from one stage to another. At each stage, there was always a specific logic to execute. By the time of this writing, I am still having parts of the code in higher stages that have not run yet. Of course, I have run them in my testing environment.

5. Host system change

When the system set to host your code gets updated or downgraded it could cause it to misbehave.

Imagine a website built upon PHP 5.4. After running for some time(years), the server gets upgraded to PHP 7. If you don't take your time to upgrade the code as well, you will have a lot of dead pages.

This issue happens all the time even with our packages managers. NPM or Composer being updated can cause many packages to misbehave. This can make think that your machine just changed its mind. LOL.

While all this seems to be turning around focus, it's strange to see how it varies at different levels. There is no scientific solution to this phenomenon. Because it's linked to human behavior.

To cut the situation, we have to adopt unit and functional tests. Testing tools are now unavoidable. Especially when the software becomes huge and complex.

Besides, it's a coding problem, and that's what makes us programmers. We love problems because they pave roads to knowledge.

There are many cases to prove this. I would like to hear from you Your experiences with this phenomenon.

Thank you for reading.