programming style

Making Code Easier to Maintain and Modify

Whether the original programmer or someone else needs to make a change in the code the job is much easier if the original programmer used lots of comments, gave the variables and constants descriptive names, and sketched out the basic structure of the program at the very beginning in pseudo-code.

Using Comments in Code

The use of comments can mean the difference between code which any competent programmer can maintain or modify and a program that even the original programmer has trouble figuring out. Every routine should start with at least one comment that documents the purpose of the routine and any non-obvious dependencies or effects that the routine may have on other portions of the program.

In a development environment in which several programmers will be contributing code, adding a comment identifying the person who wrote the code will definitely help others to know who to ask if a question should arise.

Using Descriptive Names for Variables, Constants and Functions

It can be very tempting to use short names for variables and constants but it is not a good programming practice. A name such as DateOfBirth is much easier for other programmers to understand than dob.

Global variable names can all start with a lower case “g” so that any programmer looking at the code will instantly know which entities are local and which are global. Likewise, a lower case “k” can be the first letter of constants. This type of self-documenting code greatly reduces the need for comments and some typical errors.

Using Pseudo-code in Comments

When first developing the structure of a program it can be very helpful to write out the different routines in pseudo-code. This is language which resembles a cross between English and the programming language that the code will eventually be written in.

Using pseudo-code allows the programmer to concentrate on the conceptual aspects of the program without being distracted by syntax rules. The pseudo-code can also be the basis of the comments so it can server two purposes.

Using Modular Coding

Whenever possible, the lines of a routine should fit entirely on one screen of the editor. By keeping routines short, it is easier to comprehend them and see errors. Having short routines also forces the programmer to break each task into distinct sub-tasks, each of which is easier to maintain and modify in the future.

Modular coding also has the advantage of creating reusable routines that can be used in other programs. Once a routine is debugged and verified it is easier to copy and paste it into another program than to write it all over again.

Following these simple suggestions will make a programmer’s code easier to maintain and modify. It may seem like more work, but in the end the net result is greater efficiency and fewer mistakes.

Leave a Reply

Your email address will not be published.