HACKER Q&A
📣 agomez314

How can I use the scientific method to write better code?


Highly enjoyed this previous post on empirical evidence available for dev practices (https://news.ycombinator.com/item?id=28178257)

This made me start thinking about my own practices, and what processes I could adopt to improve. What suggestions do you have where I can adopt the scientific method to my daily coding, research and software architecting work?


  👤 atmosx Accepted Answer ✓
Since there's no clear definition of _better_ you can't. The point of the scientific method is to test a theory against natural laws.

Assume you write a function using all the tricks in the book. It's fast(er) than the proposed solution on the PR review, but unreadable. If merged, future team members might have a hard time understanding what's happening. If not merged, the code will be slower (milliseconds x thousands of requests).

Which way to go? :-)


👤 djmips
Like others say, keep a journal of your work. Measure things and write them down. Do experiments. There is a lot of extra effort involved and just like in real science, you can be fooled by bad data.

👤 muzani
Science isn't always better. It's unstoppable - you don't lose ground and you will always inch closer to the truth. But it's the slowest approach. It's usually best to leave it to academics like this rather than applying it daily. (Is there a scientific study to how effective science is in daily work?)

I'd recommend reading Zen and the Art of Motorcycle Maintenance though. It covers exactly this topic, the advantages of the scientific method, and where philosophical methods can work better.


👤 gitgud
Define "better"... Do you mean, better readability, better performance, better extensibility, better reliability...

It's immensely difficult to get better in all directions. But if you focus on what you want to get better at, then it's easy to find resources to help.


👤 giantg2
Basically try things and select the ones that work. Make a note of what worked or didn't work. Then automate the working solutions to avoid human error when implementing them in th future. Use this process recursively to refine your hypothesis/results for further enhancements.