1 The following guidelines are intended to aid programmers in creating
2 code that is consistent with the existing core plugins.
4 The primary goals of these standards are internal consistency, and
5 readability in a wide range of environments.
8 1. C Language Programming
10 All code should comply with the requirements of the Free Software
11 Foundation Coding standards (which are currently available at
12 http://www.gnu.org/prep/standards_toc.html). We also follow most of
13 the FSF guidelines. Developers may suggest deviations from the FSF
14 style recommendations, which will be considered by open discussion on
15 the nagiosplug-devel mailing list. Any such deviations will apply to
16 the entire code base to ensure consistency.
18 Currently, the exceptions to FSF recommendations are roughly equivalent
19 to GNU indent with invoked as 'indent -ts 2 -br'. Specifically, the
20 exceptions are as follows:
22 a) leading white space for a statement should be formatted as tabs,
23 with one tab for each code indentation level.
25 b) in statement continuation lines, format whitespace up to the column
26 starting the statement as tabs, format the rest as spaces (this
27 results in code that is legible regardless of tab-width setting).
29 c) with the exception of the above, tabs should generally be avoided
31 d) when tab width is 2 spaces, line-length should not exceed 80
32 characters
34 e) The opening brace of an if or while block is on the same line as
35 the end of the conditional expression (the '-br' option).
38 2. Perl Language Programming
40 Taken from the O'Reilly book "Programming Perl" (3rd edition, pages 604-606) with
41 modifications for clarity and to cohere with C coding standards.
43 *) Always check the return code of system calls.
45 a) Use tab indentation.
47 b) Put space before the opening brace of a multiline block.
49 c) A short block may be put on one line, including braces.
51 d) Never omit the semicolon.
53 e) Surround most operators with space.
55 $x = 5; # do this
56 $y=5; # don't do this
58 f) Surround a "complex" subscript (inside brackets) with space.
60 g) Put empty lines between chunks of code that do different things.
62 *) Always check the return code of system calls.
64 h) Put a newline between closing brace and else or elsif.
66 i) Do not put space between a function name and its opening parenthesis.
68 j) Do not put space before a semicolon.
70 k) Put space after each comma.
72 l) Break long lines after an operator (but before 'and' and 'or', even when
73 spelled as && and ||)).
75 *) Always check the return code of system calls.
77 m) Line up corresponding items vertically.
79 n) Use redundant parentheses only where it increases readability.
81 o) An opening brace should be put on the same line as its preceding keyword,
82 if possible; otherwise, line them up vertically.
84 while ($condition) {
85 # do something
86 }
88 while ($this_condition and $that_condition and $some_other_condition
89 and $this_really_really_really_long_condition)
90 {
91 # do something
92 }
94 p) Do things the most readable way. For instance:
96 open(FOO, $foo) or die "Can't open $foo: $!";
98 is better than
100 die "Can't open $foo: $!" unless open(FOO, $foo);
102 because the second way hides the main point of the statement in a modifier.
104 q) Just because an operator lets you assume default arguments doesn't mean
105 that you should always use them. The defaults are there for lazy programmers
106 writing one-shot, non-shared programs. If you want your program to be readable,
107 consider supplying the argument.
109 r) Choose mnemonic identifiers. That is, don't name your variables $h, $c
110 and $w. Try $hostaddress, $critical and $warning instead ($host, $crit and
111 $warn is OK too).
113 s) Use underscore to split words in long identifiers. That is, use
114 $service_port instead of $ServicePort as the former is much more readable.
116 *) Always check the return code of system calls.