Skip to content
Dirk Wetter edited this page Dec 8, 2015 · 11 revisions

Coding Style

This is a short brain dump which is far from being complete

  • Global variables

    • in CAPS
    • use them only when necessary
    • initialize them
    • use readonly and variable types
    • if it's going to be a cmd line switch, there has to be also a global ENV variable which can be used without the switch (see e.g. SNEAKY or SSL_NATIVE)
  • local variables (all_lower_case)

    • declare them before usage
    • if unclear initialize them
  • use "speaking variables" but don't overdo it with the length

  • test before PR! If it's a check you need two bad and two good examples which should work as expected (compare results e.g. with SSLlabs)

  • don't use backticks anymore

  • use double square instead of single square brackets

  • use shellcheck if possible

  • especially with sed you need to be careful as GNU sed is only 80% compatible with BSD sed (sed -i, \n, \t, etc.).

  • be careful with very advanced bash features. Mac OS X is still somewhat behind with bash -- it uses bash version 3 (http://tldp.org/LDP/abs/html/bashver4.html)

  • don't use additional binaries

    • if you really, really need to use an additional one make sure it's available on the system before calling it
    • don't use highly system depended binaries (rpm, ipconfig, ..) as it is not portable or requires lot's of efforts and ugly code to be portable.
  • every operation (string, etc.) which works with bash internall functions: use them whenever possible (replacing tr/sed/awk)

  • avoid a mix of e.g. (sed and awk) or (cut and sed) or (sed, tr and <whatsoever>).

  • always use a return value for a function/method

  • use the short functions / methods

    • count_words() / count_lines() / count_ciphers()
    • strip_lf() / strip_spaces()
    • toupper()
    • newline_to_spaces()
    • is_number() / is_ipv4addr() / is_ipv6addr()

Clone this wiki locally