Skip to content

Commit 61f39e9

Browse files
authored
Create best-practices.md
1 parent 12d5bf9 commit 61f39e9

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

best-practices.md

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
2+
# Coding Style Guides/Best Practices
3+
4+
Writing readable code is an art and following style guides is one way of ensuring our code is always clean, readable and consistent. Language specific style guides exist but general coding standards apply to all programming languages. These are not to be blindly followed; strive to understand these and ask when in doubt.
5+
6+
## General
7+
- Don't duplicate the functionality of a built-in library (Note that this is not the case if the task at hand is purely algorithmic).
8+
- Don't swallow exceptions or "fail silently".
9+
- Don't write code that guesses at future functionality.
10+
- Exceptions should be exceptional.
11+
12+
## Naming
13+
14+
- Make use of meaningful variable names. Avoid abbreviations.
15+
- Name the enumeration parameter the singular of the collection. e.g when looping over a collection of books, say for book in books and not for b in books. It’s clearer this way.
16+
- Name variables created by a factory after the factory (`userFactory` or `UserFactory` creates user).
17+
- Name variables, methods, and classes to reveal intent. Intent describes the function of the class, method or variable. A typical example is if we were describing a class method to sort a collection by a certain parameter. It’s preferred to define this as `obj.sort_by(param)` or `obj->sort_by(param)`. It makes code more readable and requires less documentation.
18+
- Treat acronyms as words in names (`XmlHttpRequest` not `XMLHTTPRequest`), even if the acronym is the entire name (class `Html` not class `HTML`).
19+
- Suffix variables holding a factory with _factory (user_factory)
20+
21+
22+
## Formatting
23+
24+
- Avoid inline comments.
25+
- Avoid deep indentation. (max 3 level)
26+
- Break long lines after 80 characters.
27+
- Delete trailing whitespace.
28+
- Don't include spaces after `(, [` or before `], ).`
29+
- Don't misspell.
30+
- Don't vertically align tokens on consecutive lines.
31+
- Do not leave commented out code within production code.
32+
- If you break up an argument list, keep the arguments on their own lines and closing parenthesis on its own line. *Example 1, Example 2.*
33+
- If you break up a hash/dictionary/associative array, keep the elements on their own lines and closing curly brace on its own line.
34+
- Indent continued lines two spaces.
35+
- Indent private methods equal to public methods.
36+
- If you break up a chain of method invocations, keep each method invocation on its own line. Place the . at the end of each line, except the last. Example.
37+
- Use 2 space indentation (no tabs) or indentation specific to your stack.
38+
- Use an empty line between methods or the required number of spaces specified in the respective style guide.
39+
- Use empty lines around multi-line blocks.
40+
- Use spaces around operators, except for unary operators, such as !.
41+
- Use spaces after commas, after colons and semicolons, around `{ and before }.`
42+
- Use Unix-style line endings `(\n).`
43+
- Use uppercase for SQL keywords and lowercase for SQL identifiers.
44+
45+
46+
## Code Organization
47+
48+
- Ensure to always take advantage of inbuilt language functionality like packages, modules etc. to organise the different parts of your code.
49+
- Order methods so that caller methods are earlier in the file than the methods they call.
50+
- Order methods so that methods are as close as possible to other methods they call.
51+
- For language specific style guides, see below.
52+
53+
*Note: It is helpful to run your code through a linter to easily help point you to areas where code is not conforming to style guides. Better still, plugins exist for various IDEs to help with this.*
54+
55+
JavaScript - [Guide](https://eslint.org/docs/rules/)
56+
HTML - [Guide](https://www.w3schools.com/html/html5_syntax.asp)
57+
Vue - [Guide](https://eslint.vuejs.org/rules/)
58+
PHP - [Guide](https://www.php-fig.org/psr/psr-2/)
59+
Python - [Guide](https://www.python.org/dev/peps/pep-0008/)
60+
Java - [Guide](https://google.github.io/styleguide/javaguide.html)

0 commit comments

Comments
 (0)