Live manual Debian Live

# Debian Live Manual

# Estilo de código

750

# 15. Estilo de código

751

En este capítulo se documenta el estilo de código utilizado en los sistemas en vivo.

752

15.1 Compatibilidad

753

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

754

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

755

- Se puede comprobar las secuencias de comandos con 'sh -n' y 'checkbashisms'.

756

- Asegurarse de que el código funcione con 'set -e'.

757

15.2 Sangrado

758

- Utilizar siempre los tabuladores en lugar de espacios.

759

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

760

Bien:

761

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

762

15.3 Ajuste de líneas

763

- Generally, lines should be 80 chars at maximum.

764

- 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. :

765

Preferred:

766

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

767

Less ideal:

768

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

769

Horrible:

770

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

771

- 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:

772

Bien:

773

Foo ()
{
bar
}

774

Bad (inconsistent with existing style):

775

Foo () {
bar
}

776

Awful:

777

Foo ()
{
bar
}

778

15.4 Variables

779

- Las variables deben escribirse siempre en letras mayúsculas.

780

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

781

- Local function variables should be restricted to local scope.

782

- Las variables en relación a un parámetro de arranque en live-config comienzan con `LIVE_`.

783

- Todas las demás variables de live-config comienzan con el prefijo `_`

784

- Utilizar llaves para las variables, por ejemplo, escribir `${FOO}` en lugar de `$FOO`.

785

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

786

- Por motivos de coherencia, se debe utilizar siempre comillas en la asignación de valores a las variables:

787

Mal:

788

FOO=bar

789

Bien:

790

FOO="bar"

791

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

792

Typically bad:

793

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

794

Bien:

795

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

796

15.5 Miscelánea

797

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

798

- 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; ...`".

799

- 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`).

800

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