blob: b2904f59d1f46958a41af089c07a240ed765e5cc [file] [log] [blame]
kate.wardbcd68392008-11-12 20:15:30 +00001Coding Standards
2================
3
kate.ward274784e2008-11-14 00:09:18 +00004shFlags is more than just a simple 20 line shell script. It is a pretty
5significant library of shell code that at first glance is not that easy to
6understand. To improve code readability and usability, some guidelines have
7been set down to make the code more understandable for anyone who wants to read
8or modify it.
9
10Function Documentation
11----------------------
12
13Each function should be preceded by a header that provides the following:
14
15#. A one-sentence summary of what the function does
16#. (optional) A longer description of what the function does, and perhaps some
17 special information that helps convey its usage better.
18#. Args: a one-line summary of each argument of the form:
19 ``name: type: description``
20#. Output: a one-line summary of the output provided. Only output to STDOUT
21 must be documented, unless the output to STDERR is of significance (i.e. not
22 just an error message). The output should be of the form:
23 ``type: description``
24#. Returns: a one-line summary of the value returned. Returns in shell are
25 always integers, but if the output is a true/false for success (i.e. a
26 boolean), it should be noted. The output should be of the form:
27 ``type: description``
28
29Here is a sample header: ::
30
31 # Return valid getopt options using currently defined list of long options.
32 #
33 # This function builds a proper getopt option string for short (and long)
34 # options, using the current list of long options for reference.
35 #
36 # Args:
37 # _flags_optStr: integer: option string type (__FLAGS_OPTSTR_*)
38 # Output:
39 # string: generated option string for getopt
40 # Returns:
41 # boolean: success of operation (always returns True)
42
kate.wardbcd68392008-11-12 20:15:30 +000043Variable and Function Names
44---------------------------
45
kate.ward274784e2008-11-14 00:09:18 +000046All shFlags specific constants, variables, and functions will be prefixed
47appropriately with 'flags'. This is to distinguish usage in the shFlags code
kate.wardbcd68392008-11-12 20:15:30 +000048from users own scripts so that the shell name space remains predictable to
49users. The exceptions here are the standard ``assertEquals``, etc. functions.
50
kate.ward274784e2008-11-14 00:09:18 +000051All non built-in constants and variables will be surrouned with squiggle
52brackets, e.g. '${flags_someVariable}' to improve code readability.
kate.wardbcd68392008-11-12 20:15:30 +000053
54Due to some shells not supporting local variables in functions, care in the
55naming and use of variables, both public and private, is very important.
56Accidental overriding of the variables can occur easily if care is not taken as
57all variables are technically global variables in some shells.
58
kate.ward274784e2008-11-14 00:09:18 +000059================================ ========================
60**type** **sample**
61global public constant ``FLAGS_TRUE``
62global private constant ``__FLAGS_SHELL_FLAGS``
63global public variable not used
64global private variable ``__flags_someVariable``
65global macro ``_FLAGS_SOME_MACRO_``
66public function ``assertEquals``
67public function, local variable ``flags_someVariable_``
68private function ``_flags_someFunction``
69private function, local variable ``_flags_someVariable_``
70================================ ========================
kate.wardbcd68392008-11-12 20:15:30 +000071
72Where it makes sense, variables can have the first letter of the second and
73later words capitalized. For example, the local variable name for the total
kate.ward274784e2008-11-14 00:09:18 +000074number of test cases seen might be ``flags_totalTestsSeen_``.
kate.wardbcd68392008-11-12 20:15:30 +000075
76Local Variable Cleanup
77----------------------
78
79As many shells do not support local variables, no support for cleanup of
80variables is present either. As such, all variables local to a function must be
81cleared up with the ``unset`` command at the end of each function.
82
83Indentation
84-----------
85
86Code block indentation is two (2) spaces, and tabs may not be used. ::
87
88 if [ -z 'some string' ]; then
89 someFunction
90 fi
91
92Lines of code should be no longer than 80 characters unless absolutely
93necessary. When lines are wrapped using the backslash character '\', subsequent
94lines should be indented with four (4) spaces so as to differentiate from the
95standard spacing of two characters, and tabs may not be used. ::
96
97 for x in some set of very long set of arguments that make for a very long \
98 that extends much too long for one line
99 do
100 echo ${x}
101 done
102
kate.ward8ccc2992011-06-10 11:15:05 +0000103When a conditional expression is written using the builtin [ command, and that
104line must be wrapped, place the control || or && operators on the same line as
105the expression where possible, with the list to be executed on its own line. ::
106
107 [ -n 'some really long expression' -a -n 'some other long expr' ] && \
108 echo 'that was actually true!'
kate.ward274784e2008-11-14 00:09:18 +0000109
110.. vim:spell
111.. $Id$