3 ms·
Here are some principles that inform my taste in maintainable code: - Each function should do just one thing. - Don't reuse a variable if making a new variabl
by cnees 8y ago
Here are some principles that inform my taste in maintainable code:
- Each function should do just one thing.
- Don't reuse a variable if making a new variable with a new name would describe the value better.
- Give functions verb names that describe what they do. If that's hard, they may be doing too many things.
- A function should either change something or return a value (command-query separation)
- Any data should have a single, canonical source of truth. https://en.wikipedia.org/wiki/Single_source_of_truth https://en.wikipedia.org/wiki/Single_source_of_truth
- When deciding between making code DRY https://en.wikipedia.org/wiki/Don%27t_repeat_yourself https://en.wikipedia.org/wiki/Don%27t_repeat_yourself or not, decide if future changes should affect both places at the same time (use DRY) or not (probably doesn't need DRY, maybe shouldn't have it.)
- Avoid spooky action at a distance https://en.wikipedia.org/wiki/Action_at_a_distance_(computer_programming) https://en.wikipedia.org/wiki/Action_at_a_distance_(computer... and if you can't, refactor or at least add comments.
- Write modular functions that can be used without understanding much about the function beside what the name/arguments tell you.
One measure of your success in this area is how quickly someone who'd never seen your code could describe what it does.