Sample style rules for programming
One of the simplest ways to enhance the readability of code and documentation
is to develop (and enforce) the use of style guidelines. This will become
critical
when moving to multi-person development, but in many cases substantially
improves
code quality even when applied on an individual basis.
Below we have a set of sample style guidelines for programming.
Obviously there are many possible stylistic choices,
the one below is purely used as an example.
Some of these will correspond closely with the lab standards used for this course,
and you will find comparable patterns of rules you must adhere to in
most software development organizations.
Having such guidelines makes it easier to read, comprehend, debug, and modify large
programs written by a variety of developers over extended periods of time.
- Define a common layout: this ensures you (and others) know where
to look for the definitions of key program parts. For example, all source code
files must follow the organization format shown below:
- User description
- Design description
- Header files (include statements)
- Other pre-processor directives (# statements)
- Global constant definitions
- Prototypes (function and type declarations)
- Main function body
- Other function bodies
- Define subroutine size restrictions:
no function or method, including main, should have more than
40 lines of code including comments and whitespace,
and following the style rules
listed below.
- Define the minimum common files:
Each executable should be accompanied (in the same directory)
by a README.TXT file and a MAKEFILE, which should outline all actions
necessary to created the desired executable and possible variations.
- Define the development environment:
development is to be carried out
using one of the following packages/compilers:
- The g++ compiler for Linux or Unix
- MicroSoft Visual C++ .Net or Visual C++ 6.0 with service pack 5
- Define the code layout/style:
- Define the notaton for identifiers: (the style suggested here is
often referred to in other references as Hungarian notation)
variable and function names should
not be cryptic - they should convey the role of the function or variable
to anyone reading your code.
- Example: the variable name x means nothing to those
unfamiliar with your program, however the name lNumberofSteps
clearly indicates that the variable is being used to record some
number of steps.
- Constants should be declared entirely in uppercase.
- Variables should be prefixed with one or more characters indicating
the variable type:
u : unsigned integer
l : long integer
i : integer
s : short integer
d : double precision float
f : floating point
|
c : character
z : string
p : pointer
g : global
a : array
e : enumerated type
|
- To increase readability, capitalize the first
letter of each word in a variable name, e.g. dToolCount.
The use of underscores (except as noted below) is discouraged.
- Function names and modules: when a program is divided into modules,
there should be a three-character abbreviation used to uniquely
identify each module.
The name of every function and globally-accessible value declared within the
module should begin with that three character abbreviation followed by the
underscore.