⬆️ ⬇️

Why OOP is not encapsulation, inheritance and polymorphism, or how I learned not to worry and fell in love with markup

Hello!



Ladies and gentlemen, the article will deal with how I burned terribly from Object Oriented Programming and Design . Most likely these dokhera and Troschka, and it is quite possible that I brazenly copy someone. I, frankly, not very much googled, but minimally tried. Welcome comments with suggestions and suggestions. and srachem .





Notation and terminology



If the words encapsulation, inheritance, polymorphism , etc. highlighted in bold, the author means the developer tool.

If encapsulation, inheritance, polymorphism , etc. are highlighted by Italian, this is already a profound philosophy.

If an object is underlined by a line, then it should be considered from the point of view of the concept of object-oriented programming as an object .

If in the text you notice this here all that , then you have the interface .

If some action is underlined by such a line and is highlighted by Italian, then this is the action that produces the object (links can be seen through the syntactic construction).

If some text is highlighted as follows , then this is a design pattern.



Brief introduction



In my opinion, the main problem of the PLO is that it is understood from the wrong side.



Unfortunately, in the 21st century, every day the fucking tendency towards IT grows and the information space is filled with similar stories, like yesterday he turned out pieces of iron on a lathe, and today I’m a successful 23-year-old senor with a vocational education on his gallery riveting poor sites on Bitrix, getting the golden mountains. So they do not explain what OOP is, they are explained what encapsulation, inheritance and polymorphism are , and if they are lucky. And what kind of beast is such an OOP - well, and dick with him actually.



Regularly saw in vacancies a requirement for understanding the principles of the PLO. At the same time, the employer himself does not really understand what the PLO is. Never at the interview did I ever get asked what OOP is if it came to him, but they were asked about encapsulation, inheritance and polymorphism, access attributes, how it works with inheritance, and other technical details of the target technology. All anything, but the vacancies are quite serious, requiring a deep understanding of programming and algorithmization, and not this here.



Remark

Regularly in the “candidate sessions” (I spend these every time the contract ends at least to test the trends in the labor market) I pass the n-th number of interviews. It is worth noting that problems with this arise somewhere in 70% of cases, in the rest they really give an opportunity to solve some algorithmic problem. Basically, this is a trivial method in the style of greedy algorithms, the simplest combinatorics, or in the worst case dynamic programming.



I learned in one of the best departments of my country and there the course on the PLO was how to use it within the framework of the .NET platform. Encapsulation, inheritance, polymorphism . But not a drop of this OOP . This, by the way, is one of the most serious problems of modern IT education. By the way, until now the guys with whom I studied on the stream do not know how to use this OOP, and then you have tons of useless factories and some kind of creepy dependencies in your coder.



I myself conducted interviews and asked people how they understand the PLO . I was told about classes, objects, interfaces, access attributes, encapsulation, inheritance, polymorphism, etc. In this case, not often someone could clearly answer what the main idea and profit from the PLO. Just a bunch of fools gathered and came up with three difficult words, to which everyone is now pulling like an icon.



Actually OOP



For a start, let's see what I mean by that. The whole world thinks with objects. You sit on chairs, tables , drink coffee from cups . You exist in the world of objects, live among them. You and the object itself. First of all, the concept of object-oriented programming is the first attempt of a person to depict a program in the form in which a person sees and understands the world around. If you think a little deeper, then this is an attempt to teach the plaster to think like a person. It follows from this pleasant property of the PLO - it is really understandable to people. Let's get acquainted with a small example.



Suppose we want to get cups. The most diverse. From the point of view of my mother, who is not a programmer, we want to build a factory that will produce a variety of cups, mugs and glasses. From the point of view of me, as a programmer, we want to build a factory that will produce a variety of cups , mugs and glasses .



At its base, in cups, mugs and glasses lies the idea of ​​some abstract vessel , the method of using which is drunk from it, and whose properties it can contain liquid inside and made from some material .



Comment on the example and technical details

See how we cleverly get the most basic concepts of OOP by the simplest example? To do this, we abstract from certain details of the implementation of each object and obtained a set of common properties and all that there. By itself, this fact is very important. He tells us that object-oriented programming is easy. Anyone who has read this text will be able to understand that we have some kind of vessel that we have made of something, collected water and drink in it. In technical terms, we will have an abstract class , for example, as in the code below. I chose PHP as a language with clear object syntax and to show that polymorphism is not an opportunity to create a hundred methods in which you can pass different parameters. C # did not choose because of points, but Java lags.



abstract class Vessel { private $material; private $liquid; public function __construct(Material $material) { $this->material = material; } public function fill(Liquid $liquid) { $this->liquid = $liquid; } public function pour() { $liquid = $this->liquid; $this->liquid = null; return $liquid; } } class Cup extends Vessel { //-   } class Wineglass extends Vessel { //-   } class Mug extends Vessel { //-   } class Human { //  -  public function drink(Vessel $vessel) { $this->stomack->add($vessel->pour()); } } 


Further more. In order to start producing mugs, we need a factory . We place an order for the plant and it produces mugs, cups or glasses from the material that we need, for example, from glass . In programming, everything is like in life.



Coder
 class VesselFactory { //   ,  . $className -   ,      , $material -    public function getVessel($className, $material) { return $className($material); } } 


So that a programmer can drink from a mug, cup or glass, he needs a pen . To the pirate could drink from the handle, he needs a hole for the hook . No matter how the factory does, it is important that the programmer can take the vessel with his hand, and the pirate with a hook. Ideally, the handle should be polymorphic , so that the programmer and the pirate could take the mug, and not take the handle by hand or take a hook through the hook hole .



Comment on the example and technical details

The interface requires the object to have behavior for external interaction. The interface does not need to be Able. For example, if our computer implements a USB interface, then this interface will not be Usbabel (uusbibebl). This will be a USB interface.



 interface Knob { public function take(Holder $with); } class Cup extends Vessel implements Knob { public function take(Holder $with) { //-      } } 


This example is not very neat and thoughtful, but it completely conveys how the concept of the PLO implies everything that is so demanded at interviews. This understanding allows us to follow the SOLID principles, create a beautiful architecture and separate business logic from the controller. In essence, in understanding these simple things lies the whole essence of modern development. And even despite the fact that it is just like a boot, 60-70% of developers still cannot come to this. And the reason is that the developer is not offered an understanding of the process, he is offered a tool.



Reasoning



Everyone knows that we hammer a nail with one side of the hammer, pull it out with the other. My Dad used the sharp side of the hammer as a wedge, and the handle as a lever (that is, as scrap), because my Dad was a robot design engineer in industry. He understands the principles by which the hammer (wedge + lever) works, and uses it not only as a hammer. Therefore, we need to understand the principles of OOP to apply them most successfully.



My experience working as a team with guys from the UK, USA and EU shows this big drawback. Architects and seniors of all breeds and stripes, possessing incredible technological knowledge, hardly make good architecture (we are talking about the average case, there are a lot of brilliant guys who do such beauty that they have to look at their UMLs under a blanket). Just because they follow the guidelines directly indicated in the PLO . But much more important philosophical details are sacrificed to technical details. For example, an incorrectly made object decomposition of the system will turn your developer’s life into hell, even if it is done across all guidelines and according to all best practices. Usually, in this case, you get cool classes with interfaces, factories, but there are many, for example, spaghetti code, simply because there are either too few classes (you have to turn their behavior into conditional logic) or too many (and you have to handle different kinds of classes in conditional logic) . And there and the extrendability suffers and the tendency to alcoholism is growing.



The best practice is to present the system as if you are not a programmer, but a builder. Why are design patterns so good? Because they allow us to abstract away from the technical implementation and create structures in our heads such as we would have done not in the code, but using nails, a hammer and boards. Many of my colleagues complained that they did not understand the Gang of Four because there are examples of code in C ++ or Smalltalk. There, the idea is not at all in the code, you can dick it at all, the idea is that you and I can build a program just like a house and a car. In a human-readable manner, copying an engineering genius from completely different spheres. The programmer will be able to think as an engineer only when he begins to see the matrix around. Decompose the objects that surround it, call the methods / interfaces / properties of everything that catches your eye. Starting from our favorite mugs and ending with our favorite pirates.



Firewood throws fire into the fact that in the modern world there are millions of languages ​​that either provide us with castrated OOP (hi Javascript, Go), or technically oriented (C #, Java, C ++), or beautiful and frightening at the same time (Python, Ruby) or PHP in general. And this is not a stone in the garden of these languages, because they have a purpose, an application, and they are good and let them be.



My favorite languages

Python, C # and PHP. Just because. I also loved Brainfack, as a language with an absolutely non-standard approach, it is great for talking. In 30 minutes you can write a projection on a brainfack, and here you can see how well the developer is familiar with standard data structures, how well he has developed logic, and the most important thing is how well the developer can learn.



Conclusion



What is the moral in general? Moral for a developer to think like an engineer to become a programmer. Moral for an employer - you need your programmer to think like an engineer. Modern literature and education offer you a tool and this is really cool. You buy a book and get a huge toolbox for development. But first of all you need to understand the principles of operation of each tool in order to use it most effectively and correctly. OOP is not encapsulation, inheritance, and polymorphism; they only follow from the idea. But understanding the idea is more important than using tools built on the idea. Understandably, there must be people who hammer only nails with a hammer and they deserve wages, places and respect. All craftsmen deserve respect for the craft. But before you assemble a chair, someone has to figure out which chair we will collect. I think many guys and girls will get a nice push in their the bum qualification when aware of this difference.



Dear readers, if you have ideas and suggestions on this topic - it would be very cool if you share this with us.



')

Source: https://habr.com/ru/post/307040/



All Articles