With this in mind, what are the tips and tricks the wise and experienced HN have for writing code that is easier to read by others and and the future us?
Tips I have been told to date are to use easy to remember function and variable names, to write in a way such that the code documents itself (ie minimal comments), to use quite a few relevant comments (this contrasts with the previous tip), and so on.
I’d be very grateful for your thoughts on what works well for you in your professional environments, but also the “why” aspect - why do you think your approach, that I’m sure you have fine tuned over many years, is effective.
1. Don't try to be clever. Unless there's a really good tangible tradeoff, do things the boring way. Ideally try to avoid language-specific features just to save a few keystrokes
2. Write as much stateless code as possible. Use function parameters instead of class members. If this results in too many parameters, it might be a sign that the function is too bloated
2. Keep each method at a single level of abstraction. Don't mix high-level business logic like send_email() with low-level logic like constructing a SQL query.
3. Break out separate functions just to give a block of logic a name. This results in more context when reading the code, shorter functions, and better stack traces.
4. Don't work too hard to avoid duplication. A little duplication when two codepaths do something similar is much better than having a weird abstraction that exists just for the sake of deduplicating a few lines. (But do work hard to make sure that there is a single source of truth for important values and decisions!)
5. Prefer many functions to a few, configurable functions. When you have a function with, say, 5 boolean options, that's 2^5 = 32 cases you need to support. In reality, the function might only be actually called 6 or 7 different ways, so you don't actually need all those combinations. Having more functions that do one thing makes it much easier to tell what a particular piece of code is actually doing.
6. Start by explaining what needs to be done as though you are telling another person, and then transform that into code. It tends to result in more readable code than starting by explaining it to a machine.
1. Functions can do 90% of what you need in terms of code organization and maintainability.
2. Don't try to get functions to do more than one job. Don't try to spread one job across multiple functions.
3. Name functions using concrete, unambiguous verbs. No: handleX, doX, processX, updateX. Yes: get/setX (from/to memory), fetch/sendX (over network), load/saveX (from disk), calculateX (and return value), findX (in list).
Make your modules and function flow "tell a story". Ie make it readable like reading a literal short story.
Write quality unit tests that has realistic input data that explain your thoughts how the "module" should work. The happy path test is the most important one.
2. Use your favourite diff tool. Go over the diff and make sure first that the diff is readable for people to review it. Make changes accordingly
3. Quit diff and now read the whole thing. If you altered a function, read that function from the beginning. If you added/removed a function, read the class from the beginning. Make sure all make sense
4. Finally, ask for a review and pay attention. Don’t dismiss comments withou first considering them.
That’s what I do
I'm not saying the code is perfect, but the improvement is faster than to do it in a vacuum.
Writing the tutorial and documentation for a library and show it around to colleagues before you write the code has been helpful. "Would this make sense to you?"
I also used to give non coders a laptop and the documentation for a library I wrote and tell them to follow the documentation to do something.
I try to keep in mind that the code I'm writing will eventually be maintained by someone else years from now, who never knew the context of why I'm writing this code or the function I am trying to achieve with it.
Then, no matter the impulse to leave it alone and mark it as ‘working code’, go back and edit it.
This alleviates the pressure as it’s only your first draft, followed with a discipline to go back and fix your abstractions, organization, and elegance.
Keep functions small
Organize the code so it is easy to read
Write good tests, they serve as documentation
Write a Readme file to explain the overall purpose of the code and anything not yet implemented in the code