Live manual Debian Live

# Debian Live Manual

# Styl Kodowania

717

# 15. Styl Kodowania

718

Rozdział ten dokumentuje styl kodowania używany w systemach live.

719

15.1 Kompatybilność

720

- Avoid "bashisms", the codebase must be POSIX compliant and thus universally compatible.

721

- Furthermore it must comply with the version of the POSIX specification chosen by the current Debian Policy.

722

- Możesz sprawdzić swoje skrypty używając 'sh -n' i 'checkbashisms'.

723

- Upewnij się, że cały kod powłoki działa z 'set-e'.

724

15.2 Wcięcia

725

- Zawsze używaj tabulatorów zamiast spacji.

726

- Keep case branch terminators (";;") aligned with the "content" of the branch, rather than the branch "entry".

727

Dobrze:

728

case "${1}" in
foo)
foobar
;;
bar)
foobar
;;
esac

729

15.3 Zawijanie

730

- Generally, lines should be 80 chars at maximum.

731

- Placement of keywords like `then` and `do` should be chosen with good judgement with respect to clutter and readability. For small bits of code in particular it should be preferred to have them on the same line as the prior keyword they relate to (`if`; `for`; etc). Only place on the next line where it makes good sense to do so; typically this might only be to comply with maximum line length restrictions. One situation where they should always be placed on the next line is where what they follow is broken up onto multiple lines, and thus it being on a new line creates clear separation between that and the body of code following it. I.e. :

732

Preferred:

733

if foo; then
bar
fi
for FOO in $ITEMS; do
bar
done
if [ "${MY_LOCATION_VARIABLE}" = "something" ] && [ -e "${MY_OUTPUT_FILE}" ]
then
MY_OTHER_VARIABLE="$(some_bin ${FOOBAR} | awk -F_ '{ print $1 }')"
fi
if [ "${MY_FOO}" = "something" ] && [ -e "path/${FILE_1}" ] ||
[ "${MY_BAR}" = "something_else" ] && [ ${ALLOW} = "true" ]
then
foobar
fi

734

Less ideal:

735

if [ "${MY_LOCATION_VARIABLE}" = "something" ] && [ -e "${MY_OUTPUT_FILE}" ]; then
MY_OTHER_VARIABLE="$(some_bin ${FOOBAR} | awk -F_ '{ print $1 }')"
fi

736

Horrible:

737

if [ "${MY_LOCATION_VARIABLE}" = "something" ] && [ -e "${MY_OUTPUT_FILE}" ] || [ "${MY_LOCATION_VARIABLE}" = "something-else" ] && [ -e "${MY_OUTPUT_FILE_2}" ]; then
MY_OTHER_VARIABLE="$(some_bin ${FOOBAR} | awk -F_ '{ print $1 }')"
fi

738

- Prefer placing the opening brace of a function on a new line (for consistency with established style), and keep the braces aligned with the function name:

739

Dobrze:

740

Foo ()
{
bar
}

741

Bad (inconsistent with existing style):

742

Foo () {
bar
}

743

Awful:

744

Foo ()
{
bar
}

745

15.4 Zmienne

746

- Zmienne występują zawsze zapisane drukowanymi literami.

747

- Config variables used in live-build should start with an `LB_` prefix.

748

- Local function variables should be restricted to local scope.

749

- Zmienne dotyczące parametrów startowych live-config zaczynają się od `LIVE_`.

750

- Wszystkie inne zmienne w live-config zacznij przedroskiem `_`.

751

- Używaj nawiasów wokół zmiennych; na przykład napisz `${FOO}` zamiast `$FOO`.

752

- Always protect variables with quotes to respect potential whitespaces (except where necessary to achieve correct word splitting): write `"${FOO}"` not `${FOO}`.

753

_ * Dla zachowania spójności, należy zawsze używać znaków cytatu podczas przypisywania wartości do zmiennych:

754

Źle:

755

FOO=bar

756

Dobrze:

757

FOO="bar"

758

- If multiple variables are used, prefer quoting the full expression:

759

Typically bad:

760

if [ -f "${FOO}"/foo/"${BAR}"/bar ]; then
foobar
fi

761

Dobrze:

762

if [ -f "${FOO}/foo/${BAR}/bar" ]; then
foobar
fi

763

15.5 Różne

764

- Prefer "`|`" (without the surround quotes) as a separator in calls to sed, e.g. "`sed -e 's|foo|bar|'`" (without "").

765

- Don't use the `test` command for comparisons or tests, use "`[`" and "`]`" (without ""); e.g. "`if [ -x /bin/foo ]; ...`" and not "`if test -x /bin/foo; ...`".

766

- Use `case` wherever it makes code more readable than conditional checks (`if foo; ...` and tests without the actual `if` keyword, e.g. `[ -e "${FILE}" ] || exit 0`).

767

- Use "Foo_bar" style names for functions, i.e. a capital first letter, then all lowercase, with sensible use of underscores for better readability.