mbligh | 71d338a | 2007-10-08 05:05:50 +0000 | [diff] [blame] | 1 | These rules are fairly standard and boring. People will bitch about something |
| 2 | in here, no doubt. Get over it. Much of this was stolen from the Linux Kernel |
| 3 | coding style, because most of it makes good sense. If you disagree, that's OK, |
| 4 | but please stick to the rules anyway ;-) |
| 5 | |
| 6 | |
| 7 | Language |
| 8 | |
| 9 | Please use Python where possible. It's not the ideal language for everything, |
| 10 | but it's pretty good, and consistency goes a long way in making the project |
| 11 | maintainable. (Obviously using C or whatever for writing tests is fine). |
| 12 | |
| 13 | |
| 14 | Indentation & whitespace |
| 15 | |
| 16 | Format your code for an 80 character wide screen. |
| 17 | |
| 18 | Indentation is hard tabs, equivalent to 8 spaces. Yes, I know that's pretty |
| 19 | large, but especially in Python where code flow is controlled by indentation |
| 20 | levels, it's critical for it to be clearly visibile. |
| 21 | |
| 22 | People will claim that doesn't allow them enough levels of indentation on an |
| 23 | 80 character screen - they need to fix their code instead: use smaller |
| 24 | functions, and more local variables - it makes the code so much easier to read. |
| 25 | |
| 26 | Don't leave trailing whitespace, or put whitespace on blank lines. |
| 27 | Leave TWO blank lines between functions - this is Python, there are no clear |
| 28 | function end markers, and we need help. |
| 29 | |
| 30 | |
| 31 | Variable names and UpPeR cAsE |
| 32 | |
| 33 | Use descriptive variable names where possible - it helps to make the code |
| 34 | self documenting. |
| 35 | |
| 36 | Don't use CamelCaps style in most places - use underscores to separate parts |
| 37 | of your variable_names please. I shall make a bedgrudging exception for class |
| 38 | names I suppose, but I'll still whine about it a lot. |
| 39 | |
mbligh | 7654c82 | 2008-04-04 15:12:48 +0000 | [diff] [blame^] | 40 | Importing modules |
| 41 | |
| 42 | The order of imports should be as follows: |
| 43 | |
| 44 | Standard python modules |
| 45 | Non-standard python modules |
| 46 | Autotest modules |
| 47 | |
| 48 | Within one of these three sections, all module imports using the from |
| 49 | keyword should appear after regular imports. |
| 50 | Modules should be lumped together on the same line. |
| 51 | Wildcard imports (from x import *) should be avoided if possible. |
| 52 | Classes should not be imported from modules, but modules may be imported |
| 53 | from packages, i.e.: |
| 54 | from common_lib import error |
| 55 | and not |
| 56 | from common_lib.error import AutoservError |
| 57 | |
| 58 | For example: |
| 59 | import os, pickle, random, re, select, shutil, signal, StringIO, subprocess |
| 60 | import sys, time, urllib, urlparse |
| 61 | import MySQLdb |
| 62 | from common_lib import error |
| 63 | |
mbligh | 71d338a | 2007-10-08 05:05:50 +0000 | [diff] [blame] | 64 | |
| 65 | Comments |
| 66 | |
| 67 | Generally, you want your comments to tell WHAT your code does, not HOW. |
| 68 | We can figure out how from the code itself (or if not, your code needs fixing). |
| 69 | |
| 70 | Try to describle the intent of a function and what it does in a triple-quoted |
| 71 | (multiline) string just after the def line. We've tried to do that in most |
| 72 | places, though undoubtedly we're not perfect. A high level overview is |
| 73 | incredibly helpful in understanding code. |
| 74 | |
| 75 | |
| 76 | Simple code |
| 77 | |
| 78 | Keep it simple; this is not the right place to show how smart you are. We |
| 79 | have plenty of system failures to deal with without having to spend ages |
| 80 | figuring out your code, thanks ;-) Readbility, readability, readability. |
| 81 | I really don't care if other things are more compact. |
| 82 | |
| 83 | "Debugging is twice as hard as writing the code in the first place. Therefore, |
| 84 | if you write the code as cleverly as possible, you are, by definition, not |
| 85 | smart enough to debug it." Brian Kernighan |
| 86 | |
| 87 | |
| 88 | Function length |
| 89 | |
| 90 | Please keep functions short, under 30 lines or so if possible. Even though |
| 91 | you are amazingly clever, and can cope with it, the rest of us are all stupid, |
| 92 | so be nice and help us out. To quote the Linux Kernel coding style: |
| 93 | |
| 94 | Functions should be short and sweet, and do just one thing. They should |
| 95 | fit on one or two screenfuls of text (the ISO/ANSI screen size is 80x24, |
| 96 | as we all know), and do one thing and do that well. |
| 97 | |
| 98 | |
mbligh | 900b6c1 | 2008-01-14 16:56:47 +0000 | [diff] [blame] | 99 | Exceptions |
| 100 | |
| 101 | When raising exceptions, the preferred syntax for it is: |
| 102 | |
| 103 | raise FooException('Exception Message') |
| 104 | |
| 105 | Please don't raise string exceptions, as they're deprecated and will be removed |
| 106 | from future versions of python. If you're in doubt about what type of exception |
| 107 | you will raise, please look at http://docs.python.org/lib/module-exceptions.html |
| 108 | and client/common_lib/error.py, the former is a list of python built in |
| 109 | exceptions and the later is a list of autotest/autoserv internal exceptions. Of |
| 110 | course, if you really need to, you can extend the exception definitions on |
| 111 | client/common_lib/error.py. |
| 112 | |
| 113 | |
mbligh | 71d338a | 2007-10-08 05:05:50 +0000 | [diff] [blame] | 114 | Submitting patches |
| 115 | |
| 116 | Generate universal diffs. Email them to autotest@test.kernel.org. |
| 117 | Most mailers now break lines and/or changes tabs to spaces. If you know how |
| 118 | to avoid that - great, put your patches inline. If you're not sure, just |
| 119 | attatch them, I don't care much. Please base them off the current version. |
| 120 | |
| 121 | Don't worry about submitting patches to a public list - everybody makes |
| 122 | mistakes, especially me ... so get over it and don't worry about it. |
| 123 | (though do give your changes a test first ;-)) |