2
votes

Is there a standard practice or convention on where to declare methods which are used(or called) only inside another methods? To demonstrate this assume the following:

public class MyClass{
    methodA();
    methodB();
    methodC();

    public void methodA(){
        methodA1();
        methodA2();
    }  
    public void methodB(){
        methodB1();
        methodB2();
    }
    private void methodA1(){
        do something;
    }
    private void methodA2(){
        do something;
    }
}

Or, is there a different way of codding that could improve the readability of the above? I have read other questions with similar topics but they don't address my question which is specific on mothods which are used inside other methods. You may assume that methodA, methodB, and methodC have logical and meaningful sequence for a specific task. My problem is that the other methods(methodA1, methodA2,..) does not give sense in this task, they give sense only inside the methods they are called. Or, should I not be bothered on where to place method declarations at all?

3
public methods first, then protected, then private. makes it a bit easier to read.pecks
In a recent study on program comprehension, the results indicated that the answer is probably: any consistent ordering.Elazar
@Stefan proxy pattern doesn't seem to address my question or I may not have understood it well.Addis
@Elazar, do you mean that as far as I'm consistent in my style it's Ok even if it differs from others?Addis

3 Answers

1
votes

Methods should be ordered in such a way so that they read like a story that doesn't cause the reader to need to jump around the code too much

Robert C Martin (aka Uncle Bob)

What this means (in short), is that your methodA1, methodA2 should be placed after methodA (that uses them). Same with methodB methodB1, methodB2. I would expect to see something like that:

public class MyClass {
    methodA();
    methodB();
    methodC();

    public void methodA() {
        methodA1();
        methodA2();
    }

    private void methodA1() {
        do something;
    }
    private void methodA2() {
        do something;
    }

    public void methodB() {
        methodB1();
        methodB2();
    }

    public void methodB1() {
        do something;
    }

    public void methodB2() {
        do something;
    }
}

It's also suggested that you put your member variables at the top (thus not making the placement decision based on the access modifiers).

You may want to check Uncle Bob's books or videos to get some really good advice on writing clean code.

0
votes

If you were using just a text editor to read and change your code, then it might make sense to think about the order in which you declare your methods, but normally you'll be using a modern tool like Eclipse, which gives you an overview of the methods as well as allowing you to follow the call hierarchy, so where they are located is not important.

It's better to concentrate your effort on making your code as independent and abstract as possible, with methods that do one thing only, and classes which have a very clear function, and avoid at all costs to inflate it with work that should be done by another object.

Oh, and write good comments. That means explain why and not what you are doing.

-1
votes

If only one function calls a method, and it calls it only once, you can just use a single function.

If it calls it several times, place it right before the "parent" function without any line breaks in between. If readability will be affected (eg the javadocs will get messed up with a thousand useless functions) consider hiding them all under private with a util_/internaluse_//zzz prefix or parentname_ like methodA_methodA1

If it is called by several functions it is more of a utility function. So treat it as such. Several of these utility functions could become a utility class.

Also, personally, I use C++ style. so members, then A1, A2, A, B1, B2, B.