Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Encapsulation  RSS feed

 
Nathan Leniz
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it truly necessary? If we designed the class, or at least documented it really well, wouldn't it be fine to access variables directly rather than through a getter and setter?

I can understand if you are relying on data that you yourself aren't providing, but otherwise, I don't see a big need. Could anyone explain further?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not every programming project is a one-person project. In fact, the number of programmers involved in many commercial projects has three or four digits. Any one person understands only a small fraction of the code in detail.

Not every project is a standalone application. Many projects are libraries, intended to be reused by other projects -- staffed by people that don't know anything about how the library is implemented at all.

In any of these situations, breaking a system into small, encapsulated components that can be understood in isolation is the only possible way to maintain order. If a hundred thousand classes are all diddling with each other's data directly, and you find that a member variable in one object somewhere is somehow, suddenly 47 instead of the 32 you expected, where do you look to find the problem?
 
Neerav Narielwala
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes it's a team work... and synchronization is also important.So, I think we should follow the method which is accepted by all..
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!