The year is 3000, earth is an abandoned wasteland, no one around anymore. A shuttle suddenly breaks the silence, it’s land in the middle of no where, and a pair of scientists emerge, looking towards that one building they can see on the horizon. They rush to it, look around to find anything useful but nothing stands out, “Fear not, I have found an intact computer terminal!” one of them exclaims, “Hurry up, lets see what we can find!”. They fire it up, some images come on the screen and the terminal is finally awaiting input. “What should I look for?” the one using the terminal asks, “Anything of value, open that file called abcdf.cpp it might hold some valuable information”. site subdomains The file is opened, line after line of text show up, but no meaning can be found, no hint or clue as to the purpose of the file, they both look at each other confused, “It appears to be some ancient language, but I can’t make sense of it” one of them says, the other responds “You are right, but what is x, what is y, they are the key to this, they are used throughout the file, they must hold some significant meaning!”.
But lo and behold for it was not of any meaning, it was Jim, the lazy programmer, who did not know maintainable code, who named variables with ambiguous names, and made his colleagues curse the day they had to look at his code.
What is maintainable code?
Maintainable code is code that is written with expressions and statements such that it is easy for anyone to understand and modify the original code without much trouble. What this means is that your code is easy to read, easy to understand what each variable represents, and easy to determine what is the function of the code, and unbeknownst of him, his code will continue to annoy and waste people’s precious time long after he’s gone.
int x, y, z;
z = x + y;
int number1, number2, sum;
sum = number1 + number2;
In Exhibit A, we have three variables named with letters, they were used later for an operation, Exhibit B you have 3 variables appropriately named, and used in an operation as expected. Now look back at Exhibit A, did you guess correctly what each variable was going to be used for? Probably only after you looked at the second line, no imagine of those two lines weren’t next to each other, imagine if instead of only three variables this was done on twenty variables, that extra second you took here can go up to ten minutes, sometimes even more.
Besides being a good practice in general, maintainable code has several benefits:
1. Helps you debug more easily.
2. Allows anyone to understand what your program does simply by looking at it.
3. Also allows anyone to maintain your code.
4. Improves your relationship with your co-workers. You think I’m kidding about the last one? Ask Jim, he’s not popular, it’s not a coincidence.
I’m glad you asked, it’s not too complicated, it’s mostly common sense if anything. But here are general directions:
1. Use appropriate names for variables, use full words whenever possible (don’t use ‘X’ unless you mean the X-axis, this ain’t math, we don’t use X for everything!).
2. If there is a complex operation you need to perform, put it under a function, not only does this allow you to use the same operation more easily in the future, but it also looks better in the code.
3. Do related operations and assigns next to each other whenever possible, logical blocks like this make the directive of your code easy to predict.
4. Indentation, spacing, and line breaks whenever needed. Think of your code as a paragraph, each tells a story.
I don’t think any programmer has not once looked at code and said “Who wrote that!?”, don’t be part of the problem, be part of the solution. And don’t be like Jim, those scientists could be on their way to understanding our civilization by now, but instead they’re stuck reading Jim’s code and probably getting attacked by whatever inhabits the earth by that time, not maintaining your code can be fatal.