
Over the years of teaching and commercial development, I met many students and developers who do not fully understand the meaning of the word abstraction. From numerous attempts to clarify the meaning of this term, this article has turned out. What you will find under the cut:
- The definition of the concept of abstraction and explanation of where it came from in the PLO.
- Explanation of simple examples of what the barrier of abstraction is, a side effect of abstraction.
- How does the hardcode.
What is abstraction?
Wikipedia defines abstraction and the process of abstraction as follows:
Abstraction (from the Latin. Abstractio - distraction) - a distraction in the process of cognition from non-essential aspects, properties, connections of an object (object or phenomenon) in order to highlight their essential, regular features; abstraction; theoretical generalization as a result of such a distraction.
In European philosophy and logic, abstraction is interpreted as a method of phased production of concepts that form more and more general models - a hierarchy of abstractions. The most developed system of abstractions has mathematics. The degree of abstraction of the concept under discussion is called the level of abstraction. Depending on the goals and objectives, you can talk about the same object at different levels of abstraction.')
Grady Booch defines the concept of abstraction much simpler, but the meaning is the same:
Abstraction highlights the essential characteristics of an object, distinguishing it from all other objects.Why do we need abstraction?
Abstractions perform a protective function and help us not to go crazy with an excess of information. Imagine how we would live if, when writing with a ballpoint pen, we had to think about the fact that billions of ink molecules interact with paper molecules to make a letter. In other words, without wasting time on unnecessary details, we can grasp the very essence - look at the problem “from above”.

If it were not for a bird's-eye photograph, could one have imagined how correctly Barcelona was designed? By the way, about the example with a ballpoint pen, it would not have been possible to read either - the outline of the same letter in the letter differs even from one person.
Abstract thinking is a mechanism that allows us to process and assimilate a bunch of new information. If there were no abstraction, then the only option for us is to remain very primitive creatures.

In Brazil, the tribe lives a small tribe of Indians
Pirah . Representatives of this nationality possess extremely poor abstract thinking. Their alphabet consists of three vowels and eight consonants. They have no words for numbers, instead of numbers they have two concepts - a few and a lot. They have no colors - only the concepts of light and dark, time and history - they live only in the present day and remember only that. what the oldest living thing remembers; no - religion, crafts, art. They still have a lot of things in our usual understanding. And this is all because abstract thinking is not developed. True, in fairness, it is worth noting that while they themselves feel absolutely happy people!
So, we need abstraction as a way of knowing and describing the world around us, in order to exchange information with each other. Abstractions allow you to
decompose the subject area into a set of concepts and relationships between them.

The picture shows Legoland in London. Despite the fact that all items are collected from the children's designer, we can easily recognize in them houses, windows, doors, city blocks, people.
Barriers and side effects of abstractions
To understand the key properties of abstractions, let us draw an analogy with the construction of projections on the plane.

Suppose that we have three figures: a ball, a cylinder and a parallelepiped, while the axis of symmetry of the cylinder passing through the centers of the circles at the base is parallel to some axis of symmetry of the parallelepiped. Obviously, you can choose two planes to construct projections in such a way that the ball and the cylinder are projected in a circle, and the cylinder and the parallelepiped in rectangles.
The projection in our example illustrates the abstraction of an object - a geometric figure. What we see is that on the same plane you cannot distinguish the projections of a ball and a cylinder, and on the other, a cylinder and a parallelepiped. This effect is called a
barrier of abstraction . Abstraction does not represent the entire object as a whole, but only its essential set of characteristics. It is necessary to be prepared for the fact that some very unlike objects can become indistinguishable. If this is inconvenient, then you need to choose a different set of abstractions.
On the other hand, as we see from the example, a cylinder can be projected both into a circle and into a rectangle — objects with different geometric properties different from those that the cylinder has. The fact that an abstraction has its own properties that are different from the properties of an abstracted object is called a
side effect of abstraction .
On the very first picture two figures are collected, collected from chips, so that with a certain lighting they cast “human shadows”. For example, it seems to me that there one silhouette is male and the other is female. This is also a side effect of abstractions. Now we can classify all the shapes by their shadow.
Abstraction examples
Scope of application | Abstraction | Comment |
---|
Whole numbers | The number from the ring Zp, where p = 2 ^ digit (8, 16, 32, 64 bits) | This abstraction allows you to represent integers only from the segment –p / 2 + 1 to p / 2. A side effect is the overflow problem. |
Real Numbers | Floating point numbers | The real numbers are uncountable, and the floating point numbers are just finite. This means that an uncountable number of real numbers are represented by one floating point number. A side effect is a rounding error, due to which two numbers cannot be compared using the comparison operation, but only modulo some small epsilon | ab | <epsilon => a == b, or a / b * 1000 can be very different from a * 1000 / b. There was even an entire discipline in mathematics — numerical methods that study how to organize floating-point calculations so that the results do not differ much from calculations with real numbers. |
Money | Floating point numbers | The error in rounding floating-point numbers makes, if not impossible, their use for financial transactions, then at least makes life very difficult. In any case, I would first think in the direction of writing a separate class for monetary units. |
Picture | Computer graphics | Computer graphics is developing by leaps and bounds to make the image on the computer screen all the more realistic. |
Software | Procedure | Procedure is a basic element of decomposition in procedural programming. A side effect is the procedure of a strictly specified sequence of commands that cannot be changed without rewriting the procedure itself. |
Software | Class | We will talk about classes below. |
Subject area | Abstraction of the entity and the relationship between entities | A side effect - reflects perceptions, delusions, prejudices, etc. about the subject area of ​​a particular subject. |
Business logic | Procedure | As mentioned above, a side effect of the procedure is a rigid sequence of commands. Business logic is subject to change, as a rule it contains many exceptions, which users usually forget to tell. Attempting to present a business transaction in the form of a procedure often fails. |
Software | Thread for parallelizing operations | Multi-threaded programming is so difficult to understand that few people understand it. |
A square is a rectangle in which all sides are equal. | The class square cannot be inherited from a rectangle. | Classes are abstractions. They have their own properties that are different from mathematical objects and which make inheritance impossible. |
Classes
Grady Butch defines OOP like this:
Object-oriented programming is a programming methodology based on the representation of a program as a set of objects, each of which is an instance of a particular class, and the classes form an inheritance hierarchy.In this definition, the most important point is the hierarchy of inheritance. Because inheritance distinguishes OOP from all other methodologies.
The two basic principles of human thinking are grouping and generalization. Classes are essentially abstractions of the mechanisms of grouping and generalizing the human brain. Naturally, with its side effects and barrier. At the same time, grouping is achieved by associating one class with similar objects, and generalization in OOP is achieved due to the class hierarchy. Class hierarchies are implemented through polymorphism.
OOP, by the way, is even more interesting because it is perhaps the last programming paradigm at the moment, which is supported at the hardware level.
The main side effect of classes is that they reflect the experience, stereotypes, prejudices of the programmer who wrote them. It follows that different people will receive a different set of classes for the same task. Moreover, the same person, solving the same task, but at different points in time, will receive a different set of classes, simply because his life experience changes.
The second side effect, which is worth noting - someone else's code is always less clear than its own.

We will understand why this happens. When a person writes code, it is more natural for him to move from bottom to top - from lower-level components to higher-level components. First they wrote one class, then the second, which depends on the first, then the third, which depends on the first and second, the fourth on the third, and so on.
When a person tries to understand someone else's code, he just moves the other way - from top to bottom. That is, it first understands the general essence, then breaks it into components, then tries to understand the essence of each component, etc. Often these movements of thought from bottom to top and from top to bottom do not match for different people. Naturally, it would be easier for the student to learn someone else's code if the breakdown of the code into components coincided with his own convictions, how to do it. If this is not the case, it will be necessary to expend some effort to understand the train of thought of the developer. Therefore, when someone says that there is a “full hardcode” here, but if I rewrite it, everything will be simpler and clearer. This is always 100% true ... But only for him, for the rest, the value of rewriting is no longer so obvious.
By the way, if you do nothing on purpose, then when developing from the bottom up, the code becomes strongly interconnected, that is, not reusable. To overcome this effect, you must follow the principle of dependency inversion (The Dependency Inversion Principle).
We illustrate how the described side effect is manifested in a simple example. Many residents of large cities are purchased in large supermarkets. Suppose that a wife sends her husband to shop and, so that he does not forget, as usual, something, makes a list "for those who are in the tank."
We will try to follow the course of her thoughts:
“So why am I going to cook for dinner tonight?”
- We need to cook something delicious to pamper the child.
- So, it will need flour, milk.
- It seems the batteries are exhausted in the mixer.
- Stop! A child needs vitamins. Carrot. I will make carrot juice. and tangerines. Soon the New Year!
- Do you have bread at home? No, it seems, no. So, you need to buy!
- Still need to buy oil.
- I forgot about the child - vitamins. Buy apples.
- Something pen writes poorly. Probably run out of ink soon. Need to buy!
- So, the child should buy juice.
- And also a toy - let her rejoice.
- Do we have potatoes on borscht? The soup is enough, but not for a week. So, too, need to buy.
- I almost forgot the teacher asked to bring two notebooks.
- To borsch need sour cream.
- It seems the sugar is over.
- The baby loves grapes.
- And still need to buy bottled water.
As a result, we obtain the following list:
- flour
- milk
- batteries
- carrot
- tangerines
- bread
- butter
- apples
- a pen
- the juice
- a toy
- potatoes
- notebooks
- sour cream
- sugar
- grapes
- water
When does a husband come to the store what he discovers? The listed items are in different parts of the store. Usually the list is long, so remembering something that has already been bought is quite difficult. It is superimposed on this, that some departments are temporarily closed - goods are being unloaded, some goods are not on sale, plus crowding, winter clothing. More experienced comrades go with a pencil or pen with a very anxious look and constantly look at their list. But in the end, all the same, you forget to buy something. From my own experience, I can say that this “something” will turn out to be the most important, which is why it was generally worth going to the store.
Which list would be convenient for my husband? The one in which all products are grouped by department, separate groups go in the queue, corresponding to the order of the store. For example, for a store in which I go, it would be convenient to group the goods as follows:
- Batteries
- Children's notebooks
- A pen
- Water
- The juice
- Sugar
- Carrot
- Oranges
- Apples
- Grapes
- Potatoes
- Butter
- Bread
- Milk
- Sour cream
- Flour
- Children's toy
Another important observation is that it is impossible to determine from the abstractions themselves how successful they are. This can be done only if we try to use them in practice. And here it turns out that some abstractions are better suited for the task, while others are worse. And if you slightly change the initial conditions, then the former “good” set of abstractions may not work anymore. For example, the second shopping list from the example will stop working if you come with it to another store with a different order of display of goods. He will be no better than the first.
Hence the conclusion - it is impossible to come up with a set of classes that is suitable for all occasions. In the article The Open-Closed Principle, this is called strategic closure.
The natural question is how to create good abstractions right away. Alas, there is no exact answer to this account. But over time, a set of practices has emerged that says how to act, and promises that in this case there will be a good result. These practices include refactoring, coding standards, code review,
object gymnastics , etc. The purpose of these practices is to direct the thinking of the development team in one direction, then the chances that someone else's code will be clearer will become greater. The attitude to each of the practices of a single person depends only on the experience of using the practice acquired by him. Often the words "It does not work" should be interpreted as "I tried it - I did not succeed." There are no objective arguments “FOR”, as well as “AGAINST”.
So why then do you need OOP?
Draw parallels between natural language and the PLO
natural language | OOP |
---|
Word | the class |
rules | Syntax |
Genre | Architecture |
literary receptions | patterns |
The person expresses his thoughts in words of natural language. There are two types of tasks:
- To solve, you need to know the language itself. For example, to write War and Peace.
- The complexity does not depend on the language. No matter how much and what languages ​​you know. It does not help in the decision. For example, the Fermat theorem.
OOP is a tool that was created with an eye on large-sized programs. But, this is just one of the tools that it takes to write a large project.
I am always amazed, the articles in the style Why I love X or Why I do not like X. Everyone understands that X is a tool. After all, there are no such articles about the shovel. Although, who knows, the PLO has existed for several decades, and a shovel several thousand, and perhaps somewhere in the Stone Age there were fierce holivars on the topic of what is better than a mammoth blade or a hoe made of stone?
Related Literature
1.
Grady Booch Object-Oriented Analysis and Design with Examples of C ++ ApplicationsThis book needs no introduction. One of the most cited books on programming.
2.
Barbara Minto Principles of the Pyramid of Minto.Working as a McKinsey consultant, Barbara Minto created her own method of writing analytic documents based on how a person perceives information. In the book a lot of space is given to the principles of grouping and generalization.
3. Robert Martin (Uncle Bob)
Robert Martin has written a series of articles and books about OOP principles. The most famous of them are
SOLI.D. In Russian, the book was published by
Robert Martin. Quick Software Development. Principles, examples, practices in which these principles are described. But in my opinion, it’s better to read about them in the following articles:
The Single Responsibility Principle ,
The Substitution Principle ,
The Interface Respecting Principle ,
The Dependency Inversion Principle .