How to write good code

By Gerald Mücke | June 27, 2018

How to write good code

A couple of days ago I stumbled over a tweet on Twitter. It contained a list of catchy phrases what makes good writing. Developers also write, but more code than prose texts, so I wondered how applicable this list is to coding as well. So I came up with my own list, which might be a bit less catchy but is hopefully a usefully assembly of good coding practices or guiding principles.

The following list is taken from this tweet How to write good.

These are the points on the list

  1. Avoid Alliteration. Always.
  2. Prepositions are not words to end sentences with.
  3. Avoid cliches like the plaque. They’re old hats.
  4. Comparisons are as bad as cliches.
  5. Be more or less specific.
  6. Writes should never generalize.

Seven: Be consistent.

Although this is related to writing texts, parts of it are relevant to writing code as well.

Same rules, different context

For writing code or software in general, I re-interpreted the list. Although each point is a bit less catchy now, I hope it’s a good collection of good coding practices.

  1. Avoid baroque code, Keep It Short and Simple KISS
  2. Make control and data flows clear and concise. Handle alternative flows first (return/fail fast) and avoid exception handling for control flows. Write Light-bulb methods
  3. Write solutions to your particular problem and scale. Avoid Cargo Cult and Premature Optimization.
  4. Upfront thinking helps you identify concepts and patterns to make good abstractions but avoid Premature Abstractions. Don’t create an abstraction for less than 3 cases.
  5. Take care of proper naming of types, methods, variables and constants. Naming is part of the user experience for a developer and thus should be self-explanatory. “Don’t make me think!”
  6. Use the right data structures that suit best your use case. If you’re using a typed language, make good use of it and avoid too generic parameters or return values (Strings and Objects). No Generic and Unspecified Messages (GUM).
  7. Agree on and enforce coding rules to keep your code consistent. Coding is like Bowling, there are rule! “Smokey, this is not ‘Nam, there are rules!”
  8. Don’t do the same thing over and over again. Don’t be redundant. Don’t Repeat Yourself (DRY)
  9. Spend more effort on critical aspects and logic, keep those areas Clean, Concise, Correct and Checked (C4) and avoid bikeshedding.
  10. Don’t over-engineer things, You Ain’t Gonna Need It! (YAGNI)
comments powered by Disqus