Changes between Version 33 and Version 34 of Hacking
- Timestamp:
- 11/03/24 09:10:44 (3 weeks ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
Hacking
v33 v34 267 267 Reduce variable scope as much as possible: declare local variable in that block where it is used. 268 268 269 Don't mix variable declaration and code, declare variable only in the beginning of block. 270 271 Split variable declaration and code using one empty line. 272 273 Avoid using function to initialize variable. 274 275 Split variable declaration and value assignment. 276 277 '''This is right:''' 269 If variable is introduced to store intermediate value, declare it as constant. 270 271 '''This is right:''' 272 {{{ 273 const vfs_path_t *vpath = vfs_path_from_str (filename); 274 }}} 275 276 '''This is wrong:''' 278 277 {{{ 279 278 vfs_path_t *vpath; 280 279 281 280 vpath = vfs_path_from_str (filename); 282 }}}283 284 '''This is wrong:'''285 {{{286 vfs_path_t *vpath = vfs_path_from_str (filename);287 281 }}} 288 282 … … 324 318 WPanel *panel = current_panel; 325 319 int i; 326 ComputeDirSizeUI *ui; 327 328 ui = compute_dir_size_create_ui (); 320 321 const ComputeDirSizeUI *ui = compute_dir_size_create_ui (); 329 322 330 323 compute_dir_size_destroy_ui (ui); … … 340 333 { 341 334 compute_dir_size_destroy_ui (compute_dir_size_create_ui ()); 335 } 336 }}} 337 338 Avoid abusing non-const function parameters as local variables: 339 340 '''This is right:''' 341 {{{ 342 void 343 foo (const int iterations) 344 { 345 int result; 346 347 do_one_thing (iterations); 348 do_something (result); 349 ... 350 } 351 }}} 352 353 '''This is wrong:''' 354 {{{ 355 void 356 foo (int iterations) 357 { 358 do_one_thing (iterations); 359 do_something (iterations); 360 ... 342 361 } 343 362 }}}