Private Methods Over Public Methods

心不动则不痛 提交于 2019-12-17 23:44:32

问题


I was examining the StringTokenizer.java class and there were a few questions that came to mind.

I noticed that the public methods which are to be used by other classes invoked some private method which did all of the work. Now, I know that one of the principles of OOD is to make as much as you can private and hide all of the implementation details. I'm not sure I completely understand the logic behind this though.

I understand that it's important to make fields private to prevent invalid values being stored in them (just one of many reasons). However, when it comes to private methods, I'm not sure why they're as important.

For example, in the case of the StringTokenizer class, couldn't we just have put all of the implementation code inside the public methods? How would it have made a difference to the classes which use these methods since the API for these methods (i.e. the rules to call these public methods) would remain the same? The only reason I could think of why private methods are useful is because it helps you from writing duplicate code. For example, if all of the public methods did the same thing, then you can declare a private method which does this task and which can be used by the public methods.

Other question, what is the benefit of writing the implementation in a private method as opposed to a public method?

Here is a small example:

public class Sum{

    private int sum(int a, int b){
        return a+b;
    }

    public int getSum(int a, int b){
        return sum(a,b);
    }
}

Vs...

public class Sum{

    public int getSum(int a, int b){
        return a+b;
    }
}

How is the first sample more beneficial?


回答1:


In order to add something, a private method can ALWAYS be changed safely, because you know for sure that is called only from the own class, no external classes are able to call a private method (they can't even see it).

So having a private method is always good as you know there is no problem about changing it, even you can safely add more parameters to the method.

Now think of a public method, anyone could call that method, so if you add/remove a parameter, you will need to change also ALL the calls to that method.




回答2:


The only reason I could think of why private methods are useful is because it helps you from writing duplicate code.

In addition to consolidating duplicate code (often expressed as "Don't Repeat Yourself" or "DRY"), use of private methods can also help you to structure and document your code. If you find yourself writing method which does several things, you may wish to consider splitting it into several private methods. Doing so may make it clearer what the inputs and outputs for each piece of logic are (at a finer granularity). Additionally, descriptive method names can help supplement code documentation.




回答3:


When writing clean code in Java or any other object-oriented language, in general the cleanest most readable code consists of short concise methods. It often comes up that the logic within a method could be better expressed in separate method calls to make the code cleaner and more maintainable.

With this in mind, we can envision situations where you have many methods performing tasks towards a single goal. Think of a class which has only one single complex purpose. The entry point for that single goal may only require one starting point (one public method) but many other methods which are part of the complex operation (many private helping methods).

With private methods we are able to hide the logic which is not and should not be accessible from anywhere outside of the class itself.




回答4:


Public methods are generally code that other classes which implement that class will want to use. Private methods are generally not as useful outside the class, or don't(alone) serve the purpose of what the class is meant to accomplish.

Say you're in your IDE of choice, and you implement a some class A. Class A is only designed to do one thing, say document generation. Naturally you will have some mathematical and byte operation methods in Class A that are required to do document generation, but people trying to use Class A are not going to need these other methods, because they just want a document. So we make these methods private to keep things simple for any future users of our class.




回答5:


The purpose of declaring a method private is to

  • hide implementation details
  • exclude the method from being listed as public API
  • make sure the logic behind the code is not used/misused externally
  • most of the time your method's execution depends on other methods being run before it; then you can also be sure that you control the correct sequence of using your method

Use private for your methods unless you intend for your method to be safely used outside of the context of your class.




回答6:


Making functions private gives you advantage in following cases :

  1. Making function private gives JVM compiler the option of inlining the function and hence boosting up the application performance
  2. If the class is inheritable and you extend it from a child class, then in case if you want to hide the functions from child class then you can do this (you can extend StringTokenizer).
  3. If a piece of code has to be used in multiple functions the you move that code in private utility method



回答7:


An advantage and also a good reason to use private methods inside public classes is for security and bug prevention. Methods that are declared as private are only accessible by the class they are part of. This means that your private method can't be accidentally called from else where within the program reducing bugs and other complications. If you declare your method as public it can be accessed by the whole problem and can cause complications.

You may have a number of methods that work on a certain piece of data that you don't want any other part of the program to be able to interfere with. Using data encapsulation via private methods and/or variables helps to prevent this and also makes your code easier to follow and document.



来源:https://stackoverflow.com/questions/7749052/private-methods-over-public-methods

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!