I always hear that we need to encapsulate whenever we write object-oriented code. If I'm the only developer on a project, do I still need to use encapsulation?
3 Answers
One way to put an answer: Encapsulation, conceptually, exists for writing better, safer, less error-prone code. It doesn't exist, primarily, to facilitate teams working together on code (that might be a side effect, but that's not the purpose).
So the goods that encapsulation seeks to foster scale from one coder to many coders, and they are goods that do not really have to do with the number of coders, although those goods may find stronger expression the larger the project and teams are.

- 9,258
- 4
- 36
- 53
-
I very much agree with your answer. Can you please comment on similar [question](http://stackoverflow.com/questions/31769882/does-encapsulation-help-develop-multiple-modules-parallely)? – overexchange Aug 23 '15 at 12:08
Encapsulation is there for a reason.
Someone has to maintain and manage your code after you are done, right? What if the project gets bigger and you get team members?
So, the answer is "yes", it is always best to use encapsulation whenever possible.

- 34,814
- 22
- 96
- 117
-
Please explain for this example which i just read on codereview http://codereview.stackexchange.com/questions/20321/review-code-for-optimization-and-implementation – Vinayjava Jan 10 '13 at 07:00
-
-
You have to have logical separation of various functionality so that `coupling` is reduced. Each `class` must concentrate on what it does best and let the rest of the stuff be handled by other specialized classes. Then you don't let the other classes know how you are doing your stuff. – ATOzTOA Jan 10 '13 at 07:11
The fact you are asking this question makes me wonder you actually did not get the actual value of encapsulation as a means to reduce and thus deal with complexity. My theoretical computer science professor used to tell me that in the end, if you think at the whole binary representation of a program, any program is just a number. Very big indeed but, only a number. And that is true, any other construct we use but 0 and 1 (i.e. C++, Java, Python, functional programming, object oriented programming, aspect oriented programming, etc..) is just because of the fact we need more abstract means to get the one number we need.

- 7,358
- 6
- 47
- 84
-
Thanks a lot guys see the problem is theortically i know these stuff but when i want to implement them i feel that well it would do the same thing any way if i follow oop or not – Vinayjava Jan 10 '13 at 07:20
-
I know it wrong mentality but you guys explain me more it would be great – Vinayjava Jan 10 '13 at 07:27
-
The only reason why you would not follow OOP is to safe time, at least in the first 5000 lines of code (if your program gets bigger than that, I could swear you will miss some OOP rules). It is like tooth brushing: You can save a lot of time every day and do it quick and dirty, but in the long term you won't have much fun with your teeth. So if you plan to use them still in 10 years, you should invest the effort! – Desty Jan 10 '13 at 22:58
-
that's nice it does make sense.This should inspire to write some OO code – Vinayjava Jan 11 '13 at 07:12