Wednesday, August 31, 2011

Neat Coding

Green blocks show indentation, the red lines show skeleton code that
Delphi creates for you when you start coding for a new procedure in the
event handler.
When you are coding, particularly if your program is complicated or makes use of many if and case statements, it is vital that you are able to read your code quickly and easily. Understanding what you are typing through neat coding means that you can code faster and that others will be able to help you if there is a mistake in what you have typed.

The most important thing to remember when keeping your code neat, is proper indentation (<Tab> key). The green on the picture to the left shows indentation. It is easiest to understand if the only text that touches the grey line is the skeleton code that Delphi gives you, the Procedure, Begin and End code.

Assigning of values to you variables and all basic processing and output should be indented once, and everything that goes below the first line of an if or case statement should be indented once more. The begins and ends of an if statement should line up, as should the then and else sections, as shown below.
The next way to make your code easy to understand is to use comments (//) and line-breaks (<Enter> key). Put comments above each different section of your code and separate these sections with line-breaks: The assigning of values to variables, resetting everything on the form, initialising variables, your processing and your outputting. Also place comments above each piece of more complex processing, such as case statements. It can also be helpful to place a comment on the same line as each begin and end (after the end, use "//" and say what piece of code the end is for).

If statement where begin and end are indented to the same degree, as well as then and else indented to the
same degree. This makes it easier to follow normal and nested if statements.

Another aspect to neat code is spacing, capital letters variable naming. Look at the if statement above, between the operator and the variables (iSecondsPassed  >  iSeconds) there is spacing, this makes it easier to read. Between the variable and the divide sign, there is another space. This again makes it easier to read, particularly if you have a long mathematical equation that you are using.
Capital letters help keep your code legible when you have a variable or component with multiple words in the name, putting a capital letter at the beginning of each word makes it that little bit easier to read. Also when converting data types, Such as with StrToInt or FloatToStrF it is easier to read and see what you are converting if there are capital letters.
When naming variables, use your capital letters, and give them names that make sense to you. Give the variables names that describe their function and what is held by them. Use a small letter in front of the variable to denote what type of variable it is. For example put a small r in front of a Real variable or an s in front of a string.

Finally, if you have many extra numbers in your code, such as 30 for the number of seconds or 0.14 for VAT, don't simply leave these as numbers in your code, as they will seem arbitrary if you need to look at the code later. Declare these as constants between Procedure and Begin and give them values there.

Thank-you for reading and remember that neat code is the only code that you can understand more than a month later.

No comments:

Post a Comment