1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
Coding Style
============
C programs
----------
- Use C99 without GNU extensions. We compile with -std=gnu99 to support
list.h, but otherwise stay clear of GNU tricks.
- Kernel coding style (https://www.kernel.org/doc/Documentation/CodingStyle)
applies with the following exceptions:
- Points which are kernel specific (e.g. kmalloc)
- Lines can be up to 120 characters (although 80 is better)
- Use scripts/checkpatch-wrapper.sh to check code.
Shell scripts
-------------
- Use tabs for indentation
- Write <, >, <<, >> with space before, but no space after. E.g.
cat <<EOF >"${file}"
- Use $( ) instead of ``
- Use ${ } for variables
- Use 'type <command>' to check if command is installed
- Write "if", "for" and "while" structures with "then"/"do" on the next line
(not with semicolon on the same line). E.g.
if test -f foo.sh
then
do something
fi
for i in a b c
do
echo ${i}
done
- For short constructs, it is okay to use use && and || instead of
if-statements. E.g:
test -e foo || die "cannot find foo"
type special_command || die "cannot find program `special_command`"
- Use ./* rather than ls/$(ls) for lor loops
for i in ./*
do
echo $i
done
- Use "test" instead of [ ] or [[ ]] E.g.
test -n "${x}" && test "${a}" = "${b}"
- Use $(( )) to perform maths operations instead of "let" or "expr". E.g:
: $(( a += 5))
- Write functions like this
my_function () {
some code
}
- Quote variables except in for-loops and in $(( ))
### Stay POSIX friendly:
- Don't use bash specific features such as arrays and [[ ]]
- Avoid non-POSIX re-directions such as '&>', '>&-', '|&', etc
For example, to re-direct both stdout and stderr to /dev/null, do:
command >/dev/null 2>&1
- Don't use echo -e "foo", instead use
printf "%b\n" "foo"
- Don't use sed -i, it is not POSIX
- Don't use curly brace globbing such as "ls *.{c,h}"
|