Add notes about import statements (clarity) and fix spelling error in release-notes
[portage.git] / DEVELOPING
1 Code Guidelines
2 ---------------
3 A few code guidelines to try to stick to, please comment of none of these make
4 sense, they are pretty basic and mostly apply to old code.  However for people
5 who are looking at current code, they make take up bad habits that exist in the
6 current codebase.
7
8 Strings
9 -------
10 Try not to use the functions in the string module, they are deprecated.
11
12 string.join(<iterable>," ")
13
14 should be replaced with:
15
16 " ".join(<iterable>)
17
18 and:
19
20 string.split(string, delimeter)
21
22 should be replaced with:
23
24 string.split(delimeter)
25
26 Nearly all other methods in string work on string objects and have similar calling
27 conventions.
28
29 Comparisons
30 -----------
31
32 if foo == None
33
34 should be replaced with:
35
36 if foo is not None:
37
38 Is not does a reference comparison (address1 = address2 basically) and 
39 the == forces a by value compare (with __eq__())
40
41 Dict Lookups
42 ------------
43
44 Try not to use has_key, you can use
45
46 if foo in dict
47
48 instead of if dict.has_key(foo)
49
50 Also don't do stuff like:
51
52 if foo in dict and dict[foo]:
53
54 Generally you can do two things here, if you are messing with defaults..
55
56 dict.get(foo, some_default)
57
58 will try to retrieve foo from dict, if there is a KeyError, will insert foo
59 into dict with the value of some_default.  This method is preferred in cases where
60 you are messing with defaults:
61
62 try:
63         dict[foo]
64 except KeyError:
65         dict[foo] = default_value
66
67 The get call is nicer (compact) and faster (try,except are slow).
68
69 Imports
70 -------
71
72 Import things one per line
73
74 YES:
75   import os
76   import time
77   import sys
78
79 NO:
80   import os,sys,time
81
82 When importing from a module, you may import more than 1 thing at a time.
83
84 YES:
85   from portage.module import foo, bar, baz
86
87 Multiline imports are ok (for now :))
88
89 Try to group system and package imports separately.
90
91 YES:
92   import os
93   import sys
94   import time
95
96   from portage.locks import lockfile
97   from portage.versions import vercmp
98
99 NO:
100   import os
101   import portage
102   import portage.util
103   import time
104   import sys