pondělí 29. května 2017

The art of readable code - notes

Naming
choose specific word (get vs. download), but better be clear than cute
don't use generic names
add unit to names
if the value can be surprising, put this info into a name (e.g. password vs. plaintextPassword)
Short names only for short scope (when you can see the var declaration immediately)
Don't use ambiguous words (filter vs select or exclude)
Use first or min and last or max for inclusive ranges
Use begin and end for exclusive ranges (convention)


Aesthetics
similar codes should look similar - better introduce extra line breaks if codes belong together


Commenting
don't comment bad names: good code > bad code + comment
comment your constants

comments should be compact, but precise
named function parameters Connect(/* timeout_ms = */ 10, /* use_encryption = */ false); 


Avoid do-while, put condition up front
Return early from function


Eliminate control flow variables - return early
Shrink variable scope as much as possible


Defragment code - same tasks should be group together, separate them into different functions or at least blocks

Tests
use simplest test inputs that completely exercise your code - don't made up random big numbers, it is confusing

Žádné komentáře:

Okomentovat