Insights from a Veteran Programmer on Coding Principles
Written on
Chapter 1: Principles of Programming
Recently, I had an enlightening discussion with a retired programmer who reminisced about his coding journey. He pointed out that many contemporary programmers focus solely on writing code, often neglecting the foundational principles that underlie effective programming. This oversight can lead to code that is challenging to comprehend. He kindly shared the guiding principles that shaped his approach to programming.
- Only comment when absolutely necessary. Avoid cluttering your code with comments for every line; instead, focus on explaining the purpose of the file at its beginning. If a file has multiple purposes, consider splitting it into separate files.
- This leads us to the first principle of SOLID: Single Responsibility. Each module, class, and function should have a singular focus, performing one task to minimize unintended consequences. This practice enhances code readability and maintainability.
- Strive for extensibility in your code. Languages like Ruby and Elixir facilitate this, but you can achieve extensibility in any language by adding new code rather than altering existing code. This approach preserves the integrity of the original code while allowing for new functionalities.
- Create clear abstractions with simple interfaces, enabling easy substitution of components without requiring extensive rewrites.
- It’s more advantageous to have numerous implementations that utilize straightforward, reusable code rather than a single complex implementation riddled with exceptions. Keep your code generic and simple; build specific cases as extensions without altering the original code.
- Aim to write only essential code. Extra lines or unnecessary implementations can complicate maintenance. Remember that all code requires upkeep, and more code means more testing and maintenance.
- Adhere to the DRY principle: Don’t Repeat Yourself. Avoid excessive copying and pasting; instead, seek opportunities to generalize your code or encapsulate functionality within methods.
- Embrace simplicity (KISS). If your code needs extensive comments for clarity, it may signal a design flaw. Simplifying your code can often resolve this issue.
- Invest time in thorough testing. Schedule automated tests to ensure your code functions correctly. Avoid ad-hoc testing methods; instead, leverage established automated testing systems to streamline your workflow.
- Refactoring should only occur alongside a version control system like Git. This practice allows you to create a separate branch for testing changes without impacting stable code, enabling easy rollback if necessary.
- Allow your code to function as intended. It may seem obvious, but it’s easy to get caught up in aesthetics and forget that the primary goal is functionality.
Always remember that your code is meant for other programmers to read, not just machines. It should be flexible and open to modifications in the future.
The first video titled "Why Most Programmers Are Broke & How You Can Avoid It (LIVE)" discusses common pitfalls in programming careers and offers strategies to overcome them.
In the second video, "7 Signs Of A Bad Programmer | Prime Reacts," various characteristics of ineffective programmers are examined, providing valuable insights for self-assessment and improvement.
More insights can be found at PlainEnglish.io. Sign up for our free weekly newsletter, and stay connected with us on Twitter and LinkedIn. Join our Community Discord and explore our Talent Collective.