blob: b640019d6119a9d7402a2794ae0a73e585ef2771 [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
27Don't leave trailing whitespace, or put whitespace on blank lines.
28Leave TWO blank lines between functions - this is Python, there are no clear
29function end markers, and we need help.
30
31
32Variable names and UpPeR cAsE
33
mbligh52207362009-09-03 20:54:29 +000034Use descriptive variable names where possible - it helps to make the code
mbligh71d338a2007-10-08 05:05:50 +000035self documenting.
36
37Don't use CamelCaps style in most places - use underscores to separate parts
38of your variable_names please. I shall make a bedgrudging exception for class
39names I suppose, but I'll still whine about it a lot.
40
mblighd876f452008-12-03 15:09:17 +000041
mbligh7654c822008-04-04 15:12:48 +000042Importing modules
43
44The order of imports should be as follows:
45
46Standard python modules
47Non-standard python modules
48Autotest modules
49
50Within one of these three sections, all module imports using the from
51keyword should appear after regular imports.
52Modules should be lumped together on the same line.
53Wildcard imports (from x import *) should be avoided if possible.
54Classes should not be imported from modules, but modules may be imported
55 from packages, i.e.:
56from common_lib import error
57and not
58from common_lib.error import AutoservError
59
60For example:
61import os, pickle, random, re, select, shutil, signal, StringIO, subprocess
62import sys, time, urllib, urlparse
mbligh8bcd23a2009-02-03 19:14:06 +000063import common # Magic autotest_lib module and sys.path setup code.
64import MySQLdb # After common so that we check our site-packages first.
mbligh7654c822008-04-04 15:12:48 +000065from common_lib import error
66
mblighd876f452008-12-03 15:09:17 +000067Testing None
68
69Use "is None" rather than "== None" and "is not None" rather than "!= None".
mbligh52207362009-09-03 20:54:29 +000070This way you'll never run into a case where someone's __eq__ or __ne__
mblighd876f452008-12-03 15:09:17 +000071method do the wrong thing
72
mbligh71d338a2007-10-08 05:05:50 +000073
74Comments
75
76Generally, you want your comments to tell WHAT your code does, not HOW.
77We can figure out how from the code itself (or if not, your code needs fixing).
78
79Try to describle the intent of a function and what it does in a triple-quoted
80(multiline) string just after the def line. We've tried to do that in most
mbligh52207362009-09-03 20:54:29 +000081places, though undoubtedly we're not perfect. A high level overview is
mbligh71d338a2007-10-08 05:05:50 +000082incredibly helpful in understanding code.
83
84
mbligh5cad50f2009-06-08 16:50:51 +000085Docstrings
86
87Docstrings are important to keep our code self documenting. While it's not
88necessary to overdo documentation, we ask you to be sensible and document any
89nontrivial function. When creating docstrings, please add a newline at the
showard25f056a2009-11-23 20:22:50 +000090beginning of your triple quoted string and another newline at the end of it. If
91the docstring has multiple lines, please include a short summary line followed
92by a blank line before continuing with the rest of the description. Please
93capitalize and punctuate accordingly the sentences. If the description has
94multiple lines, put two levels of indentation before proceeding with text. An
95example docstring:
mbligh5cad50f2009-06-08 16:50:51 +000096
97def foo(param1, param2):
mbligh52207362009-09-03 20:54:29 +000098 """
showard25f056a2009-11-23 20:22:50 +000099 Summary line.
100
mbligh52207362009-09-03 20:54:29 +0000101 Long description of method foo.
mbligh5cad50f2009-06-08 16:50:51 +0000102
mbligh52207362009-09-03 20:54:29 +0000103 @param param1: A thing called param1 that is used for a bunch of stuff
104 that has methods bar() and baz() which raise SpamError if
105 something goes awry.
106 ...
107 """
mbligh5cad50f2009-06-08 16:50:51 +0000108
109The tags that you can put inside your docstring are tags recognized by systems
110like doxygen. Not all places need all tags defined, so choose them wisely while
111writing code.
112
113@author - Code author
114@param - Parameter description
115@return - Return value description
116@see - Reference to what you have done
117@todo - Things that still need to be worked out
118@version - Version string
119@warning - Call attention to potential problems with the code
120@raises - If the function can throw an exception, this tag documents the
121possible exception types.
122
mbligh71d338a2007-10-08 05:05:50 +0000123Simple code
124
125Keep it simple; this is not the right place to show how smart you are. We
126have plenty of system failures to deal with without having to spend ages
127figuring out your code, thanks ;-) Readbility, readability, readability.
mbligh52207362009-09-03 20:54:29 +0000128I really don't care if other things are more compact.
mbligh71d338a2007-10-08 05:05:50 +0000129
130"Debugging is twice as hard as writing the code in the first place. Therefore,
mbligh52207362009-09-03 20:54:29 +0000131if you write the code as cleverly as possible, you are, by definition, not
mbligh71d338a2007-10-08 05:05:50 +0000132smart enough to debug it." Brian Kernighan
133
134
135Function length
136
137Please keep functions short, under 30 lines or so if possible. Even though
138you are amazingly clever, and can cope with it, the rest of us are all stupid,
139so be nice and help us out. To quote the Linux Kernel coding style:
140
141Functions should be short and sweet, and do just one thing. They should
142fit on one or two screenfuls of text (the ISO/ANSI screen size is 80x24,
143as we all know), and do one thing and do that well.
144
145
mbligh900b6c12008-01-14 16:56:47 +0000146Exceptions
147
148When raising exceptions, the preferred syntax for it is:
149
150raise FooException('Exception Message')
151
152Please don't raise string exceptions, as they're deprecated and will be removed
153from future versions of python. If you're in doubt about what type of exception
154you will raise, please look at http://docs.python.org/lib/module-exceptions.html
155and client/common_lib/error.py, the former is a list of python built in
156exceptions and the later is a list of autotest/autoserv internal exceptions. Of
157course, if you really need to, you can extend the exception definitions on
158client/common_lib/error.py.
159
160
mbligh71d338a2007-10-08 05:05:50 +0000161Submitting patches
162
163Generate universal diffs. Email them to autotest@test.kernel.org.
164Most mailers now break lines and/or changes tabs to spaces. If you know how
mbligh52207362009-09-03 20:54:29 +0000165to avoid that - great, put your patches inline. If you're not sure, just
mbligh71d338a2007-10-08 05:05:50 +0000166attatch them, I don't care much. Please base them off the current version.
167
168Don't worry about submitting patches to a public list - everybody makes
169mistakes, especially me ... so get over it and don't worry about it.
170(though do give your changes a test first ;-))