blob: 0fcd09110c0a4f6c457e074d8f5acc54e44ce750 [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.
47Leave TWO blank lines between functions - this is Python, there are no clear
48function end markers, and we need help.
49
50
51Variable names and UpPeR cAsE
52
mbligh52207362009-09-03 20:54:29 +000053Use descriptive variable names where possible - it helps to make the code
mbligh71d338a2007-10-08 05:05:50 +000054self documenting.
55
56Don't use CamelCaps style in most places - use underscores to separate parts
57of your variable_names please. I shall make a bedgrudging exception for class
58names I suppose, but I'll still whine about it a lot.
59
jamesrend0e17162010-07-29 17:37:32 +000060Importing modules
61
62The order of imports should be as follows:
63
64Standard python modules
65Non-standard python modules
66Autotest modules
67
68Within one of these three sections, all module imports using the from
69keyword should appear after regular imports.
70Modules should be lumped together on the same line.
71Wildcard imports (from x import *) should be avoided if possible.
72Classes should not be imported from modules, but modules may be imported
73 from packages, i.e.:
74from common_lib import error
75and not
76from common_lib.error import AutoservError
77
78For example:
79import os, pickle, random, re, select, shutil, signal, StringIO, subprocess
80import sys, time, urllib, urlparse
81import MySQLdb
82from common_lib import error
83
mblighd876f452008-12-03 15:09:17 +000084
mbligh7654c822008-04-04 15:12:48 +000085Importing modules
86
87The order of imports should be as follows:
88
89Standard python modules
90Non-standard python modules
91Autotest modules
92
93Within one of these three sections, all module imports using the from
94keyword should appear after regular imports.
95Modules should be lumped together on the same line.
96Wildcard imports (from x import *) should be avoided if possible.
97Classes should not be imported from modules, but modules may be imported
98 from packages, i.e.:
99from common_lib import error
100and not
101from common_lib.error import AutoservError
102
103For example:
104import os, pickle, random, re, select, shutil, signal, StringIO, subprocess
105import sys, time, urllib, urlparse
mbligh8bcd23a2009-02-03 19:14:06 +0000106import common # Magic autotest_lib module and sys.path setup code.
107import MySQLdb # After common so that we check our site-packages first.
mbligh7654c822008-04-04 15:12:48 +0000108from common_lib import error
109
mblighd876f452008-12-03 15:09:17 +0000110Testing None
111
112Use "is None" rather than "== None" and "is not None" rather than "!= None".
mbligh52207362009-09-03 20:54:29 +0000113This way you'll never run into a case where someone's __eq__ or __ne__
mblighd876f452008-12-03 15:09:17 +0000114method do the wrong thing
115
mbligh71d338a2007-10-08 05:05:50 +0000116
117Comments
118
119Generally, you want your comments to tell WHAT your code does, not HOW.
120We can figure out how from the code itself (or if not, your code needs fixing).
121
122Try to describle the intent of a function and what it does in a triple-quoted
123(multiline) string just after the def line. We've tried to do that in most
mbligh52207362009-09-03 20:54:29 +0000124places, though undoubtedly we're not perfect. A high level overview is
mbligh71d338a2007-10-08 05:05:50 +0000125incredibly helpful in understanding code.
126
127
mbligh5cad50f2009-06-08 16:50:51 +0000128Docstrings
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
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
140def foo(param1, param2):
mbligh52207362009-09-03 20:54:29 +0000141 """
showard25f056a2009-11-23 20:22:50 +0000142 Summary line.
143
mbligh52207362009-09-03 20:54:29 +0000144 Long description of method foo.
mbligh5cad50f2009-06-08 16:50:51 +0000145
mbligh52207362009-09-03 20:54:29 +0000146 @param param1: A thing called param1 that is used for a bunch of stuff
147 that has methods bar() and baz() which raise SpamError if
148 something goes awry.
mbligh3bdba922010-05-03 18:02:54 +0000149 @return a list of integers describing changes in a source tree
mbligh52207362009-09-03 20:54:29 +0000150 ...
151 """
mbligh5cad50f2009-06-08 16:50:51 +0000152
153The tags that you can put inside your docstring are tags recognized by systems
154like doxygen. Not all places need all tags defined, so choose them wisely while
155writing code.
156
157@author - Code author
158@param - Parameter description
159@return - Return value description
160@see - Reference to what you have done
161@todo - Things that still need to be worked out
162@version - Version string
163@warning - Call attention to potential problems with the code
164@raises - If the function can throw an exception, this tag documents the
165possible exception types.
166
mbligh3bdba922010-05-03 18:02:54 +0000167When in doubt refer to: http://doxygen.nl/commands.html
168
mbligh71d338a2007-10-08 05:05:50 +0000169Simple code
170
171Keep it simple; this is not the right place to show how smart you are. We
172have plenty of system failures to deal with without having to spend ages
173figuring out your code, thanks ;-) Readbility, readability, readability.
mbligh52207362009-09-03 20:54:29 +0000174I really don't care if other things are more compact.
mbligh71d338a2007-10-08 05:05:50 +0000175
176"Debugging is twice as hard as writing the code in the first place. Therefore,
mbligh52207362009-09-03 20:54:29 +0000177if you write the code as cleverly as possible, you are, by definition, not
mbligh71d338a2007-10-08 05:05:50 +0000178smart enough to debug it." Brian Kernighan
179
180
181Function length
182
183Please keep functions short, under 30 lines or so if possible. Even though
184you are amazingly clever, and can cope with it, the rest of us are all stupid,
185so be nice and help us out. To quote the Linux Kernel coding style:
186
187Functions should be short and sweet, and do just one thing. They should
188fit on one or two screenfuls of text (the ISO/ANSI screen size is 80x24,
189as we all know), and do one thing and do that well.
190
191
mbligh900b6c12008-01-14 16:56:47 +0000192Exceptions
193
194When raising exceptions, the preferred syntax for it is:
195
196raise FooException('Exception Message')
197
198Please don't raise string exceptions, as they're deprecated and will be removed
199from future versions of python. If you're in doubt about what type of exception
200you will raise, please look at http://docs.python.org/lib/module-exceptions.html
201and client/common_lib/error.py, the former is a list of python built in
202exceptions and the later is a list of autotest/autoserv internal exceptions. Of
203course, if you really need to, you can extend the exception definitions on
204client/common_lib/error.py.
205
206
mbligh71d338a2007-10-08 05:05:50 +0000207Submitting patches
208
209Generate universal diffs. Email them to autotest@test.kernel.org.
210Most mailers now break lines and/or changes tabs to spaces. If you know how
mbligh52207362009-09-03 20:54:29 +0000211to avoid that - great, put your patches inline. If you're not sure, just
mbligh71d338a2007-10-08 05:05:50 +0000212attatch them, I don't care much. Please base them off the current version.
213
214Don't worry about submitting patches to a public list - everybody makes
215mistakes, especially me ... so get over it and don't worry about it.
216(though do give your changes a test first ;-))