ADMB Coding Standards
Coding Standards are guidelines and recommendations for writing and maintaining source code. The purpose of standards is to improve readability and to help insure that code written by multiple authors has the same appearance. ADMB uses some rules from
-
"The Elements of C++ Style" by Trevor Misfeldt, Gregory Bumgardner and Andrew Gray. A summary of this references is available on-line.
-
"C++ Coding Standards" by Herb Sutter and Andrei Alexandrescu
-
"Google C++ Style Guide" by Benjy Weinberger, Craig Silverstein,Gregory Eitzmann, Mark Mentovai,Tashana Landray.
General Principles
The most important principles are the first 2 from Misfeldt et al
- Adhere to the style of the original.
- Adhere to the Principle of Least Astonishment.
ADMB Coding Standards
- All files should have CR line endings. Exceptions are the windows batch files. Windows text files that are opened in unix show ^M characters which makes the file less readable.
- Avoid the use of tabs in source files. Tabs are displayed differently in editors. Use two (2) spaces for indents. This is common in most coding standards. See Indent tool below.
- Lines should be no longer than 80 characters. This makes it easier to print and improves readability.
- Avoid spaces at the end of lines.
- Place opening and closing braces on their own lines to avoid having to search for them. For example.
if (x<0.0)
{
y = a;
}
else
{
y = b * x;
}
Indenting tool
Many linux-like systems have the indent utility. The following bash script produces nice-looking code, but it is not perfect, and some additional work with an editor may be required. See.
#!/bin/bash # # reformats source code to ADMB style using indent # see http://www.gnu.org/software/indent/ # # Author: John Sibert # cp -v $1 $1.bak indent -nbad -bap -bbo -nbc -bl -blf -bli0 -bls -c33\ -cd33 -ncdb -ce -ci3 -cli0 -cp33 -cs -d0 -di1 -nfc1\ -nfca -hnl -i3 -ip0 -l75 -lp -npcs -nprs -npsl -saf\ -sai -saw -nsc -nsob -nss $1 -o indent.out cp -v indent.out $1

