So, I want to get things right (and even possibly get something done). But what is right ?
I will try to narrow my vague sense of “good code” into a few categories :
Reusable : AKA the UNIX philosophy. Keep your code modular, split large functions into atomic units, let your data flow freely. If you stumble on a good pattern/recipe, abstract it away so you can make use of it somewhere else.
Small resource usage : Fast code with a tiny memory footprint is always desirable, especially in the context of re-usability. A few examples of what that means to me:
- Coding a layer of abstraction should never mean adding overhead to a program.
- Don’t reinvent the wheel. Search the standard library until your eyes bleed, search already-made packages, but don’t lose time solving problems people have already solutions for. Well, unless your wheel is actually a quantum hypercube. Even then…
- Always profile your code to find bottlenecks and optimize where it counts, instead of trying to toy around and guess where the problem lies, or spend valuable time prematurely optimizing something only to find out you’ll have to replace it anyway.
Extendable : You will go back to your code to add features, so make it easy on yourself. Rather counter-intuitively, that means having clear boundaries on what your code does and does not do - by keeping a clear, accurate and heavily-documented model of the workings of your code so you can easily find the best place and way to implement a new feature.
As tiny as possible : The best documentation cannot help a codebase so large it has become unmaintainable. Don’t add features for which there is no actual demand, depreciate and remove features which are no longer used, refactor old code if there is a new, easier way to do it, even if it breaks things in the process. There are languages where backwards compatibility is desired, and Python is not one of them.