It's time to ditch Medium for good! 🌈⚡️

Introducing Devblog by Hashnode. Blog on your domain for FREE. Highly customizable and optimized for developers.

Learn more

What are some programming habits you follow to keep bugs away?

Like putting literals and constants on the left hand side in conditional statements to avoid assigning.

Write your answer…

12 answers

  • Write automated tests for features, either before or after the 'real' code.
  • 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).
  • Use linters for style and to find potential bugs.
  • Use feature branches that check the tests and linters mentioned above before merging. Reviews are also great, if you're not alone.
  • Use types for more than primitives. E.g. make types for Name, Id<Person>, EuroAmt (currency), UnsafeString...
  • Write readable code first. Only optimize after measuring performance and writing unit tests.
  • Use asserts (or require or whatever) to document assumptions.
  • When in doubt, fail. Fast and loud.
  • Avoid mutable state where possible:
    • Mark object fields final. Maybe all the fields.
    • Prefer communicating through arguments and return values rather than changing state.
    • Especially prevent sharing mutable state across threads.
    • Use immutable collections (many languages don't have them built-in, unfortunately, but find a way).
  • Use a language that has non-nullable types.
  • Keep units of code as independent as possible, exposing only what they need to know about eachother. Design patterns help here.
  • 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.
  • Don't overdo inheritance (no big class trees).
Show all replies

Kind of standard advice, but it should be included:

  • DRY: Don't repeat yourself (except caching, which can be wiped). Things will eventually go out of sync and cause bugs.
  • KIS: Keep it simple. Code should be idiomatic and simple in 99+% of the cases. Only complicate it if necessary (likely for performance, so after measuring), after you have unit tests and documentation for it.

Reply to this…

Share your programming knowledge and learn from the best developers on Hashnode

Get started

CleanCode + IDE Linters + Design patterns = keeps most of the bugs away

Reply to this…

I like your example, never used that.

In my case, I have the habit of always setting a default value to ALL my variables.

Also, I try to never change them unless it makes sense in a specific context.

Reply to this…

I am fortunate to not be clever enough to create any but the simplest bugs, unless the bug is in the language or system itself, in which case, should Google fail, it's despair.

Reply to this…

I tend to think irrespective of all tools we might be using to write code, attention to detail really matters. Also, do unit testing.

Reply to this…

Load more responses