4
0

CODING 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. The following guidelines are intended to aid programmers in creating
  2. code that is consistent with the existing core plugins.
  3. The primary goals of these standards are internal consistency, and
  4. readability in a wide range of environments.
  5. 1. C Language Programming
  6. All code should comply with the requirements of the Free Software
  7. Foundation Coding standards (which are currently available at
  8. http://www.gnu.org/prep/standards_toc.html). We also follow most of
  9. the FSF guidelines. Developers may suggest deviations from the FSF
  10. style recommendations, which will be considered by open discussion on
  11. the nagiosplug-devel mailing list. Any such deviations will apply to
  12. the entire code base to ensure consistency.
  13. Currently, the exceptions to FSF recommendations are roughly equivalent
  14. to GNU indent with invoked as 'indent -ts 2 -br'. Specifically, the
  15. exceptions are as follows:
  16. a) Leading white space for a statement should be formatted as tabs,
  17. with one tab for each code indentation level.
  18. b) Statement continuation lines should; format whitespace up to the column
  19. starting the statement as tabs, format the rest as spaces (this
  20. results in code that is legible regardless of tab-width setting).
  21. c) With the exception of the above, tabs should generally be avoided.
  22. d) When tab width is 2 spaces, line-length should not exceed 80
  23. characters.
  24. e) The opening brace of an if or while block is on the same line as
  25. the end of the conditional expression (the '-br' option).
  26. f) All input, whether user or application based, should be validated for size
  27. and type prior to further use. One such example would be using strncpy()
  28. instead of strcpy() to validate that the copied object does not overflow the
  29. bounds of the object being copied to.
  30. 2. Perl Language Programming
  31. Taken from the O'Reilly book "Programming Perl" (3rd edition, pages 604-606)
  32. with modifications for clarity and to cohere with C coding standards.
  33. *) Always check the return code of system calls.
  34. a) Use tab indentation.
  35. b) Put space before the opening brace of a multiline block.
  36. c) A short block may be put on one line, including braces.
  37. d) Never omit the semicolon.
  38. e) Surround most operators with space.
  39. $x = 5; # do this
  40. $y=5; # don't do this
  41. f) Surround a "complex" subscript (inside brackets) with space.
  42. g) Put empty lines between chunks of code that do different things.
  43. *) Always check the return code of system calls.
  44. h) Put a newline between closing brace and else or elsif.
  45. i) Do not put space between a function name and its opening parenthesis.
  46. j) Do not put space before a semicolon.
  47. k) Put space after each comma.
  48. l) Break long lines after an operator (but before 'and' and 'or', even when
  49. spelled as && and ||)).
  50. *) Always check the return code of system calls.
  51. m) Line up corresponding items vertically.
  52. n) Use redundant parentheses only where it increases readability.
  53. o) An opening brace should be put on the same line as its preceding keyword,
  54. if possible; otherwise, line them up vertically.
  55. while ($condition) {
  56. # do something
  57. }
  58. while ($this_condition and $that_condition and $some_other_condition
  59. and $this_really_really_really_long_condition)
  60. {
  61. # do something
  62. }
  63. p) Do things the most readable way. For instance:
  64. open(FOO, $foo) or die "Can't open $foo: $!";
  65. is better than
  66. die "Can't open $foo: $!" unless open(FOO, $foo);
  67. because the second way hides the main point of the statement in a modifier.
  68. q) Just because an operator lets you assume default arguments doesn't mean
  69. that you should always use them. The defaults are there for lazy programmers
  70. writing one-shot, non-shared programs. If you want your program to be readable,
  71. consider supplying the argument.
  72. r) Choose mnemonic identifiers. That is, don't name your variables $h, $c
  73. and $w. Try $hostaddress, $critical and $warning instead ($host, $crit and
  74. $warn is OK too).
  75. s) Use underscore to split words in long identifiers. That is, use
  76. $service_port instead of $ServicePort as the former is much more readable.
  77. *) Always check the return code of system calls.
  78. 3. Reserved and Semi-Reserved Plugin Options:
  79. -h, --help (REQUIRED!!!!!)
  80. -V, --version (REQUIRED!!!!!)
  81. -v, --verbose
  82. -q, --quiet
  83. -t, --timeout = INTEGER (senonds)
  84. -c, --critical = (INT|FLOAT|RANGE|LIST)
  85. -w, --warning = (INT|FLOAT|RANGE|LIST)
  86. -H, --hostname = STRING
  87. -F, --file = STRING (usually input)
  88. -O, --output = STRING (output file)
  89. -4, (use IPv4)
  90. -6, (use IPv6)
  91. Recommended, but not reserverd:
  92. -I, --ipaddress = STRING
  93. -C, --community = STRING
  94. -a, --auth(info) = STRING (authentication or password)
  95. -l, --logname = STRING
  96. -p, --password = STRING
  97. -P, --port = INT
  98. -u, --url = STRING (also --username if --url is not needed)
  99. Port really should alway be '-P' (uppercase) -- but the plugins
  100. are currently inconsistent in that regard and may be hard to change
  101. as we do not want to break compatibility with earlier syntax.