Quirks of abstraction

  • Tutorial
image

Over the years of teaching and commercial development, I have met many students and developers who do not fully understand the meaning of the word abstraction. Of the many attempts to clarify the meaning of this term, this article has turned out. What you will find under the cut:
  • The definition of abstraction and an explanation of where it came from in OOP.
  • The explanation with simple examples of what is a barrier of abstraction, a side effect of abstraction.
  • How is the hardcode obtained?


What is abstraction?


Wikipedia defines abstraction and the process of abstraction as follows:
Abstraction (from lat. Abstractio - distraction) - distraction in the process of cognition from non-essential sides, properties, connections of an object (object or phenomenon) in order to highlight their essential, regular signs; abstraction; theoretical generalization as a result of such a distraction.

In European philosophy and logic, abstraction is interpreted as a way of phased production of concepts that form more and more general models - a hierarchy of abstractions. The most developed system of abstractions is 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 Butchdefines the concept of abstraction much simpler, but the meaning is the same:
Abstraction highlights the essential characteristics of an object that distinguish it from all other objects.

Why do we need abstraction?


Abstractions perform a protective function and help us not to lose our minds from an overabundance of information. Imagine how we would live if, when writing with a ballpoint pen, we would have to think 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 essence - look at the problem “from above”.

If it weren’t for a bird's-eye view, would one have imagined how correctly designed Barcelona was? By the way, about the example with a ballpoint pen, reading would not work either - the styles of the same letter on the letter differ even for 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 have extremely meager abstract thinking. Their alphabet consists of three vowels and eight consonants. They have no words denoting numbers, instead of numbers they have two concepts - several and many. They have no colors - only the concepts of light and dark, time and history - they live only today and remember only that. what the oldest living person remembers; no - religion, crafts, art. They still have a lot of things in our usual understanding. And all this because abstract thinking is not developed. True, in fairness, it is worth noting that at the same time they feel themselves absolutely happy people!

So, we need abstraction as a way of knowing and describing the world around us, for exchanging information with each other. Abstractions allow decompositionsubject area on a set of concepts and the relationships between them.

The picture shows Legoland in London. Despite the fact that all the items are collected from a 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, we draw an analogy with the construction of projections onto a plane.

Suppose that we have three figures: a ball, a cylinder, and a box, while the axis of symmetry of the cylinder passing through the centers of circles at the base is parallel to some axis of symmetry of the box. Obviously, you can choose two planes for constructing projections in such a way that the ball and cylinder are projected in a circle, and the cylinder and parallelepiped are projected into rectangles.

The projection in our example illustrates the abstraction of an object - a geometric figure. What we see is that on one 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 the abstraction barrier.. Abstraction does not represent the whole object, but only its essential set of characteristics. One must be prepared for the fact that some very dissimilar 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 in a circle and in a rectangle - objects with different geometric properties that are different from those of the cylinder. The presence of abstractions of their own properties that are different from the properties of the abstracted object is called a side effect of abstraction .

The very first picture shows two figures collected from slivers, so that under certain lighting conditions they cast “human shadows”. For example, it seems to me that one silhouette is male and the other female. This is also a side effect of abstractions. Now we can classify all the shapes by their shadow.

Abstraction Examples


Scope of applicationAbstractionA comment
Whole numbersThe number from the ring Zp, where p = 2 ^ bit capacity (8, 16, 32, 64 bits)This abstraction allows us to represent integers only from the interval –p / 2 + 1 to p / 2. A side effect is the overflow problem.
Real numbersFloating point numbersReal numbers are uncountable, and floating-point numbers are just finite. This means that uncountable numbers of real numbers are represented by a single 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. Even a whole discipline has appeared in mathematics - numerical methods, which studies how to organize floating-point calculations so that the results do not differ much from calculations with real numbers.
MoneyFloating point numbersThe rounding error of floating-point numbers makes, if not impossible to use them for financial transactions, then at least greatly complicates life. In any case, I would first think of writing a separate class for monetary units.
PictureMachine graphicsComputer graphics are developing by leaps and bounds to make the image on the computer screen more and more realistic.
SoftwareProcedureA procedure is a basic element of decomposition in procedural programming. A side effect is a procedure with a rigidly defined sequence of commands that cannot be changed without rewriting the procedure itself.
SoftwareClassWe will talk about classes below.
Subject areaThe abstraction of essence and the relationship between entitiesSide effect - reflects perception, misconceptions, prejudices, etc. about the subject area of ​​a particular subject.
Business logicProcedureAs 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 talk about. Attempting to present a business transaction as a procedure often fails.
SoftwareThread for parallelization operationsMultithreaded programming has turned out to be so difficult to understand that few people understand it.
A square is a rectangle with all sides equal.The square class cannot be inherited from the 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 in this way:
Object-oriented programming is a programming methodology based on representing a program as a collection of objects, each of which is an instance of a particular class, and classes form an inheritance hierarchy.

In this definition, the most important point is the inheritance hierarchy. Because it is inheritance that distinguishes OOP from all other methodologies.

The two basic principles of human thinking are grouping and generalization. Classes are, in fact, abstractions of the mechanisms of grouping and generalization of the human brain. Naturally, with its side effects and barrier. At the same time, grouping is achieved by matching one class with similar objects, and generalization in OOP is achieved due to the hierarchy of classes. 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, and prejudices of the programmer who wrote them. It follows that different people will get a different set of classes for the same task. Moreover, the same person, solving the same problem, but at different points in time, will receive a different set of classes, simply because his life experience is changing.

The second side effect worth noting is that someone else’s code is always less clear than your 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 ones. First they wrote one class, then the second, which depends on the first, then the third, which envy the first and second, the fourth - on the third, etc.

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 down into components, then tries to understand the essence of each component, etc. Often these movements of thought from bottom to top and top to bottom do not coincide among different people. Naturally, it would be easier for a student to learn someone else’s code if the breakdown of the code into components coincided with his own convictions about how to do it. If this is not the case, it will take some effort to understand the train of thought of the developer. Therefore, when someone says that here is “full hardcode”, but if I rewrite it will be easier and more understandable. This is always 100% true ... But only for him, for the rest, the value of rewriting is not so obvious.

By the way, if nothing is done on purpose, then when developing from the bottom up, the code becomes strongly interconnected, that is, not reusable. To overcome this effect, one must follow the principle of the Dependency Inversion Principle.

We illustrate how the described side effect manifests itself in a simple example. Many residents of large cities are purchased in large supermarkets. Suppose that a wife sends her husband out for shopping and, so that he does not forget, as usual, anything, compiles a list “for those who are in the tank”.
We will try to follow the train of her thoughts:
“So what will I cook for dinner today?”
- You need to cook something tasty to pamper the child.
- So, you will need flour, milk.
- It seems the batteries are low in the mixer.
- Stop! The child needs vitamins. Carrot. I will make carrot juice. and tangerines. New Year is coming soon!
- Do you have bread at home? No, it seems not. So, you have to buy!
- Still need to buy oil.
- I forgot about the baby - vitamins. Buy apples.
- Something the pen does not write well. Probably ink will run out soon. Need to buy!
- So, the child needs to buy juice.
- And also a toy - let him rejoice.
- Do we have potatoes on borsch? Enough for borsch, but not for a week. So you also need to buy.
- I almost forgot the teacher asked me to bring two notebooks.
- You need sour cream for borsch.
- Like sugar is over.
- The child loves grapes.
- And still it is necessary to buy bottled water.

As a result, we get the following list:
  • flour
  • milk
  • batteries
  • carrot
  • tangerines
  • bread
  • oil
  • apples
  • a pen
  • the juice
  • a toy
  • potatoes
  • notebooks
  • sour cream
  • sugar
  • grape
  • water


When a husband comes to the store what does he discover? The products listed are in different parts of the store. Usually the list is long, so remembering something that has already been bought is difficult enough. It is imposed on this that some departments are temporarily closed - goods are being unloaded, some goods are not on sale, plus crushing, winter clothes. More experienced comrades go with a pencil or pen with a very preoccupied look and constantly look at their list. But, in the end, anyway, forget something to buy. From my own experience I can say that this “something” will be the most important, which is why it was worth going to the store.

Which list would be convenient for her husband? One in which all products are grouped by department, individual groups are in the order corresponding to the store bypass order. For example, for the store I go to, it would be convenient to group the goods as follows:
  • Batteries
  • Children's notebooks
  • A pen
  • Water
  • The juice
  • Sugar
  • Carrot
  • Oranges
  • The apples
  • Grape
  • Potatoes
  • Oil
  • Bread
  • Milk
  • Sour cream
  • Flour
  • Children toy


Another important observation is that it is impossible to determine how successful they were from the abstractions themselves. This can only be done if we try to use them in practice. And then it turns out that some abstractions are better suited for the task, while others are worse. And if you change the initial conditions a little, then the previous “good” set of abstractions may no longer work. For example, the second shopping list from the example will stop working if you go 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. The Open-Closed Principle article calls this strategic closure.

The natural question is how to immediately create good abstractions. Alas, there is no exact answer to this. But over time, a set of practices has developed that tells you what to do and promises that in this case there will be a good result. Such practices include refactoring, coding standards, code review, object gymnastics , etc. The purpose of these practices is to direct the train of thought of a group of developers in one direction, then there will be more chances that someone else’s code will be more understandable. The attitude to each of the practices of an individual person depends only on the experience gained in using the practice. Often the words "This does not work" must be interpreted as "I tried - I did not succeed." There are no objective arguments for, as well as against.

So why do you need OOP then?


Let's draw parallels between natural language and OOP
natural languageOOP
Wordthe class
rulesSyntax
GenreArchitecture
literary techniquespatterns


A person expresses any thoughts in the words of a natural language. There are two types of tasks:
  1. To solve it, you need to know the language itself. For example, to write War and Peace.
  2. Complexity is language independent. No matter how many and what languages ​​you know. This does not help in any way. For example, Fermat’s theorem.


OOP is a tool that was created with an eye to large-sized programs. But, this is just one of the tools that will be required to write a large project.

I am always surprised by articles in the style of Why I love X or Why I don't like X. Everyone understands that X is a tool. After all, there are no such articles about the shovel. Although, who knows, after all, OOP has existed for several decades, and a shovel is several thousand, and maybe somewhere in the Stone Age there were cruel holivars on the topic that a mammoth shovel or a stone hoe are better?

Related Literature


1. Grady Butch Object-oriented analysis and design with examples of applications in C ++
This book does not need to be presented. One of the most cited books on programming.
2. Barbara Minto Principles of the Minto Pyramid.
Working as a consultant to McKinsey, Barbara Minto created her own method of writing analytical 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 the principles of OOP. The most famous of them SOLI.D . Book published in Russian by Robert Martin Rapid software development. Principles, Examples, Practicewhich describes these principles. But in my opinion it is better to read about them in the articles The Single Responsibility Principle , The Open-Closed Principle , The Liskov Substitution Principle , The Interface Segregation Principle , The Dependency Inversion Principle .

Also popular now: