Changes between Version 33 and Version 34 of Hacking


Ignore:
Timestamp:
11/03/24 09:10:44 (2 months ago)
Author:
zaytsev
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Hacking

    v33 v34  
    267267Reduce variable scope as much as possible: declare local variable in that block where it is used. 
    268268 
    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:''' 
     269If variable is introduced to store intermediate value, declare it as constant. 
     270 
     271'''This is right:''' 
     272{{{ 
     273const vfs_path_t *vpath = vfs_path_from_str (filename); 
     274}}} 
     275 
     276'''This is wrong:''' 
    278277{{{ 
    279278vfs_path_t *vpath; 
    280279 
    281280vpath = vfs_path_from_str (filename); 
    282 }}} 
    283  
    284 '''This is wrong:''' 
    285 {{{ 
    286 vfs_path_t *vpath = vfs_path_from_str (filename); 
    287281}}} 
    288282 
     
    324318    WPanel *panel = current_panel; 
    325319    int i; 
    326     ComputeDirSizeUI *ui; 
    327  
    328     ui = compute_dir_size_create_ui (); 
     320     
     321    const ComputeDirSizeUI *ui = compute_dir_size_create_ui (); 
    329322 
    330323    compute_dir_size_destroy_ui (ui); 
     
    340333{ 
    341334    compute_dir_size_destroy_ui (compute_dir_size_create_ui ()); 
     335} 
     336}}} 
     337 
     338Avoid abusing non-const function parameters as local variables: 
     339 
     340'''This is right:''' 
     341{{{ 
     342void 
     343foo (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{{{ 
     355void 
     356foo (int iterations) 
     357{ 
     358    do_one_thing (iterations); 
     359    do_something (iterations); 
     360    ... 
    342361} 
    343362}}}