blob: 44ec4bf2b549c0004f0510bd6371a2bc789be7be [file] [log] [blame]
mbligh71d338a2007-10-08 05:05:50 +00001These rules are fairly standard and boring. People will bitch about something
2in here, no doubt. Get over it. Much of this was stolen from the Linux Kernel
3coding style, because most of it makes good sense. If you disagree, that's OK,
4but please stick to the rules anyway ;-)
5
6
7Language
8
9Please use Python where possible. It's not the ideal language for everything,
10but it's pretty good, and consistency goes a long way in making the project
11maintainable. (Obviously using C or whatever for writing tests is fine).
12
13
mbligh2ac475b2008-09-09 21:37:40 +000014Base coding style
15
mbligh52207362009-09-03 20:54:29 +000016When writing python code, unless otherwise stated, stick to the python style
mbligh2ac475b2008-09-09 21:37:40 +000017guide (http://www.python.org/dev/peps/pep-0008/).
18
19
mbligh71d338a2007-10-08 05:05:50 +000020Indentation & whitespace
21
22Format your code for an 80 character wide screen.
23
mblighc960fcf2008-06-18 19:58:57 +000024Indentation is now 4 spaces, as opposed to hard tabs (which it used to be).
25This is the Python standard.
mbligh71d338a2007-10-08 05:05:50 +000026
Tan Gao8aac17b2013-04-16 08:46:01 -070027For hanging indentation, use 8 spaces plus all args should be on the new line.
28
29 # Either of the following hanging indentation is considered acceptable.
30YES: return 'class: %s, host: %s, args = %s' % (
31 self.__class__.__name__, self.hostname, self.args)
32
33YES: return 'class: %s, host: %s, args = %s' % (
34 self.__class__.__name__,
35 self.hostname,
36 self.args)
37
38 # Do not use 4 spaces for hanging indentation
39NO: return 'class: %s, host: %s, args = %s' % (
40 self.__class__.__name__, self.hostname, self.args)
41
42 # Do put all args on new line
43NO: return 'class: %s, host: %s, args = %s' % (self.__class__.__name__,
44 self.hostname, self.args)
45
mbligh71d338a2007-10-08 05:05:50 +000046Don't leave trailing whitespace, or put whitespace on blank lines.
Simran Basic7330bd2013-05-31 09:23:50 -070047
mbligh71d338a2007-10-08 05:05:50 +000048Leave TWO blank lines between functions - this is Python, there are no clear
49function end markers, and we need help.
50
51
52Variable names and UpPeR cAsE
53
mbligh52207362009-09-03 20:54:29 +000054Use descriptive variable names where possible - it helps to make the code
mbligh71d338a2007-10-08 05:05:50 +000055self documenting.
56
57Don't use CamelCaps style in most places - use underscores to separate parts
58of your variable_names please. I shall make a bedgrudging exception for class
59names I suppose, but I'll still whine about it a lot.
60
mblighd876f452008-12-03 15:09:17 +000061
mbligh7654c822008-04-04 15:12:48 +000062Importing modules
63
64The order of imports should be as follows:
65
66Standard python modules
67Non-standard python modules
68Autotest modules
69
70Within one of these three sections, all module imports using the from
71keyword should appear after regular imports.
72Modules should be lumped together on the same line.
73Wildcard imports (from x import *) should be avoided if possible.
74Classes should not be imported from modules, but modules may be imported
75 from packages, i.e.:
76from common_lib import error
77and not
78from common_lib.error import AutoservError
79
80For example:
81import os, pickle, random, re, select, shutil, signal, StringIO, subprocess
82import sys, time, urllib, urlparse
mbligh8bcd23a2009-02-03 19:14:06 +000083import common # Magic autotest_lib module and sys.path setup code.
84import MySQLdb # After common so that we check our site-packages first.
mbligh7654c822008-04-04 15:12:48 +000085from common_lib import error
86
mblighd876f452008-12-03 15:09:17 +000087Testing None
88
89Use "is None" rather than "== None" and "is not None" rather than "!= None".
mbligh52207362009-09-03 20:54:29 +000090This way you'll never run into a case where someone's __eq__ or __ne__
mblighd876f452008-12-03 15:09:17 +000091method do the wrong thing
92
mbligh71d338a2007-10-08 05:05:50 +000093
94Comments
95
96Generally, you want your comments to tell WHAT your code does, not HOW.
97We can figure out how from the code itself (or if not, your code needs fixing).
98
99Try to describle the intent of a function and what it does in a triple-quoted
100(multiline) string just after the def line. We've tried to do that in most
mbligh52207362009-09-03 20:54:29 +0000101places, though undoubtedly we're not perfect. A high level overview is
mbligh71d338a2007-10-08 05:05:50 +0000102incredibly helpful in understanding code.
103
104
Simran Basic7330bd2013-05-31 09:23:50 -0700105Hardcoded String Formatting
106
107Strings should use only single quotes for hardcoded strings in the code. Double
108quotes are acceptable when single quote is used as part of the string.
109Multiline string should not use '\' but wrap the string using parenthesises.
110
111REALLY_LONG_STRING = ('This is supposed to be a really long string that is '
112 'over 80 characters and does not use a slash to '
113 'continue.')
114
mbligh5cad50f2009-06-08 16:50:51 +0000115Docstrings
116
117Docstrings are important to keep our code self documenting. While it's not
118necessary to overdo documentation, we ask you to be sensible and document any
119nontrivial function. When creating docstrings, please add a newline at the
showard25f056a2009-11-23 20:22:50 +0000120beginning of your triple quoted string and another newline at the end of it. If
121the docstring has multiple lines, please include a short summary line followed
122by a blank line before continuing with the rest of the description. Please
123capitalize and punctuate accordingly the sentences. If the description has
124multiple lines, put two levels of indentation before proceeding with text. An
125example docstring:
mbligh5cad50f2009-06-08 16:50:51 +0000126
127def foo(param1, param2):
mbligh52207362009-09-03 20:54:29 +0000128 """
showard25f056a2009-11-23 20:22:50 +0000129 Summary line.
130
mbligh52207362009-09-03 20:54:29 +0000131 Long description of method foo.
mbligh5cad50f2009-06-08 16:50:51 +0000132
mbligh52207362009-09-03 20:54:29 +0000133 @param param1: A thing called param1 that is used for a bunch of stuff
134 that has methods bar() and baz() which raise SpamError if
135 something goes awry.
Simran Basic7330bd2013-05-31 09:23:50 -0700136
137 @returns a list of integers describing changes in a source tree
138
139 @raises exception that could be raised if a certain condition occurs.
140
mbligh52207362009-09-03 20:54:29 +0000141 """
mbligh5cad50f2009-06-08 16:50:51 +0000142
143The tags that you can put inside your docstring are tags recognized by systems
144like doxygen. Not all places need all tags defined, so choose them wisely while
Simran Basic7330bd2013-05-31 09:23:50 -0700145writing code. Generally (if applicable) always list parameters, return value
146(if there is one), and exceptions that can be raised to each docstring.
mbligh5cad50f2009-06-08 16:50:51 +0000147
148@author - Code author
149@param - Parameter description
Simran Basic7330bd2013-05-31 09:23:50 -0700150@raise - If the function can throw an exception, this tag documents the
mbligh5cad50f2009-06-08 16:50:51 +0000151possible exception types.
Simran Basic7330bd2013-05-31 09:23:50 -0700152@raises - same as @raise.
153@return - Return value description
154@returns - Same as @return
155@see - Reference to what you have done
156@warning - Call attention to potential problems with the code
157@var - Documentation for a variable or enum value (either global or as a
158member of a class)
159@version - Version string
mbligh5cad50f2009-06-08 16:50:51 +0000160
mbligh3bdba922010-05-03 18:02:54 +0000161When in doubt refer to: http://doxygen.nl/commands.html
162
mbligh71d338a2007-10-08 05:05:50 +0000163Simple code
164
165Keep it simple; this is not the right place to show how smart you are. We
166have plenty of system failures to deal with without having to spend ages
167figuring out your code, thanks ;-) Readbility, readability, readability.
mbligh52207362009-09-03 20:54:29 +0000168I really don't care if other things are more compact.
mbligh71d338a2007-10-08 05:05:50 +0000169
170"Debugging is twice as hard as writing the code in the first place. Therefore,
mbligh52207362009-09-03 20:54:29 +0000171if you write the code as cleverly as possible, you are, by definition, not
mbligh71d338a2007-10-08 05:05:50 +0000172smart enough to debug it." Brian Kernighan
173
174
175Function length
176
177Please keep functions short, under 30 lines or so if possible. Even though
178you are amazingly clever, and can cope with it, the rest of us are all stupid,
179so be nice and help us out. To quote the Linux Kernel coding style:
180
181Functions should be short and sweet, and do just one thing. They should
182fit on one or two screenfuls of text (the ISO/ANSI screen size is 80x24,
183as we all know), and do one thing and do that well.
184
185
mbligh900b6c12008-01-14 16:56:47 +0000186Exceptions
187
188When raising exceptions, the preferred syntax for it is:
189
190raise FooException('Exception Message')
191
192Please don't raise string exceptions, as they're deprecated and will be removed
193from future versions of python. If you're in doubt about what type of exception
194you will raise, please look at http://docs.python.org/lib/module-exceptions.html
195and client/common_lib/error.py, the former is a list of python built in
196exceptions and the later is a list of autotest/autoserv internal exceptions. Of
197course, if you really need to, you can extend the exception definitions on
198client/common_lib/error.py.
199
200
mbligh71d338a2007-10-08 05:05:50 +0000201Submitting patches
202
203Generate universal diffs. Email them to autotest@test.kernel.org.
204Most mailers now break lines and/or changes tabs to spaces. If you know how
mbligh52207362009-09-03 20:54:29 +0000205to avoid that - great, put your patches inline. If you're not sure, just
mbligh71d338a2007-10-08 05:05:50 +0000206attatch them, I don't care much. Please base them off the current version.
207
208Don't worry about submitting patches to a public list - everybody makes
209mistakes, especially me ... so get over it and don't worry about it.
210(though do give your changes a test first ;-))