blob: 2d778cfbc085053f0290d3a000f4c5ffa99bca7d [file] [log] [blame] [view]
Christopher Wileyfe2ea872016-02-02 12:12:18 -08001# Coding style for autotest in Chrome OS / Android / Brillo
mbligh71d338a2007-10-08 05:05:50 +00002These rules are fairly standard and boring. People will bitch about something
3in here, no doubt. Get over it. Much of this was stolen from the Linux Kernel
4coding style, because most of it makes good sense. If you disagree, that's OK,
5but please stick to the rules anyway ;-)
6
7
Christopher Wileyfe2ea872016-02-02 12:12:18 -08008## Language
mbligh71d338a2007-10-08 05:05:50 +00009
10Please use Python where possible. It's not the ideal language for everything,
11but it's pretty good, and consistency goes a long way in making the project
12maintainable. (Obviously using C or whatever for writing tests is fine).
13
14
Christopher Wileyfe2ea872016-02-02 12:12:18 -080015## Base coding style
mbligh2ac475b2008-09-09 21:37:40 +000016
mbligh52207362009-09-03 20:54:29 +000017When writing python code, unless otherwise stated, stick to the python style
mbligh2ac475b2008-09-09 21:37:40 +000018guide (http://www.python.org/dev/peps/pep-0008/).
19
20
Christopher Wileyfe2ea872016-02-02 12:12:18 -080021## Indentation & whitespace
mbligh71d338a2007-10-08 05:05:50 +000022
23Format your code for an 80 character wide screen.
24
mblighc960fcf2008-06-18 19:58:57 +000025Indentation is now 4 spaces, as opposed to hard tabs (which it used to be).
26This is the Python standard.
mbligh71d338a2007-10-08 05:05:50 +000027
Tan Gao8aac17b2013-04-16 08:46:01 -070028For hanging indentation, use 8 spaces plus all args should be on the new line.
29
Christopher Wileyfe2ea872016-02-02 12:12:18 -080030```
Tan Gao8aac17b2013-04-16 08:46:01 -070031 # Either of the following hanging indentation is considered acceptable.
32YES: return 'class: %s, host: %s, args = %s' % (
33 self.__class__.__name__, self.hostname, self.args)
34
35YES: return 'class: %s, host: %s, args = %s' % (
36 self.__class__.__name__,
37 self.hostname,
38 self.args)
39
40 # Do not use 4 spaces for hanging indentation
41NO: return 'class: %s, host: %s, args = %s' % (
42 self.__class__.__name__, self.hostname, self.args)
43
44 # Do put all args on new line
45NO: return 'class: %s, host: %s, args = %s' % (self.__class__.__name__,
46 self.hostname, self.args)
Christopher Wileyfe2ea872016-02-02 12:12:18 -080047```
Tan Gao8aac17b2013-04-16 08:46:01 -070048
mbligh71d338a2007-10-08 05:05:50 +000049Don't leave trailing whitespace, or put whitespace on blank lines.
Simran Basic7330bd2013-05-31 09:23:50 -070050
mbligh71d338a2007-10-08 05:05:50 +000051Leave TWO blank lines between functions - this is Python, there are no clear
52function end markers, and we need help.
53
54
Christopher Wileyfe2ea872016-02-02 12:12:18 -080055## Variable names and UpPeR cAsE
mbligh71d338a2007-10-08 05:05:50 +000056
mbligh52207362009-09-03 20:54:29 +000057Use descriptive variable names where possible - it helps to make the code
mbligh71d338a2007-10-08 05:05:50 +000058self documenting.
59
60Don't use CamelCaps style in most places - use underscores to separate parts
Christopher Wileyfe2ea872016-02-02 12:12:18 -080061of your variable\_names please. I shall make a bedgrudging exception for class
mbligh71d338a2007-10-08 05:05:50 +000062names I suppose, but I'll still whine about it a lot.
63
mblighd876f452008-12-03 15:09:17 +000064
Christopher Wileyfe2ea872016-02-02 12:12:18 -080065## Importing modules
mbligh7654c822008-04-04 15:12:48 +000066
67The order of imports should be as follows:
68
69Standard python modules
70Non-standard python modules
71Autotest modules
72
73Within one of these three sections, all module imports using the from
74keyword should appear after regular imports.
Christopher Wileyd7445ef2014-12-05 13:26:05 -080075Each module should be imported on its own line.
Christopher Wileyfe2ea872016-02-02 12:12:18 -080076Wildcard imports (from x import \*) should be avoided if possible.
mbligh7654c822008-04-04 15:12:48 +000077Classes should not be imported from modules, but modules may be imported
78 from packages, i.e.:
Christopher Wileyfe2ea872016-02-02 12:12:18 -080079
80```
mbligh7654c822008-04-04 15:12:48 +000081from common_lib import error
Christopher Wileyfe2ea872016-02-02 12:12:18 -080082```
83
84and not:
85
86```
mbligh7654c822008-04-04 15:12:48 +000087from common_lib.error import AutoservError
Christopher Wileyfe2ea872016-02-02 12:12:18 -080088```
mbligh7654c822008-04-04 15:12:48 +000089
90For example:
Christopher Wileyfe2ea872016-02-02 12:12:18 -080091
92```
Christopher Wileyd7445ef2014-12-05 13:26:05 -080093import os
94import pickle
95import random
96import re
97import select
98import shutil
99import signal
100import subprocess
101
mbligh8bcd23a2009-02-03 19:14:06 +0000102import common # Magic autotest_lib module and sys.path setup code.
103import MySQLdb # After common so that we check our site-packages first.
Christopher Wileyd7445ef2014-12-05 13:26:05 -0800104
mbligh7654c822008-04-04 15:12:48 +0000105from common_lib import error
Christopher Wileyfe2ea872016-02-02 12:12:18 -0800106```
mbligh7654c822008-04-04 15:12:48 +0000107
Christopher Wileyfe2ea872016-02-02 12:12:18 -0800108## Testing None
mblighd876f452008-12-03 15:09:17 +0000109
110Use "is None" rather than "== None" and "is not None" rather than "!= None".
Christopher Wileyfe2ea872016-02-02 12:12:18 -0800111This way you'll never run into a case where someone's `__eq__` or `__ne__`
mblighd876f452008-12-03 15:09:17 +0000112method do the wrong thing
113
mbligh71d338a2007-10-08 05:05:50 +0000114
Christopher Wileyfe2ea872016-02-02 12:12:18 -0800115## Comments
mbligh71d338a2007-10-08 05:05:50 +0000116
117Generally, you want your comments to tell WHAT your code does, not HOW.
118We can figure out how from the code itself (or if not, your code needs fixing).
119
120Try to describle the intent of a function and what it does in a triple-quoted
121(multiline) string just after the def line. We've tried to do that in most
mbligh52207362009-09-03 20:54:29 +0000122places, though undoubtedly we're not perfect. A high level overview is
mbligh71d338a2007-10-08 05:05:50 +0000123incredibly helpful in understanding code.
124
125
Christopher Wileyfe2ea872016-02-02 12:12:18 -0800126## Hardcoded String Formatting
Simran Basic7330bd2013-05-31 09:23:50 -0700127
128Strings should use only single quotes for hardcoded strings in the code. Double
129quotes are acceptable when single quote is used as part of the string.
130Multiline string should not use '\' but wrap the string using parenthesises.
131
Christopher Wileyfe2ea872016-02-02 12:12:18 -0800132```
Simran Basic7330bd2013-05-31 09:23:50 -0700133REALLY_LONG_STRING = ('This is supposed to be a really long string that is '
134 'over 80 characters and does not use a slash to '
135 'continue.')
Christopher Wileyfe2ea872016-02-02 12:12:18 -0800136```
Simran Basic7330bd2013-05-31 09:23:50 -0700137
Christopher Wileyfe2ea872016-02-02 12:12:18 -0800138## Docstrings
mbligh5cad50f2009-06-08 16:50:51 +0000139
140Docstrings are important to keep our code self documenting. While it's not
141necessary to overdo documentation, we ask you to be sensible and document any
142nontrivial function. When creating docstrings, please add a newline at the
showard25f056a2009-11-23 20:22:50 +0000143beginning of your triple quoted string and another newline at the end of it. If
144the docstring has multiple lines, please include a short summary line followed
145by a blank line before continuing with the rest of the description. Please
146capitalize and punctuate accordingly the sentences. If the description has
147multiple lines, put two levels of indentation before proceeding with text. An
148example docstring:
mbligh5cad50f2009-06-08 16:50:51 +0000149
Christopher Wileyfe2ea872016-02-02 12:12:18 -0800150```
mbligh5cad50f2009-06-08 16:50:51 +0000151def foo(param1, param2):
mbligh52207362009-09-03 20:54:29 +0000152 """
showard25f056a2009-11-23 20:22:50 +0000153 Summary line.
154
mbligh52207362009-09-03 20:54:29 +0000155 Long description of method foo.
mbligh5cad50f2009-06-08 16:50:51 +0000156
mbligh52207362009-09-03 20:54:29 +0000157 @param param1: A thing called param1 that is used for a bunch of stuff
158 that has methods bar() and baz() which raise SpamError if
159 something goes awry.
Simran Basic7330bd2013-05-31 09:23:50 -0700160
161 @returns a list of integers describing changes in a source tree
162
163 @raises exception that could be raised if a certain condition occurs.
164
mbligh52207362009-09-03 20:54:29 +0000165 """
Christopher Wileyfe2ea872016-02-02 12:12:18 -0800166```
mbligh5cad50f2009-06-08 16:50:51 +0000167
168The tags that you can put inside your docstring are tags recognized by systems
169like doxygen. Not all places need all tags defined, so choose them wisely while
Simran Basic7330bd2013-05-31 09:23:50 -0700170writing code. Generally (if applicable) always list parameters, return value
171(if there is one), and exceptions that can be raised to each docstring.
mbligh5cad50f2009-06-08 16:50:51 +0000172
173@author - Code author
174@param - Parameter description
Simran Basic7330bd2013-05-31 09:23:50 -0700175@raise - If the function can throw an exception, this tag documents the
mbligh5cad50f2009-06-08 16:50:51 +0000176possible exception types.
Simran Basic7330bd2013-05-31 09:23:50 -0700177@raises - same as @raise.
178@return - Return value description
179@returns - Same as @return
180@see - Reference to what you have done
181@warning - Call attention to potential problems with the code
182@var - Documentation for a variable or enum value (either global or as a
183member of a class)
184@version - Version string
mbligh5cad50f2009-06-08 16:50:51 +0000185
mbligh3bdba922010-05-03 18:02:54 +0000186When in doubt refer to: http://doxygen.nl/commands.html
187
Christopher Wileyfe2ea872016-02-02 12:12:18 -0800188## Simple code
mbligh71d338a2007-10-08 05:05:50 +0000189
190Keep it simple; this is not the right place to show how smart you are. We
191have plenty of system failures to deal with without having to spend ages
192figuring out your code, thanks ;-) Readbility, readability, readability.
mbligh52207362009-09-03 20:54:29 +0000193I really don't care if other things are more compact.
mbligh71d338a2007-10-08 05:05:50 +0000194
195"Debugging is twice as hard as writing the code in the first place. Therefore,
mbligh52207362009-09-03 20:54:29 +0000196if you write the code as cleverly as possible, you are, by definition, not
mbligh71d338a2007-10-08 05:05:50 +0000197smart enough to debug it." Brian Kernighan
198
199
Christopher Wileyfe2ea872016-02-02 12:12:18 -0800200## Function length
mbligh71d338a2007-10-08 05:05:50 +0000201
202Please keep functions short, under 30 lines or so if possible. Even though
203you are amazingly clever, and can cope with it, the rest of us are all stupid,
204so be nice and help us out. To quote the Linux Kernel coding style:
205
206Functions should be short and sweet, and do just one thing. They should
207fit on one or two screenfuls of text (the ISO/ANSI screen size is 80x24,
208as we all know), and do one thing and do that well.
209
210
Christopher Wileyfe2ea872016-02-02 12:12:18 -0800211## Exceptions
mbligh900b6c12008-01-14 16:56:47 +0000212
213When raising exceptions, the preferred syntax for it is:
214
Christopher Wileyfe2ea872016-02-02 12:12:18 -0800215```
mbligh900b6c12008-01-14 16:56:47 +0000216raise FooException('Exception Message')
Christopher Wileyfe2ea872016-02-02 12:12:18 -0800217```
mbligh900b6c12008-01-14 16:56:47 +0000218
219Please don't raise string exceptions, as they're deprecated and will be removed
220from future versions of python. If you're in doubt about what type of exception
221you will raise, please look at http://docs.python.org/lib/module-exceptions.html
Christopher Wileyfe2ea872016-02-02 12:12:18 -0800222and client/common\_lib/error.py, the former is a list of python built in
mbligh900b6c12008-01-14 16:56:47 +0000223exceptions and the later is a list of autotest/autoserv internal exceptions. Of
224course, if you really need to, you can extend the exception definitions on
Christopher Wileyfe2ea872016-02-02 12:12:18 -0800225client/common\_lib/error.py.
mbligh900b6c12008-01-14 16:56:47 +0000226
227
Christopher Wileyfe2ea872016-02-02 12:12:18 -0800228## Submitting patches
mbligh71d338a2007-10-08 05:05:50 +0000229
230Generate universal diffs. Email them to autotest@test.kernel.org.
231Most mailers now break lines and/or changes tabs to spaces. If you know how
mbligh52207362009-09-03 20:54:29 +0000232to avoid that - great, put your patches inline. If you're not sure, just
mbligh71d338a2007-10-08 05:05:50 +0000233attatch them, I don't care much. Please base them off the current version.
234
235Don't worry about submitting patches to a public list - everybody makes
236mistakes, especially me ... so get over it and don't worry about it.
237(though do give your changes a test first ;-))