Software Design Principles Every Programmer Should Know | by Peter Lee | Medium

Software Design Principles Every Programmer Should Know

What are Software Design Principles? Why we should apply Software Design Principles? This article will answer that.

This article will cover some principles, design patterns that will help developers make a good system design.

After reading this article, you can understand things:

  • Why are Software Design Principles important?
  • SOLID principles
  • Other principles (DRY, KISS, YAGNI)
  • Some notes for developers

What are Software Design Principles?

In the development process, the time for writing code will only consume from 20 to 40 percent, remaining we will read code and maintain the system. So, Making a good system design is very important. In my opinion, a good system should have a good code base: easy to read, easy to understand, easy to maintain(add/modify feature and fix bugs), and easy to extend the system in the future. That will reduce development time, resources, and make us happy more.

Basically, Software Design is only a part of the development process in the Design Step(Please take a look at the below image about the development process). Before we do Software Design(low-level) we have to complete Software Architecture (high-level). Choosing an architecture will determine how to deal with performance, fault tolerance, scalability, and reliability. Software Design is responsible for code level(low-level) such as, what each module is doing, class scope, methods purposes, and so on. You can imagine like when you build a house or a building.

Development Process. Source: Internet

Why are Software Design Principles important?

Have you ever read the code of frameworks such as Laravel or Spring Framework? Reading the code of frameworks is a way to improve your skills and could very difficult understanding because they applied many design patterns: Decorator Pattern, Strategy Pattern, and so on. These frameworks applied Software Design principles wisely. I have worked with the Laravel 5 and Spring framework and I saw both frameworks use the Dependency Inversion Principle (IoC Container, Dependency Injection) for their core.

Besides, if you want to contribute to open-source which often uses a lot of Design Patterns: Singleton, Factory Method, Decorator Pattern, Strategy Pattern, Proxy, and so on. I think you have to understand those patterns then you can contribute to it. Moreover, if you can want to create a framework like Lavarel Framework(Taylor Otwell), I think you should deeply understanding Software Design Principles and Design Patterns. Learning Design Pattern isn’t difficult, you can search on the internet or watch them on youtube but identify and apply them in the real project is very difficult. You can achieve it after working with many projects in many years.

Basically, Sofware Design Principles are Object-Oriented Design Principles which based on OOP. We are very familiar with OOP. So, I won’t write about it. Besides, We have a lot of paradigms such as Procedural Programming, Functional Programming, Reactive Programming, Aspect-Oriented Programming, and so on. Please take a look at below image:

OO Design Principles

As far as I know, OOP Design Principles has 10 principles. In my opinion, we should understand the 8/10 principles that are enough. We can split these principles by group them in types:

  • SOLID Principles
  • DRY (Don’t repeat yourself) Principle
  • KISS (Keep it simple, stupid!!) Principle
  • YAGNI (You ain’t gonna need it) Principle

SOLID Principles

SOLID principles. Source: dev.to

Single Reponsitibily Principle

“A class should have one and only one reason to change, meaning that a class should have only one job.”

Single Responsibility Principle (SRP)

I have worked on a lot of projects in many years. In my experience, The Helper/Utility class often violates this principle. You should avoid them. To guarantee your code complies with this principle you can use the Decorator Pattern.

Open/Closed Principle

“Objects or entities should be open for extension but closed for modification.”

Open/Closed Principle (OCP)

This principle is applied widely in everywhere. For instance, you can imagine to Plugin Architecture in IntelliJ IDEA/Visual Studio Code/Eclipse: easy to add/remove a feature with an available plugin. Example: Lombok and Docker plugin in IntelliJ IDEA. To guarantee your code complies with this principle you can use the Strategy Pattern.

Liskov Substitution Principle

“Objects in a program should be replaceable with instances of their subtypes without altering the correctness of that program.”

Liskov Substitution Principle (LSP)

This principle states that objects of a parent’s class should be replaceable with objects of the child’s class without breaking the logic of the application. That requires the objects of your child’s class to behave in the same way as the objects of your parent’s class.

Interface Segregation Principle

“Many client-specific interfaces are better than one general-purpose interface.”

Interface Segregation Principle (ISP)

Like the Single Responsibility Principle(SRP), the target of the Interface Segregation Principle is to reduce the side effects and frequency of required changes by splitting the code into multiple/independent parts.

Dependency Inversion Principle

Entities must depend on abstractions, not on concretions. It states that the high-level module must not depend on the low-level module, but they should depend on abstractions.

Dependency Inversion Principle (DIP)

This principle is difficult to understanding but it very close to people. If you are using Spring Framework then you are using it. Some people can confuse between Dependency Inversion Principle and Dependency Injection (DI). Please see the below image:

Dependency Inversion Principle(DIP) vs Inversion Of Control (IoC) vs Dependency Injection(DI)

Martin Fowler called Inversion Of Control is a principle(or Hollywood principle) but I think it’s only a Design Pattern. We have many solutions to guarantee code abide by with IoC: Dependency Injection(DI), Service Locator, and so on.

Other principles (DRY, KISS, YAGNI)

This principle states that each small pieces of knowledge (code) may only occur exactly once in the entire system.

KISS (Keep it simple, stupid!)

This principle states that try to keep each small piece of software simple and unnecessary complexity should be avoided.

YAGNI (You ain’t gonna need it)

This principle states that always implement things when you actually need them never implements things before you need them.

Some notes for developers

  • You should try to identify and apply the Design Pattern to your work.
  • After reading this article, you should find the issues in your project and solve them.

Finally, “Talk is cheap. Show me the code.” Linus Torvalds. In the next article, I will show you my code.

References