Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
xalez19377yif you make everything public u confuse whoever is using ur objects and can introduce incorrect states
imagine u have a Car object and u want to track how much fuel is in the car and its kilometerage, u might have a method Move(distance) and FillUp(amount). Move would increase the kilometerage and decrease the fuel. Now u would have properties Fuel and Kilometers that are publicly readable but to writable
If whoever uses ur object can modify the Fuel and Kilometers variables (i.e. they r public read/write) u can't make sure that fuel is always updated correctly as the car moves
of course there is more to OOP than public/private/protected, but hope that helps a little if that's the bit that's confusing u -
xalez19377yanother, maybe more real life and simller example, is if u have a Stack class with Push and Pop methods; internally, u might implement that using a private array/list
If your array/list variable is public, there is no way to ensure that the behaviour of the stack will be correct (i.e. first in last out). If u happen to read someone's code who used that Stack, u will have to go through the code to make sure the Stack was actually used as one, rather than the private array being accessed directly -
Shodan4987yIf what you need is a real world analogy to explain access levels and visibility try reading something like this one and see if it clicks: https://cseducators.stackexchange.com/...
-
I've been there. And stop searching for that real world example, I honestly found them more confusing at understanding OOP. You know, those Animal, Dog, Cows and Bird examples. Or those Vehicle, Car examples. Never found them helpful.
Think of oop as a way to achieve separation of concerns and maintainability.
I once dealt with a god-class that fetched some zip file over FTP, extracted the zip, checked the XML files for a certain content, parsed it, then stored some of the parsed content into a database. It was a mess.
(The unit test setup was bloated. Because they had inject a mocked ftp fetcher giving an example zip file, and also a mocked database handler).
I've split that god class into multiple chunks. One for downloading the zip file. One to extract the zip. One to parse the XML into some value objects. Another service to store those value objects into a database.
Then some glue-service that instantiated all those classes and glued them together so it fullfilled my use case. -
A class should do one thing and only one thing. It should have as less public methods as possible, yet as much as is meaningful.
Each other service it calls should be injected into it, at best within the constructor and typehinted against an interface so you can plug and play.
If you download a zip file, you don't care about the protocol. FTP, HTTP, Bittorrent, E-Mail -- you care that you can call `fileFetcher.download('thatFile');`. So having a interface for a basic file fetcher is helpful, and each protocol can have its own implemenation, FtpFileFetcher, HttpFileFetcher, and they all have very complex internal logic, yet you don't care as they only expose abstract methods like `download`.
You do OOP wrong by having all properties as private if you then expose them all via setters and getters.
Mutability is your enemy. External access to internal state is your enemy. OOP is one way to solve that by restricting access. -
kpenc21297yOop doesn't model after the real world. A thing that helped me a lot is asking the question "Does class X need to care about thing Y?". And of course,. OOP is just a paradigm, you don't need to use it. It's not the holy Grail.
-
xsacha4317yIf you export everything, you are telling the linker everything can be used and thus it strips nothing. In the same way, if you make everything visible in your class, intellisense strips nothing and all your symbols are offered as functions in the class.
Sure it works if everything is public but it makes it harder to use, means you need to make the API bulletproof for functions even if you don't expect the client to use them, because they will. It also makes the job harder for the compiler as it finds it difficult to remove dead code.
Related Rants
I've always sucked at OOP and OOD, _in part_ because I have never encountered a good, common sense, relatable real-world example or analogy of why one would use protected or private variables/objects/functions over public. I watch tutorials and it all just sounds like static in my head and the explanations are just like "well, it's obvious you want to do blah blah blah because reasons."
Maybe it's just painfully obvious to everyone but me and my tiny brain just isn't capable of understanding. But if anyone has the example or analogy that made OOP click for you, please share.
undefined
object orientation
oop