Project

General

Profile

Coding Standards » History » Version 32

Peter Amstutz, 09/13/2023 03:37 PM

1 1 Tom Clegg
h1. Coding Standards
2
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 32 Peter Amstutz
h2. Ready to merge checklist
8
9
Before asking for a branch review, please fill out this template with information about the branch.
10
11
+template begins below, replace the bits between < >+
12
13
<00000-branch-title> @ commit:<git hash>
14
15
<https://ci.arvados.org/... (link to developer test job on jenkins)>
16
17
_Note each item completed with additional detail if necessary.  If an item is irrelevant to a specific branch, briefly explain why._
18
19
* All agreed upon points are implemented / addressed.
20
** _comments_
21
* Anything not implemented (discovered or discussed during work) has a follow-up story.
22
** _comments_
23
* Code is tested and passing, both automated and manual, what manual testing was done is described
24
** _comments_
25
* Documentation has been updated.
26
** _comments_
27
* Behaves appropriately at the intended scale (describe intended scale).
28
** _comments_
29
* Considered backwards and forwards compatibility issues between client and server.
30
** _comments_
31
* Follows our "coding standards":https://dev.arvados.org/projects/arvados/wiki/Coding_Standards and "GUI style guidelines.":https://dev.arvados.org/projects/arvados/wiki/Coding_Standards#GUI-Workbench-2
32
** _comments_
33
34
<Additional detail about what, why and how this branch changes>
35
36
37 2 Tom Clegg
h2. Git commits
38
39 1 Tom Clegg
Make sure your name and email address are correct.
40
41
* Use @git config --global user.email foo@example.com@ et al.
42
* 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!
43
44 19 Tom Clegg
Refer to a story number in the first (summary) line of each commit comment. This first line should be <80 chars long, and should be followed by a blank line.
45 9 Tom Clegg
46
* @1234: Remove useless button.@
47
48
*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.
49
50 1 Tom Clegg
* @closes #1234@, or
51 19 Tom Clegg
* @refs #1234@, or
52
* @no issue #@ if no Redmine issue is especially relevant.
53 9 Tom Clegg
54 1 Tom Clegg
Use descriptive commit comments.
55
56
* Describe the delta between the old and new tree. If possible, describe the delta in *behavior* rather than the source code itself.
57 9 Tom Clegg
* Good: "1234: Support use of spaces in filenames."
58
* Good: "1234: Fix crash when user_id is nil."
59 1 Tom Clegg
* Less good: "Add some controller methods." (What do they do?)
60
* Less good: "More progress on UI branch." (What is different?)
61
* Less good: "Incorporate Tom's suggestions." (Who cares whose suggestions -- what changed?)
62
63
If further background or explanation is needed, separate it from the summary with a blank line.
64
65
* Example: "Users found it confusing that the boxes had different colors even though they represented the same kinds of things."
66
67 18 Tom Clegg
*Every commit* (even merge commits) must have a DCO sign-off. See [[Developer Certificate Of Origin]].
68 1 Tom Clegg
69
* Example: <code>Arvados-DCO-1.1-Signed-off-by: Joe Smith <joe.smith@example.com></code>
70 19 Tom Clegg
71
Full examples:
72
73
<pre>
74
commit 9c6540b9d42adc4a397a28be1ac23f357ba14ab5
75
Author: Tom Clegg <tom@curoverse.com>
76
Date:   Mon Aug 7 09:58:04 2017 -0400
77
78
    12027: Recognize a new "node failed" error message.
79
    
80
    "srun: error: Cannot communicate with node 0.  Aborting job."
81
    
82
    Arvados-DCO-1.1-Signed-off-by: Tom Clegg <tom@curoverse.com>
83
</pre>
84
85
<pre>
86
commit 0b4800608e6394d66deec9cecea610c5fbbd75ad
87
Merge: 6f2ce94 3a356c4
88
Author: Tom Clegg <tom@curoverse.com>
89
Date:   Thu Aug 17 13:16:36 2017 -0400
90
91
    Merge branch '12081-crunch-job-retry'
92
    
93
    refs #12080
94
    refs #12081
95
    refs #12108
96
    
97
    Arvados-DCO-1.1-Signed-off-by: Tom Clegg <tom@curoverse.com>
98
</pre>
99
100 21 Ward Vandewege
h2. Copyright headers
101
102 23 Ward Vandewege
Each Arvados component is released either under the AGPL 3.0 license or the Apache 2.0 license. Documentation is licensed under CC-BY-SA-3.0. See the [[Arvados Licenses FAQ]] for the rationale behind this system.
103 21 Ward Vandewege
104 22 Ward Vandewege
Every file must start with a copyright header that follows this format:
105 21 Ward Vandewege
106
Code under the "AGPLv3 license":http://www.gnu.org/licenses/agpl-3.0.en.html (this example uses Go formatting):
107
108
<pre>
109
// Copyright (C) The Arvados Authors. All rights reserved.
110
//
111
// SPDX-License-Identifier: AGPL-3.0
112
</pre>
113
114
Code under the "Apache 2.0 license":http://www.apache.org/licenses/LICENSE-2.0 (this example uses Python formatting):
115
116
<pre>
117
# Copyright (C) The Arvados Authors. All rights reserved.
118
#
119
# SPDX-License-Identifier: Apache-2.0
120
</pre>
121
122
Documentation under the "Creative Commons Attribution-Share Alike 3.0 United States license":https://creativecommons.org/licenses/by-sa/3.0/us/ (this example uses textile formatting):
123
124
<pre>
125
###. Copyright (C) The Arvados Authors. All rights reserved.
126
....
127
.... SPDX-License-Identifier: CC-BY-SA-3.0
128
</pre>
129
130 1 Tom Clegg
When adding a new file to a component, use the same license as the other files of the component.
131
132 22 Ward Vandewege
When adding a new component, choose either the AGPL or Apache license. Generally speaking, the Apache license is only used for components where integrations in proprietary code must be possible (e.g. our SDKs), though this is not a hard rule. When uncertain which license to choose for a new component, ask on the IRC channel or mailing list.
133 21 Ward Vandewege
134 22 Ward Vandewege
When adding a file in a format that does not support the addition of a copyright header (e.g. in a binary format like an image), add the path to the .licenseignore file in the root of the source tree. This should be done sparingly, and must be discussed explicitly as part of code review. The file must be available under a license that is compatible with the rest of the Arvados code base.
135 21 Ward Vandewege
136 22 Ward Vandewege
When adding a file that originates from an external source under a different license, add the appropriate SPDX line for that license. This is exceptional, and must be discussed explicitly as part of code review. Not every license is compatible with the rest of the Arvados code base.
137 16 Tom Clegg
138 28 Ward Vandewege
There is a helper script at https://github.com/arvados/arvados/blob/master/build/check-copyright-notices that can be used to check - and optionally, fix - the copyright headers in the Arvados source tree.
139 24 Ward Vandewege
140 29 Ward Vandewege
The actual git hook that enforces the copyright headers lives at https://github.com/arvados/arvados-dev/blob/master/git/hooks/check-copyright-headers.rb
141 25 Ward Vandewege
142 13 Tom Clegg
h2. Source code formatting
143 1 Tom Clegg
144 13 Tom Clegg
(Unless otherwise specified by style guide...)
145
146 10 Tom Clegg
No TAB characters in source files. "Except go programs.":https://golang.org/cmd/gofmt/
147 1 Tom Clegg
148 6 Tom Clegg
* Emacs: add to @~/.emacs@ &rarr; @(setq-default indent-tabs-mode nil)@
149
* Vim: add to @~/.vimrc@ &rarr; @:set expandtab@
150 8 Tom Clegg
* See [[Coding Standards#Git setup|Git setup]] below
151 4 Ward Vandewege
152 6 Tom Clegg
No inline comments: @this = !desired; # we don't want to do it.@
153 4 Ward Vandewege
154 6 Tom Clegg
No long (>80 column) lines, except in rare cases when the alternative is really clunky.
155
156 4 Ward Vandewege
No whitespace at the end of lines. Make git-diff show you:
157 5 Ward Vandewege
158
  git config color.diff.whitespace "red reverse"
159 6 Tom Clegg
git diff --check
160 1 Tom Clegg
161 13 Tom Clegg
h2. What to include
162
163 1 Tom Clegg
No commented-out blocks of code that have been replaced or obsoleted.
164
165
* It is in the git history if we want it back.
166
* 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.
167
168
No commented-out debug statements.
169
170
* 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"@
171
172 13 Tom Clegg
h2. Style mismatch
173
174 1 Tom Clegg
Adopt indentation style of surrounding lines or (when starting a new file) the nearest existing source code in this tree/language.
175
176
If you fix up existing indentation/formatting, do that in a separate commit.
177
* If you bundle formatting changes with functional changes, it makes functional changes hard to find in the diff.
178
179 13 Tom Clegg
h2. Go
180
181
gofmt, golint, etc., and https://github.com/golang/go/wiki/CodeReviewComments
182
183
h2. Ruby
184
185
https://github.com/bbatsov/ruby-style-guide
186
187 1 Tom Clegg
h2. Python
188 13 Tom Clegg
189 30 Brett Smith
h3. Python code
190 12 Tom Clegg
191 30 Brett Smith
For code, follow "PEP 8":https://peps.python.org/pep-0008/.
192 1 Tom Clegg
193 30 Brett Smith
When you add functions, methods, or attributes that SDK users should not use, their name should start with a leading underscore. This is a common convention to signal that an interface is not intended to be public. Anything named this way will be excluded from our SDK web documentation by default.
194
195
h3. Python docstrings
196
197
Public classes, methods, and functions should all have docstrings. The content of the docstring should follow "PEP 257":https://peps.python.org/pep-0257/.
198
199
Format docstrings with Markdown and follow these style rules:
200
201
* Document function argument lists as a definition list after the high-level description. The defined term should be the name of the argument and, whenever practical, its type as it would be written in the function signature. The definition should describe the argument's purpose.
202
* Document class constructor arguments in the class docstring. The @__init__@ method is not rendered in the web documentation by default.
203
* When something is deprecated, write a @!!! deprecated@ admonition immediately after the first line. Its text should explain that the thing is deprecated, and suggest what to use instead. For example:<pre>def add(a, b):
204
    """Add two things.
205
206
    !!! deprecated
207
        This function is deprecated. Use the `+` operator instead.
208
209
210
    """</pre>
211
* Mark up all identifiers with backticks. When the identifier exists in the current module, use the short name. Otherwise, use the fully-qualified name. Our web documentation will automatically link these identifiers to their corresponding documentation.
212
* Mark up links using Markdown's footnote style. For example:<pre>"""Python docstring following [PEP 257][pep257].
213
214
[pep257]: https://peps.python.org/pep-0257/
215
"""</pre>This looks best in plaintext. A descriptive identifier is nice if you can keep it short, but if that's challenging, plain ordinals are fine too.
216
* Mark up headers (e.g., in a module docstring) using underline style. For example:<pre>"""Generic utility module
217
218
Filesystem functions
219
--------------------
220
221
222
223
Regular expressions
224
-------------------
225
226
227
"""</pre>This looks best in plaintext.
228
229
The goal of these style rules is to provide a readable, consistent appearance whether people read the documentation in plain text (e.g., using @pydoc@) or their browser (as rendered by @pdoc@).
230
231 12 Tom Clegg
232 11 Brett Smith
h2. JavaScript
233
234 20 Tom Clegg
Follow the Airbnb Javascript coding style guide unless otherwise stated:
235 14 Tom Morris
https://github.com/airbnb/javascript
236 20 Tom Clegg
237
We already have 4-space indents everywhere, though, so do that.
238
239 14 Tom Morris
240 7 Tom Clegg
h2. Git setup
241 6 Tom Clegg
242 7 Tom Clegg
Configure git to prevent you from committing whitespace errors.
243 1 Tom Clegg
244 6 Tom Clegg
<pre>
245 7 Tom Clegg
git config --global core.whitespace tab-in-indent,trailing-space
246 1 Tom Clegg
git config --global apply.whitespace error
247 17 Tom Clegg
</pre>
248
249
Add a DCO sign-off to the default commit message.
250
251
<pre>
252
cd .../arvados
253
printf '\n\nArvados-DCO-1.1-Signed-off-by: %s <%s>\n' "$(git config user.name)" "$(git config user.email)" >~/.arvados-dco.txt
254
git config commit.template ~/.arvados-dco.txt
255
</pre>
256
257
Add a DCO sign-off and "refs #xxxx" comment (referencing the issue# in the name of the branch being merged) to the default merge commit message.
258
259
<pre>
260
cd .../arvados
261 26 Eric Biagiotti
cat >.git/hooks/prepare-commit-msg <<'EOF'
262 17 Tom Clegg
#!/bin/sh
263
264
case "$2,$3" in
265
    merge,)
266
        br=$(head -n1 ${1})
267
        n=$(echo "${br}" | egrep -o '[0-9]+')
268
        exec >${1}
269
        echo "${br}"
270
        echo
271
        echo "refs #${n}"
272
        echo
273 27 Eric Biagiotti
        echo "Arvados-DCO-1.1-Signed-off-by: $(git config user.name) <$(git config user.email)>"
274 17 Tom Clegg
        ;;
275
    *)
276
        ;;
277
esac
278
EOF
279 26 Eric Biagiotti
chmod +x .git/hooks/prepare-commit-msg
280 6 Tom Clegg
</pre>
281 31 Brett Smith
282
h2. GUI (Workbench 2)
283
284
h3. Font Sizes
285
286
* Minimum 12pt (16px) 
287
* Minimum 9 pt (12px) for things like by copyright, footer
288
289
This should be able to be-resized up to 200% without loss of content or functionality.
290
291
h3. Color
292
293
* Text and images of text have a color contrast ratio of at least 4.5:1 You can use "this contrast tool":https://snook.ca/technical/colour_contrast/colour.html#fg=1F7EA1,bg=FFFFFF to check.
294
* Non-text icon, controls, etc - 3:1 must have a color contrast ratio of 3:1.
295
* Avoid hard-coding colors. Use theme colors. If a new color is needed, add it to the theme.  
296
* Used defined grays when possible using RGB value and changing the a value to indicate different meanings (i.e. Active icons have an opacity of 87%, Inactive icons have an opacity of 60%, Disabled icons have an opacity of 38%)
297
298
h3. Icons
299
300
h4. General
301
302
* Interaction target size of at least 44 x 44 pixels
303
* Label should be on right, icon on left for maximum readability
304
* Use minimum 3:1 color contrast (see Color above)
305
* User appropriate concise alt text for people using screen readers 
306
307
h4. Menu/Navigation 
308
309
* No navigation should only supported via breadcrumbs
310
* If less than 5 menu options, consider visible navigation options
311
* If more than 5 menu options, consider a combination navigation where some options are visible and some are hidden
312
* Use the following menu consistently:	
313
** Hamburger (three bars stacked vertically): Used to indicate navigation bar/menu that toggles between being collapsed behind the button or displayed on the screen, often used for global/site-wide/whole application navigation
314
** Döner (three bars that narrow vertically):  Indicates a group filtering menu
315
** Bento (3×3 grid of squares):  Indicates a menu presenting a grid of options (not currently applicable to WB)
316
** Kebab (three dots stacked vertically): Indicates a smaller inline-menu or an overflow/combination menu
317
** Meatballs (three dots stacked horizontally):  Used to indicate a smaller inline-menu.  Often used to indicate action on a related item (i.e. item next to the meatball), good for repeated use in tables, or horizontal elements
318
* If component is an accordion window,  use caret(‸)
319
320
Preferred Icon Repositories:
321
* https://v4.mui.com/components/material-icons/
322
* https://materialdesignicons.com/
323
* https://fontawesome.com/v5/search
324
325
h3. Buttons
326
327
* Label button with action for usability/to reduce ambiguity (avoid generic button labels for actions)
328
* Buttons vs Links
329
** Buttons should cause change in current context
330
** Links should navigate to a different content or a new resource (e.g. different page)
331
* If text on button - color contract should be 4.5 :1 between button and text
332
* Button color and background color contrast should be 3:1
333
334
h3. Arvados Specific Components
335
336
Use chips for displaying tokenized values/arrays
337
338
h3. References
339
340
"WCAG2.1":https://www.w3.org/WAI/WCAG21/Understanding/
341
342
"Sarah’s talk for references":https://docs.google.com/presentation/d/1HNrhvK7zVZ7jgH3ELbX7KB97SdXCZXrvov_I4Oe1l2c/edit?usp=sharing