1These 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
14Base coding style
15
16When writing python code, unless otherwise stated, stick to the python style
17guide (http://www.python.org/dev/peps/pep-0008/).
18
19
20Indentation & whitespace
21
22Format your code for an 80 character wide screen.
23
24Indentation is now 4 spaces, as opposed to hard tabs (which it used to be).
25This is the Python standard.
26
27For 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
46Don't leave trailing whitespace, or put whitespace on blank lines.
47
48Leave 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
54Use descriptive variable names where possible - it helps to make the code
55self 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
61
62Importing 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.
72Each module should be imported on its own 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
82import pickle
83import random
84import re
85import select
86import shutil
87import signal
88import subprocess
89
90import common   # Magic autotest_lib module and sys.path setup code.
91import MySQLdb  # After common so that we check our site-packages first.
92
93from common_lib import error
94
95
96Testing None
97
98Use "is None" rather than "== None" and "is not None" rather than "!= None".
99This way you'll never run into a case where someone's __eq__ or __ne__
100method do the wrong thing
101
102
103Comments
104
105Generally, you want your comments to tell WHAT your code does, not HOW.
106We can figure out how from the code itself (or if not, your code needs fixing).
107
108Try to describle the intent of a function and what it does in a triple-quoted
109(multiline) string just after the def line. We've tried to do that in most
110places, though undoubtedly we're not perfect. A high level overview is
111incredibly helpful in understanding code.
112
113
114Hardcoded String Formatting
115
116Strings should use only single quotes for hardcoded strings in the code. Double
117quotes are acceptable when single quote is used as part of the string.
118Multiline string should not use '\' but wrap the string using parenthesises.
119
120REALLY_LONG_STRING = ('This is supposed to be a really long string that is '
121                      'over 80 characters and does not use a slash to '
122                      'continue.')
123
124Docstrings
125
126Docstrings are important to keep our code self documenting. While it's not
127necessary to overdo documentation, we ask you to be sensible and document any
128nontrivial function. When creating docstrings, please add a newline at the
129beginning of your triple quoted string and another newline at the end of it. If
130the docstring has multiple lines, please include a short summary line followed
131by a blank line before continuing with the rest of the description. Please
132capitalize and punctuate accordingly the sentences. If the description has
133multiple lines, put two levels of indentation before proceeding with text. An
134example docstring:
135
136def foo(param1, param2):
137    """
138    Summary line.
139
140    Long description of method foo.
141
142    @param param1: A thing called param1 that is used for a bunch of stuff
143            that has methods bar() and baz() which raise SpamError if
144            something goes awry.
145
146    @returns a list of integers describing changes in a source tree
147
148    @raises exception that could be raised if a certain condition occurs.
149
150    """
151
152The tags that you can put inside your docstring are tags recognized by systems
153like doxygen. Not all places need all tags defined, so choose them wisely while
154writing code. Generally (if applicable) always list parameters, return value
155(if there is one), and exceptions that can be raised to each docstring.
156
157@author - Code author
158@param - Parameter description
159@raise - If the function can throw an exception, this tag documents the
160possible exception types.
161@raises - same as @raise.
162@return - Return value description
163@returns - Same as @return
164@see - Reference to what you have done
165@warning - Call attention to potential problems with the code
166@var -  Documentation for a variable or enum value (either global or as a
167member of a class)
168@version - Version string
169
170When in doubt refer to: http://doxygen.nl/commands.html
171
172Simple code
173
174Keep it simple; this is not the right place to show how smart you are. We
175have plenty of system failures to deal with without having to spend ages
176figuring out your code, thanks ;-) Readbility, readability, readability.
177I really don't care if other things are more compact.
178
179"Debugging is twice as hard as writing the code in the first place. Therefore,
180if you write the code as cleverly as possible, you are, by definition, not
181smart enough to debug it."  Brian Kernighan
182
183
184Function length
185
186Please keep functions short, under 30 lines or so if possible. Even though
187you are amazingly clever, and can cope with it, the rest of us are all stupid,
188so be nice and help us out. To quote the Linux Kernel coding style:
189
190Functions should be short and sweet, and do just one thing.  They should
191fit on one or two screenfuls of text (the ISO/ANSI screen size is 80x24,
192as we all know), and do one thing and do that well.
193
194
195Exceptions
196
197When raising exceptions, the preferred syntax for it is:
198
199raise FooException('Exception Message')
200
201Please don't raise string exceptions, as they're deprecated and will be removed
202from future versions of python. If you're in doubt about what type of exception
203you will raise, please look at http://docs.python.org/lib/module-exceptions.html
204and client/common_lib/error.py, the former is a list of python built in
205exceptions and the later is a list of autotest/autoserv internal exceptions. Of
206course, if you really need to, you can extend the exception definitions on
207client/common_lib/error.py.
208
209
210Submitting patches
211
212Generate universal diffs. Email them to autotest@test.kernel.org.
213Most mailers now break lines and/or changes tabs to spaces. If you know how
214to avoid that - great, put your patches inline. If you're not sure, just
215attatch them, I don't care much. Please base them off the current version.
216
217Don't worry about submitting patches to a public list - everybody makes
218mistakes, especially me ... so get over it and don't worry about it.
219(though do give your changes a test first ;-))
220