Coding Best Practices

Exploring Clean Coding and Best Practices, I put together a diagram to consolidate key principles in one place. Given the broad scope, I focused on a select few, mainly “SOLID Principles”.

Since I had the diagram ready, I created a short video to explain the illustration. The commentary notes are included below.

Welcome to this Developer Blog, where we take a quick look at some Coding Best Practices. Considerations and tips for your next project. There are a number of methods, we will only look at a few. More of an overview then a deep dive.

A common one is the “SOLID Principles”, this is broken down in to five key areas.

Single Responsibility – Keep classes focused and maintainable
Open/Closed – Extend functionality without modifying existing code
Liskov Substitution – Ensure subclasses work seamlessly with base types
Interface Segregation – Build lean, efficient interfaces
Dependency Inversion – Promote abstraction over tight coupling

Single Responsibility Principle”.  A class should have only one reason to change. Keep classes small and have one business need, single responsibility.

Open / Closed Principle”, classes should be open for extension but closed for modification. This ties in to Polymorphism, Instead of modifying existing code, new classes can extend functionality while keeping the original code unchanged. via subclasses or implementations of an interface.

Liskov Substitution Principle”, this forces you to think about designing classes and data structures properly. Subtypes must be interchangeable with their base types without breaking functionality. Can the lower classes be substitutable for a higher-level ones and still work. while maintaining the expected behaviour.

Interface Segregation Principle”, keeping interfaces streamlined. Rather than designing large, cumbersome interfaces, break them down into smaller, well-defined ones that cater to specific needs. This improves modularity and ensures that clients only depend on the functionality they require.

Dependency Inversion Principle”, High-level modules should not depend on low-level modules – both should depend on abstractions. For example if you have a connection make the connection values first and then pass it to the class.

These principles improve a number of areas of development, as highlighted in the centre. They contribute to both planning and coding efficiency.

  • Enhances Maintainability
  • Flexibility for Future Changes
  • Improves Readability
  • Improves Scalability
  • Minimizes Bugs
  • Modular and Reusable Code
  • Reduces Code Duplication
  • Reduces Coupling
  • Simplifies Testing

There are a number of other principles and rules, you could follow too. Some of these are shown on the right.

Law of Demeter”, suggests that an object should only interact with Itself (its own methods). Objects passed to it as arguments. Objects it directly creates. Its own instance variables. This “Reduces Coupling”, objects remain independent, making changes easier.

The “Dry” (Don’t Repeat Yourself) principle is a simple and good building block for best practice. Aimed at reducing redundancy and improving maintainability.

Ensuring that each element of knowledge or logic within a system is represented in a single, clear, and definitive manner, avoiding redundancy or ambiguity.

Avoid Magic Numbers and Strings”, this is another simple and easily overlooked. These are hardcoded values that lack context, making code difficult to understand and modify. Use constants, enums and config files to overcome this.

The last one here is “Guard Clauses”, this is to simplify conditional logic, by handling edge cases early in a function, reducing nested if statements and improving readability.

There are a number of other principles, that are worth a look to, but I have not covered them here on this diagram. Like “You Aren’t Gonna Need It” and “Keep It Simple”.

Another key area of “Best Practice” is coding style and rules, it could be simple as following guide line set by the development team, or just “Naming Conventions”. And of course there is documentation and test plans. Whatever style, practice you use you can always build on it, and improve it each time.

Write clean, scalable, and maintainable code – keep it simple, structured, and smart!

Leave a comment

Projects and Challenges

List of all Projects can be found here.

Dev Video Blog

Exploring Programming Languages.

ShortCutHex

Website Screen Capture, App with Rust.

SpellHex Syllables

Dictionary by vowel sounds and number of Syllables, App with Ruby.

Retro-Style Platformer

Creating a simple Platformer with Godot.

Coding Best Practices

5-Minute Guide on Coding Principles.

Posts