When to use OOP?

  • 0
    Good afternoon, ~ 3 years I wrote in PHP exclusively in OOP style, then switched to JS + TypeScript, where almost everything is in a procedural or functional style (without oop in general, I'm already confused as to 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 correct, when and what to use?

    I approached the choice from the point of view:
    I need an object that will store state / data, and are there general operations on that state?
    Using OOP goodies (inheritance / abstraction / polymorphism)?

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

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

    A prime example of a common 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"
    JavaScript Elizabeth Bullock, Oct 1, 2020

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



    I need an object that will store 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 take this state and do something with it. But quite often, organizing as an object is just more convenient.
    Avery Maxwell

  • 0
    FP is about something else.

    MVC is just an approach. can be easily implemented without OOP.



    OOP is all about complexity management / decomposition.

    if you are doing a one-time small task, there is no point in bothering them.
    Anonymous

  • 0
    Simple. You have this controller. You need to inject a logger into it and it is used in 10 methods, will you write 10 functions with one more parameter? And we add a database and reports it turns out noodles. And most importantly, a person is able to keep in his head about a maximum of 8 variables, in my classes it turns out from 5 to 20, which is already annoying.
    Anonymous

  • 0
    Each approach / paradigm has a purpose. If you understand this goal and really want to exploit its advantages and disadvantages, then use. If you don't understand, use a different approach, whose purpose you understand. If there are no such approaches, use the generally accepted ones.



    p.s.

    Any code can be rewritten for any approach.
    Anonymous

  • 0
    For comparison, procedure and OOP are like an old Soviet garage and like a modern one.



    Procedural code - a lot of things (including unnecessary, outdated), it seems to work and everything is there, but without a bottle you can't figure out what's where:





    OOP - everything is laid out on the shelves, order, each box for a strictly defined instrument and signed:
    Anonymous

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!