Changes between Version 25 and Version 26 of Hacking
- Timestamp:
- 01/03/16 13:58:07 (9 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
Hacking
v25 v26 2 2 = HACKING = 3 3 4 == Cod eStyle ==4 == Coding Style == 5 5 6 6 Please use the same indentation as other developers. 7 7 * Maximum line width is 100 characters. The reason is not about people with low-res screens but rather sticking to 100 columns prevents you from easily nesting more than one level of if statements or other code blocks. 8 * Use 4 space tabs with whitespace fillers to Indent. Never use tabs.9 * No trailing whitespace 10 * Follow the GNU-Style guidelines. 8 * No tabs. Use 4 space tabs with whitespace fillers to indent. 9 * No trailing whitespace. 10 * Follow the GNU-Style guidelines. 11 11 12 12 To format the code, use the '''indent''' utility with following options: … … 23 23 24 24 or in short notation: 25 26 25 {{{ 27 26 indent -gnu -fc1 -i4 -bli0 -nut -bap -l100 … … 34 33 Comments should always use the standard C syntax. C++ style comments are not currently allowed. 35 34 36 The lines before a comment should be empty. If the comment directly belongs to the following code, there should be no empty line after the comment, except if the comment contains a summary of multiple following code blocks.35 Lines before a comment should be empty. If the comment directly belongs to the following code, there should be no empty line after the comment, except if the comment contains a summary of multiple following code blocks. 37 36 38 37 '''This is right:''' … … 74 73 }}} 75 74 76 === Conditions in Code blocks (if, for, case...) === 77 78 Always follow an 'if' keyword with a space but don't include additional 79 spaces following or preceding the parentheses in the conditional. 75 === Conditions in code blocks (if, for, case...) === 76 77 Always follow an 'if' keyword with a space but don't include additional spaces following or preceding the parentheses in the conditional. 80 78 81 79 '''This is right:''' … … 142 140 path = strip_home_and_password (current_panel->cwd); } 143 141 144 145 146 147 142 for (k = 0; k < 10; k++) 143 for (j = 0; j < 10; j++) 144 for (i = 0; str_options[i].opt_name != NULL; i++) 145 g_free (*str_options[i].opt_addr); 148 146 }}} 149 147 … … 160 158 dest = input_expand_dialog (_("Link"), src, MC_HISTORY_FM_LINK, ""); 161 159 162 if ( !dest || !*dest)160 if (dest == NULL || *dest == '\0') 163 161 goto cleanup; 164 162 ... … … 176 174 === Readable Code === 177 175 178 Use your best judgement and choose the more readable option. 179 Remember that many other persons will review it: 176 Use your best judgement and choose the more readable option. Remember that many other persons will review it: 180 177 181 178 '''This is right:''' 182 179 {{{ 183 180 bytes = read (fd, &routine.pointer, sizeof (routine)); 184 if (bytes == -1 || (size_t) bytes < (sizeof (routine))) 185 }}} 186 187 '''This is wrong:''' 188 {{{ 189 if (bytes = read (fd, &routine.pointer, sizeof (routine)) == -1 || (size_t) bytes < (sizeof (routine))) 190 191 if (read (from_parent_fd, &i, sizeof (int)) != sizeof (int)) 192 return NULL; 181 if (bytes == -1 || (size_t) bytes < sizeof (routine)) 182 ... 183 }}} 184 185 '''This is wrong:''' 186 {{{ 187 if ((bytes = read (fd, &routine.pointer, sizeof (routine))) == -1 || (size_t) bytes < sizeof (routine)) 188 ... 189 }}} 190 191 Don not place more than one statement in one line. 192 193 '''This is right:''' 194 {{{ 195 a = 0; 196 b = 2; 197 198 a = f(); 199 if (a == 2) 200 b = 5; 201 202 }}} 203 204 '''This is wrong:''' 205 {{{ 206 a = 0; b = 2; 207 208 if ((a = f()) == 2) 209 b = 5; 210 211 if (a == 2) b = 5; 193 212 }}} 194 213 … … 227 246 }}} 228 247 248 Do not check boolean values for equality: 249 250 '''This is right:''' 251 {{{ 252 gboolean b1, b2; 253 254 if (b1) 255 if (!b2) 256 }}} 257 258 '''This is wrong:''' 259 {{{ 260 gboolean b1, b2; 261 262 if (b1 == TRUE) 263 if (b2 == FALSE) 264 }}} 265 266 229 267 === Variables === 230 268 231 Declare variable only in the beginning of block. Split variable declaration and code using one empty line. Try to avoid using functions to initialize variables. Split variable declaration and value assignment. 269 Reduce variable scope as much as possible: declare local variable in that block where it is used. 270 271 Don't mix variable declaration and code, declare variable only in the beginning of block. 272 273 Split variable declaration and code using one empty line. Avoid using function to initialize variable. Split variable declaration and value assignment. 232 274 233 275 '''This is right:''' … … 239 281 240 282 '''This is wrong:''' 241 242 283 {{{ 243 284 vfs_path_t *vpath = vfs_path_from_str (filename); 244 285 }}} 245 286 246 === Make use of helper variables ===247 248 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.287 === Helper variables === 288 289 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. 249 290 250 291 '''This is right:''' … … 326 367 327 368 Do not mix global and local variables and functions. 328 Do not mix static and non-static stat ments.369 Do not mix static and non-static statements. 329 370 330 371 Follow typical structure of *.c file: