T
T
theNorthWind2019-11-11 19:31:56
OOP
theNorthWind, 2019-11-11 19:31:56

Why is OOP necessary?

Hello. Please explain, in your opinion, why you need oop? Why is it useful and what does it like?
Personally, I am a beginner and work in a laboratory where only I program, and the rest are scientists. I can say in free swimming, not counting textbooks and courses. But on every street corner I hear that oops is just an absolute must. I have not yet met such a need - I easily and intuitively single out all the logical parts of the program in functions and everything looks beautiful in my opinion. OOP is hard for me to understand. And if simple examples from textbooks look clear enough - let's say there is a class animal, it has paws and eyes, and in each object of this class they are defined in their own way. You can imagine it and even imagine where it is needed.
But when you come across a real project (I had to get acquainted with the framework tied to OOP and the code using it), then the beauty of this abstraction is lost - the object is too complex to imagine, it is not clear what is inherited from somewhere in the depths of the code, that's all Redefined 100 times... Abstraction to abstraction to abstraction... Useless functions that need to be rewritten anyway... Does it make things easier?
And the most interesting thing is when a class is created for each sneeze / each comma.
I'm not asking a question to incite hatred, but I'm not asking you to teach me about life either. I want to try to understand the point of view of those who are comfortable and can learn something.

Answer the question

In order to leave comments, you need to log in

17 answer(s)
S
Saboteur, 2019-11-11
@theNorthWind

Previously, a program could be written in one continuous listing. But when trying to make changes, it turned out to be very difficult to understand all the dependencies inside the program, once its size exceeds some critical level.
There was a fashion for modularity.
But the programs have become more complex, and the module no longer fits in the brain of one person so that it can be quickly edited.
In the process of various approaches, an OOP approach was invented, the essence of which is as follows:
Since all programs operate on some data, then you need to take this data, take the functions (methods) that work with this data and place it in one object.
If you need to change the data type, add / subtract / share functionality, then the programmer will work with this one object. At the same time, if different objects request something from each other, then in OOP it is quite easy to make versioning and backward compatibility.
Well, everything else (inheritance, polymorphism, and so on) has already arisen as a consequence of the fact that OOP does not solve all problems. There is no other, more convenient global paradigm for complex programs yet, so OOP has taken its niche.
Smaller programs, especially those that can be written by one person, can be written in any way, but the larger the program, the more difficult it is to maintain, and OOP is one of the most accessible methods to "divide" the program into independent encapsulated pieces.

S
Sergey Gornostaev, 2019-11-11
@sergey-gornostaev

Read "Clean Code" by Robert Martin, it is clearly explained there. All existing programming paradigms, design patterns and architectural principles exist for exactly one purpose - to reduce the complexity of maintaining and developing a large code base.

E
Evgeny Romashkan, 2019-11-11
@EvgeniiR

Understand the difference between OOP and procedural programming first.
OOP in the formulation "Encapsulation, Inheritance and Polymorphism" may not be necessary.
Object-oriented design as a decomposition tool is needed to control the complexity of the system.
And in general, you want to briefly analyze the topic of many books and publications here. That doesn't happen.
From the fact that the author called this mess OOP, it did not become so. You judge a tool by examples of its poor use.
And in this, I must say, there is common sense. OOP is now a term that does not have a specific definition, and everyone has their own understanding of it. Therefore, it is worth looking at something more specific.
However, if this answer gives you something:
The purpose of OOP is to manage the complexity of software.

R
Rustam Azizov, 2019-11-11
@UnformedVoid

OOP was conceived as an approach for decomposing code into modules - classes. Each class performs its function and the code remains clean. This is ideal. But in real life, OOP, while eliminating the complexities of procedural programming, adds its own. Everything is as you described. The ideas described in the books do not have applications in real examples. To write good OOP code, you need to know a lot and have a good understanding of the abstract side. Plus, OOP is implemented in many ways. Popular implementations of OOP (Java, C#) are essentially Class Oriented Programming. In addition, OOP increases the amount of code. Also, the postulates on which the current (popular) OOP is based create prerequisites for fragile code. For example, inheritance. Inheritance was originally intended as a method for reusing code. But over time it became clear that large class hierarchies lead to unpredictable errors. If an error is found in the base class when there is already a large hierarchy, fixing it is fraught with the appearance of difficult situations with the fall of the modules tied to it. Other examples could be given, but this is a topic for an entire article, and possibly several. Functional programming features are now being introduced into OOP languages. Functional programming is based on the idea of ​​function composition. OOP is now moving (or has already moved) from inheritance to object composition. That is, it is recommended to use composition instead of large inheritance hierarchies. Functional programming is devoid of the problems of OOP. Many things that you need to specifically learn in OOP (for example, design patterns, dependency injection), in FP are either a fundamental principle, or a naturally emerging consequence (in OOP, too, many good practices emerge naturally, however, in order to understand their naturalness, you have to look carefully). FP gives you a lot out of the box. OOP is a good thing, but it is becoming obsolete. FP allows you to write reliable, extensible, concise, elegant, and efficient code at a much lower cost.
Please do not perceive my attitude towards OOP as negative - it has its own advantages, its own niche. Plus, in the context of OOP, people were able to learn a lot, because OOP led them to many thoughts, created the need to study the structuring and modularization of code. In the context of FP, this would not be so obvious (for example, dependency injection within FP is not at all interesting to study, since in FP it is just passing function parameters, which is what we already understand). So everything has its place and time.

C
CityCat4, 2019-11-11
@CityCat4

Nothing :)
If the problem is solved without it. OOP is just a tool and should be used where it makes sense. If it does not make sense to use it - do not use it :)

A
Anton R., 2019-11-11
@anton_reut

It seems to me that you begin to understand the benefits of OOP when, after a week, you stop understanding what your own procedural noodle code does ... I had this )

V
vanyamba-electronics, 2019-11-12
@vanyamba-electronics

I can recommend reading Grady Booch . It pretty clearly explains what it is and why.

N
Northern Lights, 2019-11-12
@php666

Everything is very simple.
Imagine, for example, a car in which the engine is welded to the body, the generator is welded to the engine, and the wheels are welded directly to the hub assembly. How do you change a wheel? Yes, in general, no way.
Imagine, for example, the structure of a computer, where all the hardware is one monolithic block. Let's say you have damaged some minor detail - the USB connector. Everything - a computer for 50 rubles in the trash?
Objects allow you to compose the tasks of individual entities (car engine, PC motherboard), so that a set of such objects would provide the mechanism for the operation of a particular device. This is in the real world.
The same is true in programming. A large task is divided into subtasks, business logic is linked by objects, which provides developers with ease of maintenance and "durability" of such code.

U
uvelichitel, 2019-11-11
@uvelichitel

OOP is used in the enterprise because it is easier to replicate, maintain and sell.
Functional stuff like Haskell is popular in institutes because it looks more like formulas, it's prettier, it's easier to defend dissertations, no one will understand anything anyway.
In laboratories, C and Fortran seem to prevail when you really need to calculate something.))

W
WTERH, 2019-11-11
@Expany

They will throw slippers on me right now, but!
IMHO, honestly, for all the time of practice, I came across a motley bunch of functionalities and procedurals, from small to large, from simple to complex.
I can say for sure only one, perhaps the best definition for me (exclusively), OOP is, in essence, more flexible "functions" (classes) containing other functions (methods) inside themselves, and in general, they are a set of necessary tools in a single wrapper.
That is, by calling 1 class, I get access to all its methods inside at once, and I can access each of them at any time when it is needed, or not in one of them.
We have on one project, a legacy with classes older than my mother (just kidding), everything is built according to this principle.
All internal methods are collected in a class in order to use it as mentioned above, nothing more (and I honestly would not call it OOP, that's all, but what to do, we work with what we have).
In general, this is probably the simplest and what and how one could describe OOP (although this is not accurate).

D
Daniel Demidko, 2019-11-12
@DanielDemidko

OOP is needed when we have a set of data of different types that we want to process together as one. That's all

K
Kirill Alekseev, 2019-11-12
@kspitfire

As many have already answered, OOP is just a way to manage complexity. Not the most ideal, but the most popular and working option.
The problem "everything is clear in the textbook, but it is not clear in the real code" is familiar to me, I also encountered this at the very beginning. The catch here is that we need to stop comparing objects in OOP with real objects in the material world. These are abstractions, and examples with cats and dogs are given in order to make it easier for the reader to imagine all this. In fact, an object is anything that can be distinguished in the subject area as something independent and separate.
The best advice, in addition to literature, will probably be this - read and study the code of projects with OOP. Start small, with those whose subject area you understand. See what abstractions they have, what they have separated into separate classes, how it all interacts with each other.

I
ittakir, 2019-11-11
@ittakir

First, you should stop being afraid to write redundant code.
Yes, OOP forces you to describe classes, make constructors, destructors. But all this serves to make the code easier for a person to understand. No need to save every byte in the source. The more clearly your code is described (for example, variables are not called i, j, k, but value, count, capacity), the better. Also with classes, the eye gets used to the structure, that here is a description of the data, next to the functions that initialize this data, work with it and destroy it.
When you write only procedures, without OOP, then the larger the project, the more difficult it is to understand which functions work with which data and in what order.
Sometimes there is an OOP of the brain, when there is really an object for every sneeze. But no one is forcing you to write the same. Keep objects to a minimum. Then get used to it and understand what the trick is here.

M
MisterN, 2019-11-11
@MisterN

Actually, what language do you write in, what framework were you considering, what class scared you so much?
Do you use an IDE to navigate by class? Probably in all jetbrain ideas, the cursor on the called method (this->method()) and Ctrl + B help to find the place where it is implemented, for example.
- I'm the only one programming - I
haven't met such a need yet - I easily and intuitively highlight all the logical parts of the program in functions and everything looks beautiful in my opinion
Well, work on the functional, what's the problem? The only thing is that you don't have to rewrite everything later. Or do you still need a framework, and its bad developers wrote it wrong, in OOP? Simply comfortable is known in comparison with inconvenient. If the project is not so big, then you can live without OOP. Although, I do not see any difficulties in the tool, allowing you to add more structure and order. The method is a function in the class. It is like a function outside the class, only in the class. Variables and classes are united, structured according to some kind of logic. Isn't that enough to use OOP? In addition to any patterns, isolation of complexity and implementation of methods, etc.
the cool thing is that you don't have to represent everything inside the class. You can abstract from it and solve only the tasks that are relevant to you.

P
pazukdev, 2021-01-18
@pazukdev

As already stated in the comment above:

with one goal - to reduce the complexity of maintaining and developing a large code base

I would add that OOP is trying to increase the "human readability" of the code by transferring the properties of the real world to the writing process and code structure.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question