Changes between Version 11 and Version 12 of Hacking
- Timestamp:
- 02/15/11 21:41:37 (14 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
Hacking
v11 v12 71 71 }}} 72 72 73 === Co de blocks (if, for, case...) ===73 === Conditions in Code blocks (if, for, case...) === 74 74 75 75 Always follow an 'if' keyword with a space but don't include additional … … 79 79 {{{ 80 80 if (i == 0) 81 81 82 }}} 82 83 '''This is wrong:''' … … 84 85 if ( i == 0 ) 85 86 if (0 == i) 86 }}} 87 88 }}} 89 90 === Functions usage === 87 91 88 92 Always insert a space between the name and left parentheses when invoking functions. … … 91 95 {{{ 92 96 do_example (int param1, int *result1); 97 93 98 }}} 94 99 '''This is wrong:''' 95 100 {{{ 96 101 do_example(int param1, int *result1); 97 }}} 102 103 }}} 104 105 === Braces === 98 106 99 107 Braces for code blocks used by '''for, if, switch, while, do..while''', etc. should begin on the next line after the statement keyword and end on a line of their own. … … 136 144 }}} 137 145 146 === Goto === 147 138 148 Use "goto" only when necessary. "goto"s are evil, but they can greatly enhance readability and reduce memory leaks when used as the single exit point from a function. 139 149 … … 158 168 g_free (dest); 159 169 } 160 }}} 170 171 }}} 172 173 === Readable The Code === 161 174 162 175 Use your best judgement and choose the more readable option. 176 Remember that many other persons will review it: 163 177 164 178 '''This is right:''' … … 166 180 bytes = read (fd, &routine.pointer, sizeof (routine)); 167 181 if (bytes == -1 || (size_t) bytes < (sizeof (routine))) 182 168 183 }}} 169 184 … … 174 189 if (read (from_parent_fd, &i, sizeof (int)) != sizeof (int)) 175 190 return NULL; 191 192 }}} 193 194 === Make use of helper variables === 195 196 Please try to avoid passing function calls as function parameters in new code. This makes the code much easier to read and it's also easier to use the "step" command within gdb. 197 198 '''This is right:''' 199 {{{ 200 void 201 dirsizes_cmd (void) 202 { 203 WPanel *panel = current_panel; 204 int i; 205 ComputeDirSizeUI *ui; 206 207 ui = compute_dir_size_create_ui (); 208 209 compute_dir_size_destroy_ui (ui); 210 211 recalculate_panel_summary (panel); 212 213 } 214 215 }}} 216 217 '''This is wrong:''' 218 {{{ 219 void 220 dirsizes_cmd (void) 221 { 222 compute_dir_size_destroy_ui (compute_dir_size_create_ui ()); 223 } 176 224 177 225 }}}