tags:

views:

341

answers:

8

I am designing a class where some methods won't cause any harm if they are exposed as public. But they can be private as well, since they will be used only from the same class in my project.

Making them public has the following advantages:

  1. Unit Testable without the need of accessors.
  2. Flexibility.

Making them private has the following advantages:

  1. Public documentation simplification.
  2. Some unknown bugs aren't exposed.

Which are the general guidelines in this case?

+4  A: 

In this case, I'd usually make them as private as possible, and then promote their accessibility when the need arises (e.g., code from other classes would benefit from being able to access those methods directly). It's easy to add methods to an API, and much harder to remove them (without breaking other code).

Chris Jester-Young
Why would someone try to remove a method from an API?
Jader Dias
All he is saying is that if you start off by making every method public you will have a much harder time making some of the methods private later on because some other classes will probably be using them.If someone wants to borrow $20 and you tell them no from the beginning its a lot easier than if you give them the money then ask for it back 10 minutes later.
Pete
+12  A: 

Always make everything as private as possible.

For unit testing, I sometimes make a member internal, then use the InternalsVisibleTo attribute in the AssemblyInfo.cs to permit the unit test assembly access to the internal members.

John Saunders
This is why I advocate (in C#) *not* using explicit visibility modifiers. The defaults make everything as private as possible, and you can always add the modifier to make something more visible. All that private, private, private is just noise that makes it harder to see what's other-than-private.
Kyralessa
+4  A: 

I am a big fan of only making public what you explicitly want to make public. I like the warm safe cocoon that my class offers me.

TheTXI
I'd put it this way: "...only making public what needs to be public by necessity..." ;-)
Cerebrus
+3  A: 

The only public members of a type should be a part of the public interface of the type itself. Your goal should be to minimize the interface of the type to the fewest number of members and expose little to none of the underlying implementation.

Andrew Hare
+1  A: 

I would NEVER make my privates public ! Period.

Cerebrus
Sounds like something my teacher used to say in programming class. "Only your friends and other members of the class can touch your privates."
TheTXI
Hey, didn't you post that one in some much older question?! Maybe that's where it got into my head! :P
Cerebrus
I did, I will try and find it real quick.
TheTXI
http://stackoverflow.com/questions/643949/poll-whats-your-best-computer-science-analogy-to-a-real-life-situation/643966#643966
TheTXI
I've always preferred "Only friends can touch each others' private members."
Robert Rossney
+2  A: 

Oh, please please read Ch. 06 Of Code Complete 2 by Steve McConnell, if you have access to it. That will answer your question perfectly.

In general, If the Method fits in the overall "Persona" of the class, make it public. More technically, try not to break the abstraction. I would have given an example, but I do not know the context of your work and so examples might be irrelevant.

If you do not need it, there is no need to make a Method public.

For testing, +1 to John sanders.

But I really can not explain you here as Steve has explained in CC2.

I hope its OK to post Book references on Satckoverflow? (Please comment.)

trappedIntoCode
Yes I see many book references. In fact, I often favorite questions that contain good references to books.
Pete
@Pete: Thanks for the info.
trappedIntoCode
+1  A: 

Expose just what's needed for your intended clients and usage scenarios and nothing more. I would not consider unit testing as a client and make changes so that the code not intended for public consumption to begin with is publicly accessible just for the sake of unit testing. If you do, you will clutter up your api, reduce the usability of your api, and make future changes harder and not ideal as there may now be client code that uses what's supposed to be private api.

I would check if you have a better alternatives. For example, you can generate private accessors in Visual Studio 2005 and 2008 that make non-public api of a class publicly available for testing purposes. This may clutter up your unit test code but to me the most important thing is your design and the api you're releasing to your clients including you and your team.

On another note, I would also mention that the unit testing gives you a great opportunity to see how well your design is and how easy it is to consume your api from a client perspective. Armed with frustration, issues, etc during unit test development, you make changes to enhance your api and design to be more simple, beautiful, and usable.

Mehmet Aras
+2  A: 

If you find that it's difficult to thoroughly test a class, then the class may be doing too much. Splitting the class up using composition can make the individual units more testable.

Sometimes it makes sense, other times it does not. Just a thought.

Mark Simpson