1 Code for Human Consumption
It is one of the most pervasive misunderstandings in computing that the source code is for the computer's consumption. Computers work with low-level binary code, a series of impenetrable ones and zeroes, or hexadecimal numbers, not the structured high level languages we code in. The reason that these languages were developed was to help the programmer.
In practice, coding for human consumption means coding for clarity first, efficiency and speed second.
2 Comment Often and Comment Well
The comment is the extreme example of a language element for human consumption. Compilers will strip the comments from the executable program. The purpose of the comment is to tell you (and any future developer) what the program is intended to do. Write comments with this in mind and avoid simply restating the code. For example, a good comment: 'Disable button to prevent its activation'. And a bad comment: 'Set cmd = False'
A good test to see that you have got the level of comment right is to ask yourself the question, 'Could someone understand what the program does if all but the comments were removed?'
3 Lay Out Code to Increase Legibility
Just as it is important for an author to split a book into chapters and paragraphs that aid reading, so it is important for the developer to consider the layout of the code and how that can aid the readability of the code. In particular any code branch (an IF..THEN...ELSE construction) and any code repetition (a WHILE...END WHILE construction) should be indented so that it is easy to see where it starts and ends.
4 Expect the Unexpected and Deal With It
Before you open a file, make sure that the file is present. Before you set focus to a control, make sure that the control is visible and enabled. Try to work out what conditions could cause your code to fail and test for them before they cause the program to fall over.
5 Name Your Variables to Aid Readability
There are a number of strategies to variable naming. The key is to be consistent and to be as informative as possible. If you name a variable nMonth, you give the programmer extra information as to what that variable is expected to contain. The Researcher of this entry prefers what's known as the Hungarian notation. This involves putting certain combinations of letters at the beginning of a variable to indicate what type it is - 's' for a string, 'l' for a long. It is known as Hungarian notation because it was introduced by Dr Charles Simonyi of Microsoft, who hailed originally from Hungary. Whichever style you choose to use, just remember to be consistent.
6 Keep Your Functions and Subroutines Simple
A function or subroutine should ideally only do one thing. One of the commonest sources of misunderstandings is a function that performs a number of different operations. It should be split into separate functions for each operation which are easy to reuse, and so that the scope of a code change is easy to understand.
7 Avoid Global Variables
Functions and variables that are only used in one module should not be visible outside that module. Variables that are only used in a function or subroutine should not be visible outside that function or subroutine. Having global variables (variables which are visible to any and every part of the program with no protection from misuse) encourages the bad habit of changing critical variable values anywhere in your program, rather than in the specific area to which the variables relate. This can be an endless source of hard-to-find bugs. Keep your variables local and as private as your language allows.
There are many other hints and tips which can help you become a better and more efficient programmer and the programs you write more maintainable, but the seven secrets listed above will serve as a good foundation - however high you build upon them.