Coding

General rules regarding coding style

  • Use tabs only for indentation. Tab width depends on the language:
    • Java, PHP, CSS, JavaScript, Go - 4 characters
    • C, autotools, m4, Perl, SQL - 8 characters
  • There should be no trailing whitespace.
  • Use LF line endings only.
  • All text files should end with a single newline:
    • C ISO standard requires one:
      123 A source file that is not empty shall end in a new-line character, which shall not be immediately preceded by a backslash character before any such splicing takes place
    • PHP permits one:
      The closing tag for the block will include the immediately trailing newline if one is present
    • IEEE POSIX:
      3.205 Line: A sequence of zero or more non-<newline>s plus a terminating <newline>.
      3.392 Text File: A file that contains characters organized into one or more lines. The lines do not contain NUL characters and none can exceed {LINE_MAX} bytes in length, including the <newline>
  • Unused code should not be commented out, but removed.
  • Comment text should be separated with a single space from the comment indicator.
  • Non-obvious code should be commented. Also, as a general rule, if at least one developer was puzzled about something, it should be commented as well.
  • All Zabbix developers are encouraged to read Deep C by Olve Maudal and Jon Jagger.
File names
  • The total length of the name and path of a file must be less than 83 symbols. Older tar implementations have a file length limit of 100 characters. Since 16 characters are reserved for the root folder (for example "zabbix-2.2.22rc2") and 1 for the directory separator that leaves 83 characters for other files.

You can use the following command to check if any file in the current directory exceeds this limitation:

find . | grep -v ".svn" | awk 'length > 85 {print $1,length-2}'

Or a version without grep/awk:

path_limit=83; path_limit=$[$path_limit+2]; while read path; do [[ ${#path} -gt $path_limit ]] && echo ${#path} $path; done < <(find ./ ! -wholename '*/.svn*')
Database rules

Database table and field names use underscores between words. For example, 'expand_macros'. While old table and field names lack underscores, they will not be mass changed, but new tables and fields, and ones that have to be modified for other reasons, should use underscores.

Profile index key naming

Profile index keys (idx field in table profiles) should be prefixed with web. Main categories/namespaces are dot-separated. Lowercase only is used and words are separated with underscores: _. For example: web.hostinventories.filter_field_value

If a key may hold some entity and may be specified more than once, it is still named in the singular (as that specific instance may contain one entity only). For example, web.dashconf.groups.groupid should be used (not web.dashconf.groups.groupids).

External dependencies

External dependencies add maintenance overhead and should be avoided where possible. Regardless the language used (C, Go, PHP, etc.), every new dependency on a third-party library must be carefully evaluated (technically and legally), discussed with a team lead and then approved by a senior staff engineer.

Language-specific rules