It may have happened to you that the company asked you to complete another developer code. If the company is not sensitive to the cleanliness and design of the code of its former developers, you will probably remember that experience as one of your bitter memories! Dirty code is full of bad smell everywhere! Of course, you do not encounter rotten food here!
Liskov Substitution Principle
Bad smell in programming refers to errors that later cause bigger problems in the code. When we use the term good code design, we mean code that does not smell bad (or at least tolerably smells bad)!
To achieve a fragrant code, we must follow the principles of good code design. One of the principles that we must consider to design good code is known as the SOLID principles. The principles of SOLID are:
- Single Responsibility Principle (SRP)
- Open / Closed Principle
- Liskov Substitution Principle or Liskov Substitution Principle (LSP)
- Interface Segregation Principle
- Dependency Inversion Principle
In this article, we will introduce the third principle, the Liskov Substitution Principle. The principle of substitution is known as one of the most difficult principles of SOLID, but in this article we will teach you the simplest possible way. So do not worry and be with us carefully until the end of the article!
What is the principle of Liskov’s succession?
The principle of Liskov’s succession was first introduced in 1987 by Barbara Liskoff at her conference entitled “Data Abstraction”. He later described the principle of succession in an article he co-authored with Janet Wing:
To be honest, the above definition is more like a kind of hard algebraic annoying rule! But in simple language we can put it this way:
“The objects of the parent and child classes should be able to be used interchangeably without any problems with the code,” says Lyskoff.
Why should we use the principle of substitution?
At the beginning of learning object-oriented programming, you were probably always told that whenever the relationship between two entities is of the is-A type, it is a sign that you should use Inheritance. But the fact is that this is not the case in all cases, and its regular use in some cases has problems. The principle of substitution is used to ensure that inheritance is always used correctly.
To better understand the subject, let us give a simple example. Suppose we have a rectangular class and a square class in the program. What do you think the relationship between these two classes should be like? As you know, a square is a kind of rectangle. So we have an is-A relation. But probably because you recently read in a pessimistic article like this one that the constant use of inheritance in these cases produces a bad smell, you are afraid of answering the inheritance!
But suppose a careless programmer uses inheritance to solve this problem quickly. We want to have a method that calculates the area of a shape using the length and width object of the shape object. But the problem is that the square, unlike the rectangle, only needs width to calculate the area. Now we either have to go to the rectangle class and adjust it to accept the squares excuse, or the program will face Exception because the width attribute is left in the area calculation method. By applying the first method, we have violated the Open / Closed principle in SOLID, and by applying the second method, the program has problems.
In this article, you will get acquainted with the third principle of SOLID in code design. The SOLID Principles are a set of five basic principles in proper programming practices. SOLID principles help you avoid code odors! By following these principles, you can write Clean Code. Changes in codes and their expansion are reduced by following a simple SOLID, and its financial and time costs. So you see how vital it is for a programmer to learn each of these principles. Therefore, it is better to take reading this type of articles seriously! Have you ever experienced a bad smell due to not following the principle of Lyskov’s succession ?! We are happy to read your comments!