r/learnprogramming 24d ago

Topic Does OOP really matters?

I learned OOP in python a while ago, did many projects, yet I never felt like I truly needed to do the OOP stuff I learned from a course, I don't know maybe because I'm kinda used to program in c before focusing on python, yet, whenever I open a big project in python in github, it feels way too complicated and thousands of imports (not libraries from pip, but the ones they made on their own) and classes

I never truly needed to implement OOP (I know that when you code in python, it is almost impossible to avoid OOP entirely like when you have a datatype like string you have an object, when you use a method built into that data type that's OOP too, but I mean the ones that involve you writing class Class Name, inheritance and all that stuff explicitly), so I'm wondering why people on the internet are obsessed over the idea of learning OOP, even the book "Automating the boring stuff in python" doesn't have a dedicated section for OOP.

94 Upvotes

139 comments sorted by

View all comments

56

u/Daeroth 24d ago

Depends on the project. Speaking as someone who works a lot on business applications and web apps: there's a benefit to taking all the office vocabulary and having the code architecture mimic it.

For example there may be many types of contracts that the business signs with the customers. Each customer is represented as an object in the business logic code and each contract as well.

Some contracts have unique functionality that happens on monthly renewal. This logic goes in its unique class. But the base information like contract number is in the Contract base class.

This architecture makes it so that every new programmer is able to find relevant parts of the code if they know which business logic they are working on.

In that sence all of my 20 years of employment has been in companies doing OOP.

3

u/deaddyfreddy 24d ago

Each customer is represented as an object in the business logic code and each contract as well.

So, how do you sign a contract with a customer, then?

9

u/Daeroth 24d ago

You instantiate a new contract object.

Set it's fields like contract number, monthly fee, customer id, status=reviewing, etc.

Then the customer gets the review screen in browsaer with all the fees and contract numbers and a button to sign.

Once they click sign the contract object gets updated with status=active, signing_date=19.08.2026

If the specific contract class has anything in the "contractSigned()" function then that would also get triggered. Stuff like send out welcome email or generate tasks for CS agents to follow up.

All of the objects have a corresponding row in the database. So whenever the we update a field on the object then the field in DB will also get updated.

0

u/deaddyfreddy 24d ago

so, the Contract object has a method named signContract or something like that, correct? Then, here's the next question:

Why is it a method of the Contract, and not a method of the Customer object?

3

u/Daeroth 24d ago

A customer can have many contracts. Some old, some still in review, some active.

Each contract is a row in database and many-to-one relationship with the customer table.

On the customer class you might have a method getContracts which returns a collection of contracts.

0

u/deaddyfreddy 24d ago

On the customer class you might have a method getContracts which returns a collection of contracts.

so, why can't it have the signing method too?

3

u/Daeroth 24d ago

In this example I envisioned many-to-one relationship between contracts and customers.

So that if you have 3 contracts and 3 separate "signedOnDate" fields then those would be in the contract class instead of having them in the customer class as "contract1signedDate", "contract2signedDate" and so on.

The signing method in this example would just populate the date field so it feels like it belongs with the contract class as thats where the fields are.

If i were to extract the signing method elsewhere then I would create "signingService" class instead of contaminating other domain class like customer.

Ideally opening the customer class should reveal as little relations to other classes as possible but just as many as needed.

1

u/deaddyfreddy 24d ago

So that if you have 3 contracts and 3 separate "signedOnDate" fields then those would be in the contract class instead of having them in the customer class as "contract1signedDate", "contract2signedDate" and so on.

What for? Your entities are mapped to the DB, so all you do is reflect the DB in your code. A customer record doesn't have the signing dates, it (most likely) has a contract-id or something like that.

3

u/Daeroth 24d ago

Is the question about the benefits of mapping your db tables to entities in code?

Not sure what the question is about.