Hugo Hacker News

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

atmosx 2021-08-16 16:02:18 +0000 UTC [ - ]

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 2021-08-16 17:23:30 +0000 UTC [ - ]

Optimizing is perhaps one of the best applications of scientific reasoning in programming since the 'natural law' in this case is how the hardware actually works as opposed to whatever wrong abstraction you start with. Sometimes it is more important to be fast so in that case you know which way to go.

muzani 2021-08-17 01:43:15 +0000 UTC [ - ]

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.

djmips 2021-08-16 17:20:41 +0000 UTC [ - ]

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.

hidden-spyder 2021-08-17 14:00:30 +0000 UTC [ - ]

Do you have any examples to further elaborate your points? I can't think about how to actually apply those and would like some help visualizing it.

agomez314 2021-08-17 11:57:22 +0000 UTC [ - ]

thanks! will start doing that.

gitgud 2021-08-17 04:29:12 +0000 UTC [ - ]

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 2021-08-16 13:56:57 +0000 UTC [ - ]

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.