-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.coding
100 lines (79 loc) · 2.25 KB
/
README.coding
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
1) 4 space wide soft tabs
2) don't go over 80 chars wide on a line. Adding "match ErrorMsg '\%>80v.\+'"
to your vimrc may help.
3) never cut and paste more than 2 lines of code. use a function.
4) good use of whitespace
5) good comments. don't comment obvious stuff, explain complicated stuff
6) good function names. Pls don't go crazy removing vowels to make names
short. Much better is a longer name whose meaning is immediately obvious
7) put the function name on the next line after the return type so that
it is easy to search for it with /^name/. Example:
// OK
int
my_function()
// NOT OK
int my_function()
8) good variable names. Same as function names unless the variable has a
very temporary lifetime in which case some abbreviation is OK.
9) length of functions. Each function should be easy to read. If a
function has a bunch of internal code that's longer than about 10 lines of
code (just a rule of thumb), then that code should be split out to its
own function. The reason for this is that it makes the parent function
much easier to read. Give the new function a meaningful name and then
the parent function is very easy to read. When someone is interested in
the new child function, they can also just read it and don't need to read
the entire parent function. Another decent rule of thumb is for a function
not to be longer than 80 lines.
10) Use of brackets. Always use brackets unless the action is on same line as
condition. Bracket can be on the end of the line if the line doesn't wrap.
If the line wraps, then the bracket must be dropped to the next line.
Examples:
// OK
if (x>y) do_something();
// NOT OK
if (x>y)
do_something();
// OK
if (x>y) {
do_something();
}
// OK
int
function(arg,arg) {
do_something();
}
// OK
int
function(arg,arg,arg,arg,arg,arg,arg,arg,arg,arg,arg,arg,arg,
arg,arg,arg,arg,arg,arg)
{
do_something();
}
// NOT OK
int
function(arg,arg,arg,arg,arg,arg,arg,arg,arg,arg,arg,arg,arg,
arg,arg,arg,arg,arg,arg) {
do_something();
}
// OK
for(a,b,c) {
do_something();
}
// OK
for(a,
b,
c)
{
do_something();
}
// NOT OK
for(a,
b,
c) {
do_something();
}
// NOT OK (if you're gonna drop b, then pls drop c as well)
for(a,
b, c) {
do_something();
}