From c5bdb612c14fcfc64245d966257bcffb04f08f0c Mon Sep 17 00:00:00 2001 From: Lars Moelleken Date: Sun, 1 Mar 2015 23:50:33 +0100 Subject: [PATCH] adding some code-guidelines ... --- Coding-style-guide.md | 333 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 311 insertions(+), 22 deletions(-) diff --git a/Coding-style-guide.md b/Coding-style-guide.md index f5005ed..5299b13 100644 --- a/Coding-style-guide.md +++ b/Coding-style-guide.md @@ -1,49 +1,338 @@ -Variables ---------- +##General Code-Style## + +While you should follow the code-style that's already there for files that you're modifying, the following are required for any new code. + +###Indentation### + +Indent 2 spaces. No tabs. + +Use blank lines between blocks to improve readability. Indentation is two spaces. Whatever you do, don't use tabs. For existing files, stay faithful to the existing indentation. + +###Line Length and Long Strings### + +Maximum line length is 80 characters. + +If you have to write strings that are longer than 80 characters, this should be done with a here document or an embedded newline if possible. Literal strings that have to be longer than 80 chars and can't sensibly be split are ok, but it's strongly preferred to find a way to make it shorter. + +``` +# Bad: +long_string_1="I am an exceptionalllllllllllly looooooooooooooooooooooooooooooooooooooooong string." + +# Good: +cat <&2 + exit "${E_BAD_MOVE}" +fi + +# Good: use "$?" to get the last return value +mv "${file_list}" "${dest_dir}/" +if [[ "$?" -ne 0 ]]; then + echo "Unable to move ${file_list} to ${dest_dir}" >&2 + exit "${E_BAD_MOVE}" fi ``` + +##Features and Bugs## + +###Command Substitution### + +Use $(command) instead of backticks. + +Nested backticks require escaping the inner ones with \. The $(command) format doesn't change when nested and is easier to read. + +``` +# Bad: +var="`command \`command1\``" + +# Good: +var="$(command "$(command1)")" +``` + +###Eval### + +Eval is evil! Eval munges the input when used for assignment to variables and can set variables without making it possible to check what those variables were. + +##Sources## + +- [Shell Style Guide](https://google-styleguide.googlecode.com/svn/trunk/shell.xml) +- [BASH Programming - Introduction HOW-TO](http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html) +- [Linux kernel coding style](https://www.kernel.org/doc/Documentation/CodingStyle) \ No newline at end of file