blob: e74d5630fb25d4e6ad8762d301af62ba804ca479 [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 +000044Leave TWO blank lines between functions - this is Python, there are no clear
Christopher Wiley2f011d92016-02-04 09:35:32 -080045function end markers, and we need help. Note that this intentionally
46contradicts PEP-8.
mbligh71d338a2007-10-08 05:05:50 +000047
48
Christopher Wileyfe2ea872016-02-02 12:12:18 -080049## Variable names and UpPeR cAsE
Christopher Wiley2f011d92016-02-04 09:35:32 -080050 * Use descriptive variable names where possible
51 * Use `variable_names_like_this`
52 * Use `method_and_function_names_like_this`
53 * Use `UpperCamelCase` for class names
mbligh71d338a2007-10-08 05:05:50 +000054
mblighd876f452008-12-03 15:09:17 +000055
Christopher Wileyfe2ea872016-02-02 12:12:18 -080056## Importing modules
mbligh7654c822008-04-04 15:12:48 +000057
58The order of imports should be as follows:
59
Christopher Wiley2f011d92016-02-04 09:35:32 -080060 * Standard python modules
61 * Non-standard python modules
62 * Autotest modules
mbligh7654c822008-04-04 15:12:48 +000063
64Within one of these three sections, all module imports using the from
65keyword should appear after regular imports.
Christopher Wileyd7445ef2014-12-05 13:26:05 -080066Each module should be imported on its own line.
Christopher Wiley2f011d92016-02-04 09:35:32 -080067Do not use Wildcard imports (`from x import *`) if possible.
68
69Import modules, not classes. For example:
Christopher Wileyfe2ea872016-02-02 12:12:18 -080070
71```
mbligh7654c822008-04-04 15:12:48 +000072from common_lib import error
Christopher Wiley2f011d92016-02-04 09:35:32 -080073
74def foo():
75 raise error.AutoservError(...)
Christopher Wileyfe2ea872016-02-02 12:12:18 -080076```
77
78and not:
79
80```
mbligh7654c822008-04-04 15:12:48 +000081from common_lib.error import AutoservError
Christopher Wileyfe2ea872016-02-02 12:12:18 -080082```
mbligh7654c822008-04-04 15:12:48 +000083
84For example:
Christopher Wileyfe2ea872016-02-02 12:12:18 -080085
86```
Christopher Wileyd7445ef2014-12-05 13:26:05 -080087import os
88import pickle
89import random
90import re
91import select
92import shutil
93import signal
94import subprocess
95
mbligh8bcd23a2009-02-03 19:14:06 +000096import common # Magic autotest_lib module and sys.path setup code.
97import MySQLdb # After common so that we check our site-packages first.
Christopher Wileyd7445ef2014-12-05 13:26:05 -080098
mbligh7654c822008-04-04 15:12:48 +000099from common_lib import error
Christopher Wileyfe2ea872016-02-02 12:12:18 -0800100```
mbligh7654c822008-04-04 15:12:48 +0000101
Christopher Wileyfe2ea872016-02-02 12:12:18 -0800102## Testing None
mblighd876f452008-12-03 15:09:17 +0000103
Christopher Wiley2f011d92016-02-04 09:35:32 -0800104Use `is None` rather than `== None` and `is not None` rather than `!= None`.
Christopher Wileyfe2ea872016-02-02 12:12:18 -0800105This way you'll never run into a case where someone's `__eq__` or `__ne__`
Christopher Wiley2f011d92016-02-04 09:35:32 -0800106method does the wrong thing.
mblighd876f452008-12-03 15:09:17 +0000107
mbligh71d338a2007-10-08 05:05:50 +0000108
Christopher Wileyfe2ea872016-02-02 12:12:18 -0800109## Comments
mbligh71d338a2007-10-08 05:05:50 +0000110
111Generally, you want your comments to tell WHAT your code does, not HOW.
112We can figure out how from the code itself (or if not, your code needs fixing).
113
114Try to describle the intent of a function and what it does in a triple-quoted
115(multiline) string just after the def line. We've tried to do that in most
mbligh52207362009-09-03 20:54:29 +0000116places, though undoubtedly we're not perfect. A high level overview is
mbligh71d338a2007-10-08 05:05:50 +0000117incredibly helpful in understanding code.
118
119
Christopher Wileyfe2ea872016-02-02 12:12:18 -0800120## Hardcoded String Formatting
Simran Basic7330bd2013-05-31 09:23:50 -0700121
122Strings should use only single quotes for hardcoded strings in the code. Double
123quotes are acceptable when single quote is used as part of the string.
Christopher Wiley2f011d92016-02-04 09:35:32 -0800124Multiline strings should not use '\' but wrap the string using parentheseses.
Simran Basic7330bd2013-05-31 09:23:50 -0700125
Christopher Wileyfe2ea872016-02-02 12:12:18 -0800126```
Simran Basic7330bd2013-05-31 09:23:50 -0700127REALLY_LONG_STRING = ('This is supposed to be a really long string that is '
128 'over 80 characters and does not use a slash to '
129 'continue.')
Christopher Wileyfe2ea872016-02-02 12:12:18 -0800130```
Simran Basic7330bd2013-05-31 09:23:50 -0700131
Christopher Wileyfe2ea872016-02-02 12:12:18 -0800132## Docstrings
mbligh5cad50f2009-06-08 16:50:51 +0000133
134Docstrings are important to keep our code self documenting. While it's not
135necessary to overdo documentation, we ask you to be sensible and document any
136nontrivial function. When creating docstrings, please add a newline at the
showard25f056a2009-11-23 20:22:50 +0000137beginning of your triple quoted string and another newline at the end of it. If
138the docstring has multiple lines, please include a short summary line followed
139by a blank line before continuing with the rest of the description. Please
140capitalize and punctuate accordingly the sentences. If the description has
141multiple lines, put two levels of indentation before proceeding with text. An
142example docstring:
mbligh5cad50f2009-06-08 16:50:51 +0000143
Christopher Wileyfe2ea872016-02-02 12:12:18 -0800144```
mbligh5cad50f2009-06-08 16:50:51 +0000145def foo(param1, param2):
mbligh52207362009-09-03 20:54:29 +0000146 """
showard25f056a2009-11-23 20:22:50 +0000147 Summary line.
148
mbligh52207362009-09-03 20:54:29 +0000149 Long description of method foo.
mbligh5cad50f2009-06-08 16:50:51 +0000150
mbligh52207362009-09-03 20:54:29 +0000151 @param param1: A thing called param1 that is used for a bunch of stuff
152 that has methods bar() and baz() which raise SpamError if
153 something goes awry.
Simran Basic7330bd2013-05-31 09:23:50 -0700154
155 @returns a list of integers describing changes in a source tree
156
157 @raises exception that could be raised if a certain condition occurs.
158
mbligh52207362009-09-03 20:54:29 +0000159 """
Christopher Wileyfe2ea872016-02-02 12:12:18 -0800160```
mbligh5cad50f2009-06-08 16:50:51 +0000161
162The tags that you can put inside your docstring are tags recognized by systems
163like doxygen. Not all places need all tags defined, so choose them wisely while
Simran Basic7330bd2013-05-31 09:23:50 -0700164writing code. Generally (if applicable) always list parameters, return value
165(if there is one), and exceptions that can be raised to each docstring.
mbligh5cad50f2009-06-08 16:50:51 +0000166
Christopher Wiley2f011d92016-02-04 09:35:32 -0800167| Tag | Description |
168|----------|------------------------------------------------------------------------------------------|
169| @author | Code author |
170| @param | Parameter description |
171| @raise | If the function can throw an exception, this tag documents the possible exception types. |
172| @raises | Same as @raise. |
173| @return | Return value description |
174| @returns | Same as @return |
175| @see | Reference to other parts of the codebase. |
176| @warning | Call attention to potential problems with the code |
177| @var | Documentation for a variable or enum value (either global or as a member of a class) |
178| @version | Version string |
mbligh5cad50f2009-06-08 16:50:51 +0000179
mbligh3bdba922010-05-03 18:02:54 +0000180When in doubt refer to: http://doxygen.nl/commands.html
181
Christopher Wileyfe2ea872016-02-02 12:12:18 -0800182## Simple code
mbligh71d338a2007-10-08 05:05:50 +0000183
184Keep it simple; this is not the right place to show how smart you are. We
185have plenty of system failures to deal with without having to spend ages
186figuring out your code, thanks ;-) Readbility, readability, readability.
Christopher Wiley2f011d92016-02-04 09:35:32 -0800187Strongly prefer readability and simplicity over compactness.
mbligh71d338a2007-10-08 05:05:50 +0000188
189"Debugging is twice as hard as writing the code in the first place. Therefore,
mbligh52207362009-09-03 20:54:29 +0000190if you write the code as cleverly as possible, you are, by definition, not
mbligh71d338a2007-10-08 05:05:50 +0000191smart enough to debug it." Brian Kernighan
192
193
Christopher Wileyfe2ea872016-02-02 12:12:18 -0800194## Function length
mbligh71d338a2007-10-08 05:05:50 +0000195
196Please keep functions short, under 30 lines or so if possible. Even though
Christopher Wiley2f011d92016-02-04 09:35:32 -0800197you are amazingly clever, and can cope with it, the rest of us are busy and
198stupid, so be nice and help us out. To quote the Linux Kernel coding style:
mbligh71d338a2007-10-08 05:05:50 +0000199
200Functions should be short and sweet, and do just one thing. They should
201fit on one or two screenfuls of text (the ISO/ANSI screen size is 80x24,
202as we all know), and do one thing and do that well.
203
204
Christopher Wileyfe2ea872016-02-02 12:12:18 -0800205## Exceptions
mbligh900b6c12008-01-14 16:56:47 +0000206
207When raising exceptions, the preferred syntax for it is:
208
Christopher Wileyfe2ea872016-02-02 12:12:18 -0800209```
mbligh900b6c12008-01-14 16:56:47 +0000210raise FooException('Exception Message')
Christopher Wileyfe2ea872016-02-02 12:12:18 -0800211```
mbligh900b6c12008-01-14 16:56:47 +0000212
213Please don't raise string exceptions, as they're deprecated and will be removed
214from future versions of python. If you're in doubt about what type of exception
215you will raise, please look at http://docs.python.org/lib/module-exceptions.html
Christopher Wileyfe2ea872016-02-02 12:12:18 -0800216and client/common\_lib/error.py, the former is a list of python built in
mbligh900b6c12008-01-14 16:56:47 +0000217exceptions and the later is a list of autotest/autoserv internal exceptions. Of
218course, if you really need to, you can extend the exception definitions on
Christopher Wileyfe2ea872016-02-02 12:12:18 -0800219client/common\_lib/error.py.
mbligh900b6c12008-01-14 16:56:47 +0000220
221
Christopher Wileyfe2ea872016-02-02 12:12:18 -0800222## Submitting patches
mbligh71d338a2007-10-08 05:05:50 +0000223
Christopher Wiley2f011d92016-02-04 09:35:32 -0800224Submit changes through the Chrome OS gerrit instance. This process is
225documented on
226[chromium.org](http://dev.chromium.org/developers/contributing-code).