B
B
beginer1232017-08-25 06:50:36
JavaScript
beginer123, 2017-08-25 06:50:36

Why write in OOP style in JS?

I don’t quite understand why people mostly program using the OOP approach?
Personally, all these classes, methods, private methods, inheritance, encapsulation, and so on, seem confusing to me.
Especially in JS (I understand in some kind of c ++ it so historically happened that OOP is used)
What is the convenience?
Why not write programs through functions? (functional style)
And call them from each other if necessary.
For example

function getUsers(){....}
function getFollowers(){....}
function getLikes(){....}

Then just use them as a constructor one after another or in each other
. I always write programs like this, I tried to rewrite using classes, but honestly I didn’t understand what the advantage would be, everything just gets more confusing, I decided to leave the functions
. What is the advantage of OOP? On a specific example

Answer the question

In order to leave comments, you need to log in

9 answer(s)
P
Ptolemy_master, 2017-08-25
@Ptolemy_master

My five cents.
Basically there is no need. For small projects, that's it.
But there is one but. As your project grows, managing this collection of features will be oh so difficult.
Imagine that in addition to simply calling users and likes, you will need to count them, perform many other manipulations.
What do you like more?
1. A long list of functions
getUsers
getLikes
calculateUserRating
moveUser
copyUser
saveUser
saveLike
getLike
userLikes (Is this a list of likes or is the user liking?)
... and another hundred or
2. A small list of
User
Like
Rating objects
and simple, clear calls like
User.getList
User.calculateRating
User.copy User.getLikes
User.doLike
Notice the
difference?

E
Eugene, 2017-08-25
@zolt85

From Wikipedia :
JavaScript is a multi-paradigm programming language. Supports object-oriented, imperative and functional styles. It is an implementation of the ECMAScript language.
Those. people write like that because the language allows it.
This topic is very broad and deep. No one is forcing you to write in any particular style. In the same "C with Crosses" you can write imperative-functional code. You will have an entry point in the main method, and then do whatever you want. But there is one small problem - no one will understand and accept this code except you. That is why in software development (not in the programming language, but in the craft of software development) there are such things as design patterns, code conventions and code style. People have to negotiate, find the best option for communication through code. Even if you work alone, your team consists of at least two people - it's you and you in the future. And by writing noodles from a sequential function call, you put a huge pig in your future. Read Bob Martin
PS
Yes, the OOP paradigm now dominates, yes it is not ideal, but this is what most sane developers understand.

S
Saboteur, 2017-08-25
@saboteur_kiev

1. Do not confuse functional programming with procedural (imperative) programming. These are TOTALLY different things.
2. OOP is a paradigm that works well in large projects and facilitates further development and maintenance of the product.
3. OOP allows you to encapsulate a significant part of the code into practically independent objects, which allows you to distribute development among several programmers, with virtually no performance loss. In Imperative Programming, this will cause orders of magnitude more conflicts, and objects are quite independent in this respect, so it is enough to distribute tasks to programmers so that two programmers do not fit into one object. It is the third - the most important thing in OOP. ALL large products where you need to cooperate with at least 10-20 programmers without OOP will be very sad, not to mention products where hundreds of people are needed.
Well, all the further development of OOP has already come out as an attempt to improve the paradigm, simplifying and adding useful convenient things in such a way that points 2-3 are observed.

I
Ilya, 2017-08-25
@rpsv

To implement complex , although in fact any, business logic.
But you didn’t understand the advantages of OOP, only because, it seems to me, that you created a class, accumulated the functions that you wrote before, and were satisfied.
If you approach development from the side of the subject area and entities (objects), then OOP is formed by itself.
Read something about SOLID, or something.
For an example, try implementing a similar organizer: https://www.wed-expert.com/organizer .
As for me, the entities are immediately visible here: a category and an element (from it are inherited: task, guest, cost).
After you describe the data of the entity, all the code will be operating on these objects (add, delete, edit).
If we talk about any React, then with its component "ideology", using entities is very implementable.

P
Pavel, 2017-08-25
@PaulTMatik

FP vs OOP is as old as time. The interpreter/compiler is absolutely purple which style you use. There is a task - it must be solved, if your code solves it, using, of course, a minimum of resources, then what's the difference how it is written.
Some languages ​​have taken specific paradigms as a basis, for example, only OOP, in such languages, of course, everything will be focused on this, so writing them in some other way is less productive and even less understandable for another programmer.
The convenience of any approach is subjective. If you're writing in a language that allows for either approach, pick the one you like best. Unless you have complexes, that your perfectly working code will seem ridiculous to someone narrow-minded, due to the fact that you chose FP over OOP.

L
Legebocker, 2017-08-25
@EnDeRJaY

What is the convenience?
I used to think about it myself, didn’t understand what’s good in classes? And then I realized that laziness is the engine of progress. In the sense that you once wrote a class in a header and use it to your health.
What is the advantage of OOP? On a specific example.
Let's say there are two factories. Both produce cars, but the approach is different. Some use functions, while others use classes. Those who use functions have problems, because there are a lot of orders, and to make them you need to call a lot of functions. And those who use classes, they don't exist because they wrote the class once and now call it simple all the time auto Renault(Logan);.
Also with inheritance and polymorphism
Two new machines came out that inherited something from the old one, and orders went to these factories. The first ones started writing tons of functions, and the second ones added a prefix to the inherited functions in the base class virtual
. Here's the use of inheritance and polymorphism.
Well, encapsulation is easy, although it is not easy to achieve. You just need to not go into details. Like with the volume buttons in the remote. You can only switch to "Louder" or "Quiet", but you cannot select a certain volume level, because you or you will get to it later. Encapsulation hid the details from you. And the classics also have encapsulation. They wrote a class once and it will work, and it doesn’t matter to them how, the main thing is that it works and they get what they want.
PS
I said a lot of bullshit, but this is my understanding of OOP

D
Dmitry Alexandrov, 2017-08-25
@jamakasi666

FP and OOP can be compared with a database, For example:
1) The entire database is one single table in which everything will have to be added. This is roughly speaking the OP's approach.
2) The entire database is divided into heaps of tables, each of which stores data of its type. This is OOP.
For a small simple program, the first option will be easier, and the second one will turn out to be a wild overhead, for a large and complex program, in principle, you can use the first option, but it will be wildly inconvenient and confusing, and the second option is extremely convenient and the total code will become smaller and it will be less confusing.

S
sta-ger, 2017-08-25
@sta-ger

OOP disciplines. FP is well applicable in small projects that are done, so to speak, "in haste". For large, scalable projects that involve multiple people, OOP is essential. You will work with a large project - you will understand.

N
NSA-bot, 2017-08-25
@NSA-bot

I recommend reading this book. It is not tied to a specific language, although there are examples in java, but they are as simple as possible, only to explain the theory.
https://vk.com/wall-54530371_3964

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question