Ask HN: What are your best resources for writing clean code?
Hey,
I'm in the process (after 4 years of BSc and 2 years of MSc in SE) to get serious about writing good, proper, clean code.
Colleagues have recommended Clean Code: A Handbook of Agile Software Craftsmanship and I'm very tempted to try James Koppel's coaching.
What books/resources/courses have really helped you to step up your code game?
13 comments
[ 3.7 ms ] story [ 43.3 ms ] threadThat said, The Pragmatic Programmer is my favorite resource for it. https://refactoring.guru/ is also a nice reference point, in an easy problem-solution format.
Clean code is great, and it's definitely someone's weakness, but it doesn't sound like yours. Find out what's making you procrastinate or hesitate, and focus on that instead.
If you're procrastinating because your code is a mess, then you probably want to learn clean coding. But at some point it will have diminishing gains, like learning to type fast.
The little things that have greatly improved my productivity recently: ES6 literacy, zsh aliasing, knowing array methods by heart, IDE shortcuts, CSS animation.
Ideal productivity is not at 100% clean code & architecture, but somewhere between 40%-80%, I'd say. The more confident you are of a feature and the more confident you are in the architecture, the more you can invest in it.
There's an anti-pattern on refactoring as well. Many times you want to put a constant or config in the code itself. Sometimes you even want to ignore DRY and copy code. I also use god objects as a dumping ground for methods and then refactor parts of them into a class much later, when a pattern emerges.
Things like Clean Code are a bit too dogmatic for my tastes now, there are some reasonable ideas there but it's tempting to apply them where they don't fit.
What helped me was starting my own large project [0] based off porting another codebase. I started with wanting to follow Clean Code practices to the letter but now, 2/3 years later and with the benefit of hindsight I'm increasingly moving away from a lot of the Clean Code 'rules' (short methods, etc).
But I also write other short lived projects which allow me to try different things out and learn from my mistakes. When you think how we learn as children a lot is based on making mistakes and I think that is what's missing from a lot of how programming is taught.
[0]: https://github.com/UglyToad/PdfPig
Having said that though, it's a good primer in what's possible in writing clean and testable code.
Functional programming, with its functions without side effects, has taught me how to better isolate the functionality in OOP code.
Lisp with its "code is data, data is code" has taught me how to create data that can be used to generate the correct code that had lots of duplication.
Years of working on legacy code has taught me the difference between useful and confusing abstraction, and what kind of code is more maintenable.
As @UglyToad has exposed "write code, lots of it".
[0] https://interpreterbook.com [1] https://compilerbook.com
While the books are not strictly about clean code, there's definitely a focus on it within the books.
If you do it in a separate change/commit, it should be welcome.
I am not talking of refactoring: that is a completely different story, and requires extensive testing, long reviews...