Coders at Work documents conversations with fifteen great programmers. These four coding principles came up throughout the book as universal principles that are worth adopting:
Principle 1 – Keep Code Simple
Coding as simply as possible is normally enough to solve most problems. Programs ought to make sense, there are very few inherently hard problems.
When you have a hard problem, recognize that there is probably a known solution. Once implemented, use comments to state the purpose and reference source algorithms. Always protect difficult to understand code with tests and remember it is easier to optimize correct code than to correct optimized code.
Readability of code is now first priority, judge programs by their ability to communicate with the human reader. Use a dictionary to find appropriate language. Think of programming as writing a story to express a concept to a very tired person with limited vocabulary.
Principle 2 – Think About APIs
Consider interfaces first and identify the common methods, RPCs or queries. With an API, when in doubt, leave it out.
You cannot get an API right until you have practised using it externally. Use test-first programming and refactor the APIs instead of refactoring the implementation code underneath the APIs.
See Joshua Bloch’s How to Design a Good API and Why It Matters
Principle 3 – Deliver Valuable Work
Set up projects for immediate gratification, find the first piece of success that is achievable. Build team confidence with a clear understanding of the project and tasks required to achieve success. This includes understanding who the customers are.
Rewrites rarely support business objectives. Migrate to better languages but do not expect that they will give you a one order of magnitude improvement in productivity, reliability or simplicity.
Principle 4 – Debug with Focus
Use test-driven development, static analysis and code reviews.
We will always have bugs if we stretch our capabilities. The worst bugs are multithreaded ones. When you encounter a bug, think like a scientist and patiently change one thing at a time to understand the root cause of things.