Answer the question
In order to leave comments, you need to log in
Classes and OOP: why, and most importantly, when to use and when not?
Good day to all. I hope at least here I will be given a clear answer to my question.
Having read and watched more than a dozen different articles and videos on YouTube about Python in particular about OOP, I have never heard an answer to a question that seems to me quite important:
why , and most importantly, when should classes be used , and when is enough will write some simple functions.
Suppose . I want to write a tg bot that sends the user a video downloaded from ... well, for example, from KinoPoisk .
That is, the user sends a link to the movie to the bot, and the bot does everything necessary and eventually sends him a vidos (trailer) for this movie.
In my opinion, the task is simple. You need to write a few functions and the bot is ready.
I understand that everyone is now turned on this OOP, and most programmers will say that OOP is cool blah blah blah. It's all clear. But, the question is: why should I create classes for such a task?
I would be grateful if someone could explain this to me in simple terms.
ps if you set a bad example - provide your own example, and explain what and how.
Thank you .
Answer the question
In order to leave comments, you need to log in
All these classes, factories, abstractions and other buzzwords are needed so that the programmer can control the complexity of the program he creates. Our consciousness stores an average of 7 different entities, so by cutting the task into finished pieces - objects, we give ourselves and colleagues the opportunity to reduce the number of entities that need to be kept in our heads.
Further, the program, cut into parts, allows the replacement of one part by another. This makes it possible to develop a program, gradually increasing complexity and replacing pieces that have not yet been written with plugs, testing it, replacing a complex implementation of something with a predictable plug, and moving ready-made pieces of code from place to place, without rewriting the entire program again in case of a slight change in logic.
When such an approach is justified - almost always. A program with a well-organized internal structure is much more flexible and better supported than a long sheet without such a structure.
Firstly, the OOP principle is needed when you want to sort everything out like a perfectionist, and secondly, when the project has more than one or two entities and their instances
. For example, a question and answer service
questions (hasMany answers)
answers (belongsTo question)
likes (belongsTo answer)
comments (belongsTo answer)
A simple analogy of the difference between procedural and OOP.
Here you are writing a scientific article in which there is a certain main text, but every now and then there are references to the details of experiments, lengthy quotations from other people's works and a bunch of tables of several pages each.
The procedural approach is like a book: the whole text is a continuous sheet, and in order to follow the course of the main idea, you have to scroll through the details.
OOP is hypertext: in front of you is only the main material, not littered with anything. If you want to clarify some details, you can easily follow the link and work with them. If they don't interest you, then they don't bother you.
So, after a certain amount and complexity, working with a "book" (for example, finding a specific table and checking its data) is hard even for the author.
why, and most importantly - when to use classes, and when it will be enough to write a few simple functions.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question