Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should methods be made private?

Tags:

oop

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 ... 

I'm 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?

like image 641
AaronSzy Avatar asked Apr 20 '10 16:04

AaronSzy


People also ask

When should methods be private vs public?

The rule is that a method should be made provided unless it is needed. One of the main reasons for this is that in a future release of an API etc., you can always make a private function public, but you can almost never make a previous public function private without breaking existing code.

When should a method be public or private Java?

We should use public access modifier if we want to make the method or property visible from anywhere, other classes, and instances of the object. Use the private access modifier if you want to make the method or property visible in its own class only.

Can we make method as private?

Certainly you can define a method as private , so that it can only be called (essentially) from other methods in the same class. This is a pretty common practice.

What does it mean for a method to be private?

When a method is private it means it can only be accessed by objects OF THE SAME CLASS.


2 Answers

The only rule I follow is to make as little as possible public.

Look at it this way. You can always make something public later on - it won't break any existing code. Trying to make something private that was public could well end up breaking a lot of existing code.

If someone wants more functionality from your class then they can make the request and you can expose what they need. Chances are that they'll want something that's subtly different to what you already have anyway so you'll need a new interface.

like image 109
ChrisF Avatar answered Oct 14 '22 23:10

ChrisF


A helpful guidance technique to follow is to write an interface for your class before you start the implementation. Then when implementing, tell yourself that a method not in the interface should be private, or not in that class at all.

If you didn't know a method needed to be there when you were designing the contract of the class, it probably shouldn't be part of its public interface.

like image 22
Ben James Avatar answered Oct 14 '22 22:10

Ben James