blob: c10ccc466624a602a5a004c1be12deaa0d881f88 [file] [log] [blame] [view]
Christopher Wileyfe2ea872016-02-02 12:12:18 -08001# Coding style for autotest in Chrome OS / Android / Brillo
Christopher Wiley2f011d92016-02-04 09:35:32 -08002These rules elaborate on, but rarely deviate from PEP-8. When in doubt, go
3with PEP-8.
mbligh71d338a2007-10-08 05:05:50 +00004
5
Christopher Wileyfe2ea872016-02-02 12:12:18 -08006## Language
Christopher Wiley2f011d92016-02-04 09:35:32 -08007 * 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.
mbligh2ac475b2008-09-09 21:37:40 +000011
12
Christopher Wileyfe2ea872016-02-02 12:12:18 -080013## Indentation & whitespace
mbligh71d338a2007-10-08 05:05:50 +000014
15Format your code for an 80 character wide screen.
16
Christopher Wiley2f011d92016-02-04 09:35:32 -080017Indentation is 4 spaces, as opposed to hard tabs (which it used to be).
mblighc960fcf2008-06-18 19:58:57 +000018This is the Python standard.
mbligh71d338a2007-10-08 05:05:50 +000019
Tan Gao8aac17b2013-04-16 08:46:01 -070020For hanging indentation, use 8 spaces plus all args should be on the new line.
21
Christopher Wileyfe2ea872016-02-02 12:12:18 -080022```
Christopher Wiley2f011d92016-02-04 09:35:32 -080023
Tan Gao8aac17b2013-04-16 08:46:01 -070024 # 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)
Christopher Wileyfe2ea872016-02-02 12:12:18 -080040```
Tan Gao8aac17b2013-04-16 08:46:01 -070041
mbligh71d338a2007-10-08 05:05:50 +000042Don't leave trailing whitespace, or put whitespace on blank lines.
Simran Basic7330bd2013-05-31 09:23:50 -070043
mbligh71d338a2007-10-08 05:05:50 +000044
Christopher Wileyfe2ea872016-02-02 12:12:18 -080045## Variable names and UpPeR cAsE
Christopher Wiley2f011d92016-02-04 09:35:32 -080046 * 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
mbligh71d338a2007-10-08 05:05:50 +000050
mblighd876f452008-12-03 15:09:17 +000051
Christopher Wileyfe2ea872016-02-02 12:12:18 -080052## Importing modules
mbligh7654c822008-04-04 15:12:48 +000053
54The order of imports should be as follows:
55
Christopher Wiley2f011d92016-02-04 09:35:32 -080056 * Standard python modules
57 * Non-standard python modules
58 * Autotest modules
mbligh7654c822008-04-04 15:12:48 +000059
60Within one of these three sections, all module imports using the from
61keyword should appear after regular imports.
Christopher Wileyd7445ef2014-12-05 13:26:05 -080062Each module should be imported on its own line.
Christopher Wiley2f011d92016-02-04 09:35:32 -080063Do not use Wildcard imports (`from x import *`) if possible.
64
65Import modules, not classes. For example:
Christopher Wileyfe2ea872016-02-02 12:12:18 -080066
67```
mbligh7654c822008-04-04 15:12:48 +000068from common_lib import error
Christopher Wiley2f011d92016-02-04 09:35:32 -080069
70def foo():
71 raise error.AutoservError(...)
Christopher Wileyfe2ea872016-02-02 12:12:18 -080072```
73
74and not:
75
76```
mbligh7654c822008-04-04 15:12:48 +000077from common_lib.error import AutoservError
Christopher Wileyfe2ea872016-02-02 12:12:18 -080078```
mbligh7654c822008-04-04 15:12:48 +000079
80For example:
Christopher Wileyfe2ea872016-02-02 12:12:18 -080081
82```
Christopher Wileyd7445ef2014-12-05 13:26:05 -080083import os
84import pickle
85import random
86import re
87import select
88import shutil
89import signal
90import subprocess
91
mbligh8bcd23a2009-02-03 19:14:06 +000092import common # Magic autotest_lib module and sys.path setup code.
93import MySQLdb # After common so that we check our site-packages first.
Christopher Wileyd7445ef2014-12-05 13:26:05 -080094
mbligh7654c822008-04-04 15:12:48 +000095from common_lib import error
Christopher Wileyfe2ea872016-02-02 12:12:18 -080096```
mbligh7654c822008-04-04 15:12:48 +000097
Christopher Wileyfe2ea872016-02-02 12:12:18 -080098## Testing None
mblighd876f452008-12-03 15:09:17 +000099
Christopher Wiley2f011d92016-02-04 09:35:32 -0800100Use `is None` rather than `== None` and `is not None` rather than `!= None`.
Christopher Wileyfe2ea872016-02-02 12:12:18 -0800101This way you'll never run into a case where someone's `__eq__` or `__ne__`
Christopher Wiley2f011d92016-02-04 09:35:32 -0800102method does the wrong thing.
mblighd876f452008-12-03 15:09:17 +0000103
mbligh71d338a2007-10-08 05:05:50 +0000104
Christopher Wileyfe2ea872016-02-02 12:12:18 -0800105## Comments
mbligh71d338a2007-10-08 05:05:50 +0000106
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
mbligh52207362009-09-03 20:54:29 +0000112places, though undoubtedly we're not perfect. A high level overview is
mbligh71d338a2007-10-08 05:05:50 +0000113incredibly helpful in understanding code.
114
115
Christopher Wileyfe2ea872016-02-02 12:12:18 -0800116## Hardcoded String Formatting
Simran Basic7330bd2013-05-31 09:23:50 -0700117
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.
Christopher Wiley2f011d92016-02-04 09:35:32 -0800120Multiline strings should not use '\' but wrap the string using parentheseses.
Simran Basic7330bd2013-05-31 09:23:50 -0700121
Christopher Wileyfe2ea872016-02-02 12:12:18 -0800122```
Simran Basic7330bd2013-05-31 09:23:50 -0700123REALLY_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.')
Christopher Wileyfe2ea872016-02-02 12:12:18 -0800126```
Simran Basic7330bd2013-05-31 09:23:50 -0700127
Christopher Wileyfe2ea872016-02-02 12:12:18 -0800128## Docstrings
mbligh5cad50f2009-06-08 16:50:51 +0000129
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
showard25f056a2009-11-23 20:22:50 +0000133beginning 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:
mbligh5cad50f2009-06-08 16:50:51 +0000139
Christopher Wileyfe2ea872016-02-02 12:12:18 -0800140```
mbligh5cad50f2009-06-08 16:50:51 +0000141def foo(param1, param2):
mbligh52207362009-09-03 20:54:29 +0000142 """
showard25f056a2009-11-23 20:22:50 +0000143 Summary line.
144
mbligh52207362009-09-03 20:54:29 +0000145 Long description of method foo.
mbligh5cad50f2009-06-08 16:50:51 +0000146
mbligh52207362009-09-03 20:54:29 +0000147 @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.
Simran Basic7330bd2013-05-31 09:23:50 -0700150
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
mbligh52207362009-09-03 20:54:29 +0000155 """
Christopher Wileyfe2ea872016-02-02 12:12:18 -0800156```
mbligh5cad50f2009-06-08 16:50:51 +0000157
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
Simran Basic7330bd2013-05-31 09:23:50 -0700160writing code. Generally (if applicable) always list parameters, return value
161(if there is one), and exceptions that can be raised to each docstring.
mbligh5cad50f2009-06-08 16:50:51 +0000162
Christopher Wiley2f011d92016-02-04 09:35:32 -0800163| 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 |
mbligh5cad50f2009-06-08 16:50:51 +0000175
mbligh3bdba922010-05-03 18:02:54 +0000176When in doubt refer to: http://doxygen.nl/commands.html
177
Christopher Wileyfe2ea872016-02-02 12:12:18 -0800178## Simple code
mbligh71d338a2007-10-08 05:05:50 +0000179
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.
Christopher Wiley2f011d92016-02-04 09:35:32 -0800183Strongly prefer readability and simplicity over compactness.
mbligh71d338a2007-10-08 05:05:50 +0000184
185"Debugging is twice as hard as writing the code in the first place. Therefore,
mbligh52207362009-09-03 20:54:29 +0000186if you write the code as cleverly as possible, you are, by definition, not
mbligh71d338a2007-10-08 05:05:50 +0000187smart enough to debug it." Brian Kernighan
188
189
Christopher Wileyfe2ea872016-02-02 12:12:18 -0800190## Function length
mbligh71d338a2007-10-08 05:05:50 +0000191
192Please keep functions short, under 30 lines or so if possible. Even though
Christopher Wiley2f011d92016-02-04 09:35:32 -0800193you 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:
mbligh71d338a2007-10-08 05:05:50 +0000195
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
Christopher Wileyfe2ea872016-02-02 12:12:18 -0800201## Exceptions
mbligh900b6c12008-01-14 16:56:47 +0000202
203When raising exceptions, the preferred syntax for it is:
204
Christopher Wileyfe2ea872016-02-02 12:12:18 -0800205```
mbligh900b6c12008-01-14 16:56:47 +0000206raise FooException('Exception Message')
Christopher Wileyfe2ea872016-02-02 12:12:18 -0800207```
mbligh900b6c12008-01-14 16:56:47 +0000208
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
Christopher Wileyfe2ea872016-02-02 12:12:18 -0800212and client/common\_lib/error.py, the former is a list of python built in
mbligh900b6c12008-01-14 16:56:47 +0000213exceptions 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
Christopher Wileyfe2ea872016-02-02 12:12:18 -0800215client/common\_lib/error.py.
mbligh900b6c12008-01-14 16:56:47 +0000216
217
Christopher Wileyfe2ea872016-02-02 12:12:18 -0800218## Submitting patches
mbligh71d338a2007-10-08 05:05:50 +0000219
Christopher Wiley2f011d92016-02-04 09:35:32 -0800220Submit changes through the Chrome OS gerrit instance. This process is
221documented on
222[chromium.org](http://dev.chromium.org/developers/contributing-code).