Notes on Coding Style
Coding is much like writing. There are many, many conventions, and following these conventions leads to cleaner, more maintainable code. But every so often thereâs a situation that calls for breaking the rules.
This lack of clarity has led to wars amongst passionate programmers. Tabs vs spaces, indents vs brackets, syntax sugar vs no syntax sugar, on and on and on.
Iâll quote fastaiâs coding style:
Everyone has strong opinions about coding style, except perhaps some very experienced coders, who have used many languages, who realize thereâs lots of different perfectly acceptable approaches.
I agree, there are many acceptable approaches. Just like how in writing there are many kinds of agreeable prose. For most programmers, itâs enough to develop a style of writing thatâs agreeable to most. Even if you see this work as a craft, if you try and cut every program to perfection then you just wonât get much done. Thereâs a tradeoff between volume and quality of output, and too often I find myself leaning too hard into one or the other.
Over the years Iâve developed a few heuristics for âgood enoughâ programming style. These are things I check and re-check for every line of code I write. Great programs wonât come about just from following these guide lines. But I have yet to see great programs that omits these guidelines.
- Favor clarity. All parts of a program â the code, comments, and context â should make it clear what the program is doing and why.
- Favor brevity. Short programs > long programs, short lines > long lines, short comments > long comments. Itâs not always the case, but shorter programs tend to be more clear. There are just fewer opportunities to screw up and confuse your reader.
- Avoid too much copy and paste. It is okay to copy and paste occsaionally, but if you abuse it you can end up with a big mess.
- Think about âlocalityâ of functions and variables. Often it makes more sense to readers when data and the functions that process those data are close to eachother in the file. Many times I have been confused by having to keep open 3+ tabs of editors. Sometimes theyâre 3+ tabs of the same file opened at different functions in a 10k+ line mess. Avoid this.
- Donât repeat yourself. I have seem comments repeat logging statements repeat code. Donât do this.
- Too few comments and your program is obscure. Too many comments and I canât tell whatâs being excuted.
- My rule of thumb has been to never let comments explain what is happening in the code. If you canât tell whatâs happening by reading the code, then re-write it.
- Reserve comments for explaining why something is the way it is. In other words, to provide context.
- Rewrite, rewrite, rewrite, rewrite! Just like in prose, the first draft should not be the last. You learn so much in the process of writing that oftentimes a rewrite is necessary to incorporate the new, better ideas learned. It is okay (albeit very painful) to scrap thousands of lines. Do it more often.