Every programmer knows that there are multiple ways to solve the same problem in a programming language like C or C++. This can be considered both a good or a bad thing. It is a good thing because you have the “flexibility” to solve the problem in different ways.
But the “flexibility” can also be considered as a bad thing, because you can use different solutions for the same problem in different places of the same program. This is considered a bad thing, because you not only use unnecessary CPU cycles or memory, but it also affects the readability of the source code.
In this tutorial we look at some ways to increase the readability of the program you write.
The Use of Functions
As mentioned before, it is considered a bad thing to solve the same problem in the same program in different ways.
A better way is to write a function for solving a problem. This way you can solve the problem over and over again (the same way) by calling the function in the program. This will increase the readability tremendously because you will use less code and if you give the function a descriptive name you also know what that line of code does if you read it.
Another way to increase readability is by using standard functions and data structures.
(Such as the STL library provides for example.) It is always better to use existing libraries then writing your own. Why?
- It is normally better written (error handling etc)
- It usually faster.
- If you create a new function people will assume that it is somehow different from a standard available function.
- People will remember function prototypes overtime, thus easier to read.
Always try to use the appropriate language features. This will increase the readability a lot.
For example some things you need to avoid:
- Choose the right data type (use an integer if you never need a decimal for instance.)
- If a value must be unsigned then use an unsigned number.
- Avoid the use of the wrong kind of loops.
- If a value never changes then use a const
- Etc. (You will get the picture.)
Another good rule is that you should avoid the use of a feature because a feature exists.
For example you should avoid the use of do-while loops. The feature exists, but a do-while loop can be easily avoided.
You should avoid the use of so called magic numbers. A magic number is a number that is used in the code without an obvious reason.
x = y + 25; for (int i=0; i < 30; ++i)
As you can see we have two magic numbers in this code (25 and 30.)
You don’t know what they mean or what for effect they have on the end result.
A better way is to use macros in C or constants in C++. This way you can give a descriptive name to the variable.
This will increase the readability plus it will make it easier to change things if you have used the constants multiple
times in the code (you have only to change it in one place.)
When you use complex expressions in your program then you must try to avoid putting everything on one single line.
For example: if you use multiple levels of parentheses, then it can be very useful to use temporary variables to avoid one-line calculations. This will give you two advantages:
- It is much easier to read and to follow if you use multiple lines.
- You can use a distinctive name for the temporary variables, which in turn will increase the readability.
Another advantage of complex expressions on multiple lines is that you can use comment lines for each section of
the complex expression, which in turn will increase the readability of the source code.
As you can see there are many ways to increase the readability of a program (and these are just a few of them.)
So next time you write a program also think about the readability and program style of your source code.