• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Coding style for autotest in Chrome OS / Android / Brillo
2These rules elaborate on, but rarely deviate from PEP-8.  When in doubt, go
3with PEP-8.
4
5
6## Language
7 * Use Python where possible
8 * Prefer writing more Python to a smaller amount of shell script in host
9   commands.  In practice, the Python tends to be easier to maintain.
10 * Some tests use C/C++ in test dependencies, and this is also ok.
11
12
13## Indentation & whitespace
14
15Format your code for an 80 character wide screen.
16
17Indentation is 4 spaces, as opposed to hard tabs (which it used to be).
18This is the Python standard.
19
20For hanging indentation, use 8 spaces plus all args should be on the new line.
21
22```
23
24     # Either of the following hanging indentation is considered acceptable.
25YES: return 'class: %s, host: %s, args = %s' % (
26             self.__class__.__name__, self.hostname, self.args)
27
28YES: return 'class: %s, host: %s, args = %s' % (
29             self.__class__.__name__,
30             self.hostname,
31             self.args)
32
33     # Do not use 4 spaces for hanging indentation
34NO:  return 'class: %s, host: %s, args = %s' % (
35         self.__class__.__name__, self.hostname, self.args)
36
37     # Do put all args on new line
38NO:  return 'class: %s, host: %s, args = %s' % (self.__class__.__name__,
39             self.hostname, self.args)
40```
41
42Don't leave trailing whitespace, or put whitespace on blank lines.
43
44
45## Variable names and UpPeR cAsE
46 * Use descriptive variable names where possible
47 * Use `variable_names_like_this`
48 * Use `method_and_function_names_like_this`
49 * Use `UpperCamelCase` for class names
50
51
52## Importing modules
53
54The order of imports should be as follows:
55
56 * Standard python modules
57 * Non-standard python modules
58 * Autotest modules
59
60Within one of these three sections, all module imports using the from
61keyword should appear after regular imports.
62Each module should be imported on its own line.
63Do not use Wildcard imports (`from x import *`) if possible.
64
65Import modules, not classes.  For example:
66
67```
68from common_lib import error
69
70def foo():
71    raise error.AutoservError(...)
72```
73
74and not:
75
76```
77from common_lib.error import AutoservError
78```
79
80For example:
81
82```
83import os
84import pickle
85import random
86import re
87import select
88import shutil
89import signal
90import subprocess
91
92import common   # Magic autotest_lib module and sys.path setup code.
93import MySQLdb  # After common so that we check our site-packages first.
94
95from common_lib import error
96```
97
98## Testing None
99
100Use `is None` rather than `== None` and `is not None` rather than `!= None`.
101This way you'll never run into a case where someone's `__eq__` or `__ne__`
102method does the wrong thing.
103
104
105## Comments
106
107Generally, you want your comments to tell WHAT your code does, not HOW.
108We can figure out how from the code itself (or if not, your code needs fixing).
109
110Try to describle the intent of a function and what it does in a triple-quoted
111(multiline) string just after the def line. We've tried to do that in most
112places, though undoubtedly we're not perfect. A high level overview is
113incredibly helpful in understanding code.
114
115
116## Hardcoded String Formatting
117
118Strings should use only single quotes for hardcoded strings in the code. Double
119quotes are acceptable when single quote is used as part of the string.
120Multiline strings should not use '\' but wrap the string using parentheseses.
121
122```
123REALLY_LONG_STRING = ('This is supposed to be a really long string that is '
124                      'over 80 characters and does not use a slash to '
125                      'continue.')
126```
127
128## Docstrings
129
130Docstrings are important to keep our code self documenting. While it's not
131necessary to overdo documentation, we ask you to be sensible and document any
132nontrivial function. When creating docstrings, please add a newline at the
133beginning of your triple quoted string and another newline at the end of it. If
134the docstring has multiple lines, please include a short summary line followed
135by a blank line before continuing with the rest of the description. Please
136capitalize and punctuate accordingly the sentences. If the description has
137multiple lines, put two levels of indentation before proceeding with text. An
138example docstring:
139
140```
141def foo(param1, param2):
142    """
143    Summary line.
144
145    Long description of method foo.
146
147    @param param1: A thing called param1 that is used for a bunch of stuff
148            that has methods bar() and baz() which raise SpamError if
149            something goes awry.
150
151    @returns a list of integers describing changes in a source tree
152
153    @raises exception that could be raised if a certain condition occurs.
154
155    """
156```
157
158The tags that you can put inside your docstring are tags recognized by systems
159like doxygen. Not all places need all tags defined, so choose them wisely while
160writing code. Generally (if applicable) always list parameters, return value
161(if there is one), and exceptions that can be raised to each docstring.
162
163|   Tag    | Description                                                                              |
164|----------|------------------------------------------------------------------------------------------|
165| @author  | Code author                                                                              |
166| @param   | Parameter description                                                                    |
167| @raise   | If the function can throw an exception, this tag documents the possible exception types. |
168| @raises  | Same as @raise.                                                                          |
169| @return  | Return value description                                                                 |
170| @returns | Same as @return                                                                          |
171| @see     | Reference to other parts of the codebase.                                                |
172| @warning | Call attention to potential problems with the code                                       |
173| @var     | Documentation for a variable or enum value (either global or as a member of a class)     |
174| @version | Version string                                                                           |
175
176When in doubt refer to: http://doxygen.nl/commands.html
177
178## Simple code
179
180Keep it simple; this is not the right place to show how smart you are. We
181have plenty of system failures to deal with without having to spend ages
182figuring out your code, thanks ;-) Readbility, readability, readability.
183Strongly prefer readability and simplicity over compactness.
184
185"Debugging is twice as hard as writing the code in the first place. Therefore,
186if you write the code as cleverly as possible, you are, by definition, not
187smart enough to debug it."  Brian Kernighan
188
189
190## Function length
191
192Please keep functions short, under 30 lines or so if possible. Even though
193you are amazingly clever, and can cope with it, the rest of us are busy and
194stupid, so be nice and help us out. To quote the Linux Kernel coding style:
195
196Functions should be short and sweet, and do just one thing.  They should
197fit on one or two screenfuls of text (the ISO/ANSI screen size is 80x24,
198as we all know), and do one thing and do that well.
199
200
201## Exceptions
202
203When raising exceptions, the preferred syntax for it is:
204
205```
206raise FooException('Exception Message')
207```
208
209Please don't raise string exceptions, as they're deprecated and will be removed
210from future versions of python. If you're in doubt about what type of exception
211you will raise, please look at http://docs.python.org/lib/module-exceptions.html
212and client/common\_lib/error.py, the former is a list of python built in
213exceptions and the later is a list of autotest/autoserv internal exceptions. Of
214course, if you really need to, you can extend the exception definitions on
215client/common\_lib/error.py.
216
217
218## Submitting patches
219
220Submit changes through the Chrome OS gerrit instance.  This process is
221documented on
222[chromium.org](http://dev.chromium.org/developers/contributing-code).
223