Live manual

Debian Live

<< previous toc next >>

Debian Live Manual

Lo stile nello scrivere codice

15. Lo stile nello scrivere codice

This chapter documents the coding style used in live systems.

15.1 Compatibilità

Assicurarsi che tutto il codice giri con 'set -e'.

15.2 Rientri

Corretto:

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

         bar)
                 foobar
                 ;;
esac

15.3 Ritorno a capo

Preferred:

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

Less ideal:

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

Horrible:

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

Corretto:

Foo ()
{
         bar
}

Bad (inconsistent with existing style):

Foo () {
         bar
}

Awful:

Foo ()
         {
         bar
         }

15.4 Variabili

Sbagliato:

FOO=bar

Corretto:

FOO="bar"

Typically bad:

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

Corretto:

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

15.5 Varie


<< previous toc next >>