Coding Standards » History » Version 10

Tom Clegg, 08/28/2014 09:48 AM

1 1 Tom Clegg
h1. Coding Standards
2 1 Tom Clegg
3 3 Tom Clegg
The rules are always up for debate. However, when debate is needed, it should happen outside the source tree. In other words, if the rules are wrong, first debate the rules in IRC etc., then fix the rules, then follow the new rules.
4 1 Tom Clegg
5 2 Tom Clegg
{{toc}}
6 1 Tom Clegg
7 2 Tom Clegg
h2. Git commits
8 2 Tom Clegg
9 1 Tom Clegg
Make sure your name and email address are correct.
10 1 Tom Clegg
11 1 Tom Clegg
* Use @git config --global user.email foo@example.com@ et al.
12 1 Tom Clegg
* It's a little unfortunate to have commits with author @foo@myworkstation.local@ but not bad enough to rewrite history, so fix this before you push!
13 1 Tom Clegg
14 9 Tom Clegg
Refer to a story number in each commit comment.
15 9 Tom Clegg
16 9 Tom Clegg
* @1234: Remove useless button.@
17 9 Tom Clegg
18 9 Tom Clegg
*When merging/committing to master,* refer to the story number in a way Redmine will notice. Redmine will list these commits/merges on the story page itself.
19 9 Tom Clegg
20 9 Tom Clegg
* @closes #1234@, or
21 9 Tom Clegg
* @refs #1234@
22 9 Tom Clegg
23 1 Tom Clegg
Use descriptive commit comments.
24 1 Tom Clegg
25 1 Tom Clegg
* Describe the delta between the old and new tree. If possible, describe the delta in *behavior* rather than the source code itself.
26 9 Tom Clegg
* Good: "1234: Support use of spaces in filenames."
27 9 Tom Clegg
* Good: "1234: Fix crash when user_id is nil."
28 1 Tom Clegg
* Less good: "Add some controller methods." (What do they do?)
29 1 Tom Clegg
* Less good: "More progress on UI branch." (What is different?)
30 1 Tom Clegg
* Less good: "Incorporate Tom's suggestions." (Who cares whose suggestions -- what changed?)
31 1 Tom Clegg
32 1 Tom Clegg
If further background or explanation is needed, separate it from the summary with a blank line.
33 1 Tom Clegg
34 1 Tom Clegg
* Example: "Users found it confusing that the boxes had different colors even though they represented the same kinds of things."
35 1 Tom Clegg
36 2 Tom Clegg
h2. Source files
37 1 Tom Clegg
38 10 Tom Clegg
No TAB characters in source files. "Except go programs.":https://golang.org/cmd/gofmt/
39 1 Tom Clegg
40 6 Tom Clegg
* Emacs: add to @~/.emacs@ → @(setq-default indent-tabs-mode nil)@
41 6 Tom Clegg
* Vim: add to @~/.vimrc@ → @:set expandtab@
42 8 Tom Clegg
* See [[Coding Standards#Git setup|Git setup]] below
43 4 Ward Vandewege
44 6 Tom Clegg
No inline comments: @this = !desired; # we don't want to do it.@
45 4 Ward Vandewege
46 6 Tom Clegg
No long (>80 column) lines, except in rare cases when the alternative is really clunky.
47 6 Tom Clegg
48 4 Ward Vandewege
No whitespace at the end of lines. Make git-diff show you:
49 5 Ward Vandewege
50 5 Ward Vandewege
  git config color.diff.whitespace "red reverse"
51 6 Tom Clegg
git diff --check
52 1 Tom Clegg
53 1 Tom Clegg
No commented-out blocks of code that have been replaced or obsoleted.
54 1 Tom Clegg
55 1 Tom Clegg
* It is in the git history if we want it back.
56 1 Tom Clegg
* If its absence would confuse someone reading the new code (despite never having read the old code), explain its absence in an English comment. If the old code is really still needed to support the English explanation, then go ahead -- now we know why it's there.
57 1 Tom Clegg
58 1 Tom Clegg
No commented-out debug statements.
59 1 Tom Clegg
60 1 Tom Clegg
* If the debug statements are likely to be needed in the future, use a logging facility that can be enabled at run time. @logger.debug "foo"@
61 1 Tom Clegg
62 1 Tom Clegg
Adopt indentation style of surrounding lines or (when starting a new file) the nearest existing source code in this tree/language.
63 1 Tom Clegg
64 1 Tom Clegg
If you fix up existing indentation/formatting, do that in a separate commit.
65 1 Tom Clegg
66 1 Tom Clegg
* If you bundle formatting changes with functional changes, it makes functional changes hard to find in the diff.
67 6 Tom Clegg
68 7 Tom Clegg
h2. Git setup
69 6 Tom Clegg
70 7 Tom Clegg
Configure git to prevent you from committing whitespace errors.
71 1 Tom Clegg
72 6 Tom Clegg
<pre>
73 7 Tom Clegg
git config --global core.whitespace tab-in-indent,trailing-space
74 7 Tom Clegg
git config --global apply.whitespace error
75 6 Tom Clegg
</pre>