• 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.
119Double quotes 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```python
141def foo(param1, param2):
142    """
143    Summary line.
144
145    Long description of method foo.
146
147    @param param1: A Spam object that has methods bar() and baz()
148            which raise SpamError if something goes awry.
149    @type param1: Spam
150
151    @return: a list of integers describing changes in a source tree
152    @rtype: list
153
154    @raise SpamError: when some stated condition occurs.
155
156    """
157```
158
159The tags that you can put inside your docstring are tags recognized by systems
160like epydoc. Not all places need all tags defined, so choose them wisely while
161writing code. Generally always list any parameters, the return value (if there
162is one), and exceptions that can be raised.
163
164|   Tag    | Description
165|----------|-------------------------------------------------------------------
166| @author  | Code author
167| @cvar    | Class variable (defined on `self.__class__.`)
168| @ivar    | Instance variable for a class (defined on `self.`)
169| @param   | Parameter description
170| @raise   | Exception type the function can throw, and the conditions for it
171| @return  | Return value description
172| @rtype   | The type the method returns
173| @see     | Reference to other parts of the codebase
174| @type    | The type of a given parameter or variable
175| @warning | Call attention to potential problems with the code
176| @version | Version string
177
178For additional information and fields, refer to the epydoc manual:
179http://epydoc.sourceforge.net/manual-fields.html
180
181## Simple code
182
183Keep it simple; this is not the right place to show how smart you are. We
184have plenty of system failures to deal with without having to spend ages
185figuring out your code, thanks ;-) Readbility, readability, readability.
186Strongly prefer readability and simplicity over compactness.
187
188"Debugging is twice as hard as writing the code in the first place. Therefore,
189if you write the code as cleverly as possible, you are, by definition, not
190smart enough to debug it."  Brian Kernighan
191
192
193## Function length
194
195Please keep functions short, under 30 lines or so if possible. Even though
196you are amazingly clever, and can cope with it, the rest of us are busy and
197stupid, so be nice and help us out. To quote the Linux Kernel coding style:
198
199Functions should be short and sweet, and do just one thing.  They should
200fit on one or two screenfuls of text (the ISO/ANSI screen size is 80x24,
201as we all know), and do one thing and do that well.
202
203
204## Exceptions
205
206When raising exceptions, the preferred syntax for it is:
207
208```
209raise FooException('Exception Message')
210```
211
212Please don't raise string exceptions, as they're deprecated and will be removed
213from future versions of python. If you're in doubt about what type of exception
214you will raise, please look at http://docs.python.org/lib/module-exceptions.html
215and client/common\_lib/error.py, the former is a list of python built in
216exceptions and the later is a list of autotest/autoserv internal exceptions. Of
217course, if you really need to, you can extend the exception definitions on
218client/common\_lib/error.py.
219
220
221## Submitting patches
222
223Submit changes through the Chrome OS gerrit instance.  This process is
224documented on
225[chromium.org](http://dev.chromium.org/developers/contributing-code).
226