A
A
alex4answ2020-11-05 18:09:53
JavaScript
alex4answ, 2020-11-05 18:09:53

When to use OOP?

Good afternoon, ~3 years I wrote in PHP exclusively in the OOP style, then I switched to JS + TypeScript, where almost everything is in the procedural or functional style (without OOP in general, I'm already confused how it's called correctly).

And then looking back, I see that most things can be described with the OOP approach and FP, but how will it be right, when and what to use?

I approached the choice in terms of:

I need an object that will hold the state/data, and are there general operations on that state?
Do we use OOP goodies (inheritance/abstraction/polymorphism)?

- yes = writing in OOP style
- no = writing without OOP

But, a huge bunch of things, from MVC frameworks to small libraries, are written in OOP, regardless of whether we need state, general operations on data, etc.

A prime example of a regular controller in MVC:
class PageController {
  public actionIndex() {
    // что-то
  }

  public actionDelete() {
    // что-то удаляем
  }
}


Why and when to use OOP?
so that it's not just "because I'm so used to it"

Answer the question

In order to leave comments, you need to log in

3 answer(s)
W
Wataru, 2020-11-05
@alex4answ

OOP is not only when you take some entities from the subject area and wrap each one into an object that can do something. It's more of an approach to code organization. You divide the task into subtasks, and the data into separate parts, abstract the details within the objects. This reduces the complexity of the architecture. Theoretically, any program can be written inside one huge function with a bunch of gotos. But no one does that because it's impossible to maintain and incredibly hard to write. OOP is a logical continuation of procedures. Now you not only abstract some parts of the program in one place, but now also the data along with them.

I need an object that will hold the state/data, and are there general operations on that state?

Question: What do you mean you need? You can always take a global variable, write functions that accept this state and do something with it. But quite often organizing as an object is just more convenient.

G
Griboks, 2020-11-05
@Griboks

Each approach/paradigm has some purpose. If you understand this purpose and really want to use its advantages and disadvantages, then use it. If you don't understand, use another approach whose purpose you understand. If there are no such approaches, use generally accepted ones.
ps
Any code can be rewritten for any approach.

A
Anton R., 2020-11-05
@anton_reut

For comparison, procedural and OOP are like an old Soviet garage and a modern one.
Procedural code - a lot of things (including unnecessary, obsolete), it seems to work and everything is there, but without a bottle you can't figure out what's where:
%D0%A5%D0%BB%D0%B0%D0%BC-%D0%B2-%D0%B3%D0%B0%D1%80%D0%B0%D0%B6%D0%B5-1280x720.jpg
OOP - everything is laid out on the shelves, order, each drawer for a strictly defined tool and signed:
etapy

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question