blob: 452e5f74cc6af82f14866ae6ba267af2a4c4fc47 [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
16When writing python code, unless otherwise stated, stick to the python style
17guide (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
34Use descriptive variable names where possible - it helps to make the code
35self 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
mbligh7654c822008-04-04 15:12:48 +000041Importing modules
42
43The order of imports should be as follows:
44
45Standard python modules
46Non-standard python modules
47Autotest modules
48
49Within one of these three sections, all module imports using the from
50keyword should appear after regular imports.
51Modules should be lumped together on the same line.
52Wildcard imports (from x import *) should be avoided if possible.
53Classes should not be imported from modules, but modules may be imported
54 from packages, i.e.:
55from common_lib import error
56and not
57from common_lib.error import AutoservError
58
59For example:
60import os, pickle, random, re, select, shutil, signal, StringIO, subprocess
61import sys, time, urllib, urlparse
62import MySQLdb
63from common_lib import error
64
mbligh71d338a2007-10-08 05:05:50 +000065
66Comments
67
68Generally, you want your comments to tell WHAT your code does, not HOW.
69We can figure out how from the code itself (or if not, your code needs fixing).
70
71Try to describle the intent of a function and what it does in a triple-quoted
72(multiline) string just after the def line. We've tried to do that in most
73places, though undoubtedly we're not perfect. A high level overview is
74incredibly helpful in understanding code.
75
76
77Simple code
78
79Keep it simple; this is not the right place to show how smart you are. We
80have plenty of system failures to deal with without having to spend ages
81figuring out your code, thanks ;-) Readbility, readability, readability.
82I really don't care if other things are more compact.
83
84"Debugging is twice as hard as writing the code in the first place. Therefore,
85if you write the code as cleverly as possible, you are, by definition, not
86smart enough to debug it." Brian Kernighan
87
88
89Function length
90
91Please keep functions short, under 30 lines or so if possible. Even though
92you are amazingly clever, and can cope with it, the rest of us are all stupid,
93so be nice and help us out. To quote the Linux Kernel coding style:
94
95Functions should be short and sweet, and do just one thing. They should
96fit on one or two screenfuls of text (the ISO/ANSI screen size is 80x24,
97as we all know), and do one thing and do that well.
98
99
mbligh900b6c12008-01-14 16:56:47 +0000100Exceptions
101
102When raising exceptions, the preferred syntax for it is:
103
104raise FooException('Exception Message')
105
106Please don't raise string exceptions, as they're deprecated and will be removed
107from future versions of python. If you're in doubt about what type of exception
108you will raise, please look at http://docs.python.org/lib/module-exceptions.html
109and client/common_lib/error.py, the former is a list of python built in
110exceptions and the later is a list of autotest/autoserv internal exceptions. Of
111course, if you really need to, you can extend the exception definitions on
112client/common_lib/error.py.
113
114
mbligh71d338a2007-10-08 05:05:50 +0000115Submitting patches
116
117Generate universal diffs. Email them to autotest@test.kernel.org.
118Most mailers now break lines and/or changes tabs to spaces. If you know how
119to avoid that - great, put your patches inline. If you're not sure, just
120attatch them, I don't care much. Please base them off the current version.
121
122Don't worry about submitting patches to a public list - everybody makes
123mistakes, especially me ... so get over it and don't worry about it.
124(though do give your changes a test first ;-))