Sunday, June 19, 2022

Basic Rules of Coding

It’s fairly easy to write code for a computer to run. But it’s incredibly hard to write code that isn’t ugly, awkward, or broken.

Most code needs to work reliably. No matter what else is happening around it, such as faults, user errors, or upgrades, the code should always do what is expected. That is an extremely hard attribute to get correct.

It is always better to get the code right the first time because it will save a lot of time overall. Patching any mess in the wild is super expensive. Some bugs are just time or money sinks.

It is preferable to reuse battle-tested code than it is to have new stuff, even if it is well-tested (which is rare). Testing and bug fixing are way more expensive than writing code. If you spend more time writing better code, you’ll spend less time firefighting it, thus saving a lot of time overall.

So there are a few very simple, high-level, rules that should be followed while coding:

First Rule: Write the code to be readable by as many people as possible. Readability always outweighs any other property. Every time.

Second Rule: Name all things correctly. Default most things to being explicit, but shorter names are usually better. Acronyms are bad, far worse if they are completely made-up.

Third Rule: Keep it all super organized. Be explicit about the organizational lines. Encapsulate it into one place, don’t fragment it all over the codebase.

Fourth Rule: Use what you have first before adding new stuff. Understand what you use really well, don’t use any stuff if you don’t understand it. Stuff that you fully understand is always better than anything else.

Fifth Rule: Edit, edit, edit. It sounds sexier to call it refactoring, but really it is just pure editing. Raw code is raw until you’ve passed through editing enough times to make it refined. The code is not done until you have spent time cleaning it up and correctly documenting it. Anything less just is just ‘in-progress’, definitely not ‘finished’.

Most programming should be slow, it is far better for quality if you take your time. However these days it is usually horrifically rushed, so it is even more critical to not waste any precious time. You don’t have enough of it. Don’t waste it doing unnecessary things, don’t waste it doing sloppy things that will hurt you later. Work smarter, not harder.