Private vs. Public members in practice (how import

2019-01-06 15:31发布

One of the biggest advantages of object-oriented programming is encapsulation, and one of the "truths" we've (or, at least, I've) been taught is that members should always be made private and made available via accessor and mutator methods, thus ensuring the ability to verify and validate the changes.

I'm curious, though, how important this really is in practice. In particular, if you've got a more complicated member (such as a collection), it can be very tempting to just make it public rather than make a bunch of methods to get the collection's keys, add/remove items from the collection, etc.

Do you follow the rule in general? Does your answer change depending on whether it's code written for yourself vs. to be used by others? Are there more subtle reasons I'm missing for this obfuscation?

22条回答
ら.Afraid
2楼-- · 2019-01-06 15:55

I base my decision on the Code's depth within a module. If I'm writting code that is internal to a module, and does not interface with the outside world I don't encapsulate things with private as much because it affects my programmer performance (how fast I can write and rewrite my code).

But for the objects that server as the module's interface with user code, then I adhere to strict privacy patterns.

查看更多
你好瞎i
3楼-- · 2019-01-06 15:55

Certainly it makes a difference whether your writing internal code or code to be used by someone else (or even by yourself, but as a contained unit.) Any code that is going to be used externally should have a well defined/documented interface that you'll want to change as little as possible.

For internal code, depending on the difficulty, you may find it's less work to do things the simple way now, and pay a little penalty later. Of course Murphy's law will ensure that the short term gain will be erased many times over in having to make wide-ranging changes later on where you needed to change a class' internals that you failed to encapsulate.

查看更多
Evening l夕情丶
4楼-- · 2019-01-06 16:02

C# Properties 'simulate' public fields. Looks pretty cool and the syntax really speeds up creating those get/set methods

查看更多
Animai°情兽
5楼-- · 2019-01-06 16:04

Encapsulation is important when at least one of these holds:

  1. Anyone but you is going to use your class (or they'll break your invariants because they don't read the documentation).
  2. Anyone who doesn't read the documentation is going to use your class (or they'll break your carefully documented invariants). Note that this category includes you-two-years-from-now.
  3. At some point in the future someone is going to inherit from your class (because maybe an extra action needs to be taken when the value of a field changes, so there has to be a setter).

If it is just for me, and used in few places, and I'm not going to inherit from it, and changing fields will not invalidate any invariants that the class assumes, only then I will occasionally make a field public.

查看更多
来,给爷笑一个
6楼-- · 2019-01-06 16:04

I follow the rules on this almost all the time. There are four scenarios for me - basically, the rule itself and several exceptions (all Java-influenced):

  1. Usable by anything outside of the current class, accessed via getters/setters
  2. Internal-to-class usage typically preceded by 'this' to make it clear that it's not a method parameter
  3. Something meant to stay extremely small, like a transport object - basically a straight shot of attributes; all public
  4. Needed to be non-private for extension of some sort
查看更多
神经病院院长
7楼-- · 2019-01-06 16:05

Specifically to your example of using a collection that you would return, it seems possible that the implementation of such a collection might change (unlike simpler member variables) making the utility of encapsulation higher.

That being said, I kinda like Python's way of dealing with it. Member variables are public by default. If you want to hide them or add validation there are techniques provided, but those are considered the special cases.

查看更多
登录 后发表回答