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

8Responses

Write your response…

This answer has received 3 appreciations.

  • 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).

Go ahead, but I don't take responsibility for any bugs or downtime caused :-)

Write a reply...

This answer has received 2 appreciations.

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

Write a reply...

This answer has received 1 appreciation.

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.

Write a reply...

This answer has received 1 appreciation.

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

Write a reply...

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.

Write a reply...

Load more responses

Join a friendly and inclusive Q&A network for coders

  • 🖥Pick the technologies you like & read great content through your feed.
  • 💬Ask a question when you want to learn more about anything.
  • 🚀Share what you know & build your portfolio.
Sign up nowLearn more

loading ...