| |||||||||
Programming style (also called coding standards or code convention) is a term that describes conventions for writing source code in a certain programming language.
Programming style is often dependent on the actual choice of programming language one is writing in. C style will vary from BASIC style, and so on.
Good style, being a subjective matter, is difficult to concretely categorize; however, there are a number of general characteristics.
Appropriate choices for variable names is seen as the keystone for good style. Poorly-named variables make code harder to read and understand.
For example, consider the following pseudocode snippet:
Because of the choice of variable names, the function of the code is difficult to work out. However, if the variable names are made more descriptive:
the code's intent is easier to discern, namely, "Given a 24-hour time, true will be returned if it is in the morning and false otherwise."
Indent style, in programming languages that use braces or indenting to delimit logical blocks of code, such as C, is also a key to good style. Using a logical and consistent indent style makes one's code more readable. Compare:
with something like
The first example is much easier to read because it is indented well, and logical blocks of code are grouped and displayed together more clearly.
The use of logical control structures for looping adds to good programming style as well. It helps someone reading code to understand the program's sequence of execution (in imperative programming languages). For example, in pseudocode:
The above snippet obeys the two aforementioned style guidelines, but however the following using the "for" construct makes the code much easier to read:
Free-format languages often completely ignore whitespace. Making good use of spacing in one's layout is therefore considered good programming style.
Compare the following examples of C code.
with
Python forces the use of indentation to mark control structures. By doing this, the need for bracketing with curly braces ({ and }) is eliminated, and readability is improved while not interfering with common coding styles. However, some programmers do not like being forced to use a style they didn't choose.
See also: identifier naming convention, hrair limit, computer bug