When should methods be made private?

Posted by AaronSzy on Stack Overflow See other posts from Stack Overflow or by AaronSzy
Published on 2010-04-20T16:17:56Z Indexed on 2010/04/20 16:23 UTC
Read the original article Hit count: 113

Filed under:

There are lots of times where i'm not sure whether a particular method should be made private or not. For example, i'm building a class right now, which, is responsible for generating a report. This class has a buildReport method and several methods which collect the necessary data for buildReport.

// single public method
// uses a set of helper methods
public buildReport()

// helper methods
private avgSurveyTime()
private fetchVendors()
private fetchSendCounts()
private ...

Im debating whether i should make these helper methods public. The only method i really plan on calling outside at the moment is buildReport. However, it might be useful to get just a list of the vendors with fetchVendors etc.

I see two schools of thought on this: You can always expose as little as possible. (In which case, many of my classes would only have one public method) OR you can expose all you can that might be useful to the user of the class.

Is there a good rule of thumb to use for deciding when methods should be made public/private?

© Stack Overflow or respective owner

Related posts about best-practices