2

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?

Addis
  • 2,480
  • 2
  • 13
  • 21
  • Proxy pattern? https://en.wikipedia.org/wiki/Proxy_pattern – Stefan Aug 09 '16 at 09:18
  • 1
    public methods first, then protected, then private. makes it a bit easier to read. – pecks Aug 09 '16 at 09:20
  • In a recent study on program comprehension, the results indicated that the answer is probably: any consistent ordering. – Elazar Aug 09 '16 at 09:27
  • @Stefan proxy pattern doesn't seem to address my question or I may not have understood it well. – Addis Aug 09 '16 at 09:55
  • @Elazar, do you mean that as far as I'm consistent in my style it's Ok even if it differs from others? – Addis Aug 09 '16 at 09:56
  • My question is not on how to sequence public, private, protected, static,..members so it's not duplicate. – Addis Aug 09 '16 at 10:03

3 Answers3

2

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.

Marcucciboy2
  • 3,156
  • 3
  • 20
  • 38
sestus
  • 1,875
  • 2
  • 16
  • 16
  • You placed methodA1 & methodA2 after methodA not before. – Addis Aug 09 '16 at 09:45
  • Sorry @Addis typo. I edited my answer. MethodA1 and methodA2 should be placed after methodA (that uses them) – sestus Aug 09 '16 at 09:49
  • This is completely opposite of what it should be. When you read instruction manual, you don't have stuff like "take the device apart and touch this cable" and further: "but first disconnect the device from electricity". – Jacek Krysztofik Jan 14 '22 at 12:48
0

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

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.

gia
  • 757
  • 5
  • 19