encapsulation in every class where variables are set/reset
What do you think of it? Personally I see little reason in hiding fields behind getter/setter methods if no additional checks are in place.
Am I missing something? When coding alone what benefit do I get from hiding fields behind set methods?
You are right, there is no real benefit of get and set if you just want to have a public field. But in case you have a code associated with get or set, like if you get something and you want your class to go and get something from internet or database it is worth it... Or if you set something and your class will change color of something that is where get / set works good, otherwise use public field... it is as good.
At my university it was solely used to change private fields, with no checks happening whatsoever Thanks for the heads up :)
You can also think of scenario where you want to increment or multiply value of variable when you set it or get it. Most of unit conversion and all related things.
If you decide to add those checks later to a public field you’ll need to recompile all of its clients and if it already had a getter/setter you only need to recompile the library itself
Yes, that I understand
Обсуждают сегодня