On hashnode I asked a question about programming tips to write better code. Mark gave a really nice answer which I thought would be great to share on my blog.

  1. Write automated tests for features, either before or after the ‘real’ code.
  2. When fixing any bug, write a test so it can never come back unnoticed. (Note that all this testing is not just for having tests; it also encouraged modular design and dependency injection).
  3. Use linters for style and to find potential bugs.
  4. Use feature branches that check the tests and linters mentioned above before merging. Reviews are also great, if you’re not alone.
  5. Use types for more than primitives. E.g. make types for Name, Id, EuroAmt (currency), UnsafeString…
  6. Write readable code first. Only optimize after measuring performance and writing unit tests.
  7. Use asserts (or require or whatever) to document assumptions.
  8. When in doubt, fail. Fast and loud.
  9. Avoid mutable state where possible.
  10. Mark object fields final. Maybe all the fields.
  11. Prefer communicating through arguments and return values rather than changing state.
  12. Especially prevent sharing mutable state across threads.
  13. Use immutable collections (many languages don’t have them built-in, unfortunately, but find a way).
  14. Use a language that has non-nullable types.
  15. Keep units of code as independent as possible, exposing only what they need to know about eachother. Design patterns help here.
  16. Be willing to throw things out. Schedule time for refactoring. Sometimes it was designed wrong, other times it grew out of control, but it has to be fixed.
  17. Don’t overdo inheritance (no big class trees).