0
\$\begingroup\$

What is your opinion on method ordering?

Should it be public methods at the top followed by private methods?

or

Should it be linear order so that methods calling methods stack on top of each other?

Which do you think is better for maintainability / readability?

Examples

Public first:

public class Main {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        //
    }

    @Override
    protected void onResume() {
        startUploadActivity();
    }

    @FromXML
    public void onSignInClick(View button){
        startAuthorisationForYouTube();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(resultIsASignInAttempt(requestCode) && signedInSucccessfully(resultCode)){
            dealWithResult(data);
        } else
        if(resultIsASignInAttempt(requestCode) && signInFailed(resultCode)){
            startRefusalActivity();
        }
    }

@Override
protected void onStop() {
    super.onStop();
}


    private void startUploadActivity() {
        //
    }

    private void startAuthorisationForYouTube() {
        //
    }

    private static boolean resultIsASignInAttempt(int requestCode) {
        //
    }

    private static boolean signedInSucccessfully(int resultCode) {
        //
    }

    private static boolean signInFailed(int resultCode) {
        //
    }

    private void dealWithResult(Intent data) {
        //
    }

    private static Tokens getAccessTokens(Intent data) {
        //
    }

    private void startRefusalActivity() {
        //
    }
}

Calling order:

public class Main {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        //
    }

    @Override
    protected void onResume() {
            startUploadActivity();

    }

    private void startUploadActivity() {
        //
    }

    @FromXML
    public void onSignInClick(View button){
        startAuthorisationForYouTube();
    }

    private void startAuthorisationForYouTube() {
        //
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(resultIsASignInAttempt(requestCode) && signedInSucccessfully(resultCode)){
            dealWithResult(data);
        } else
        if(resultIsASignInAttempt(requestCode) && signInFailed(resultCode)){
            startRefusalActivity();
        }
    }

    private static boolean resultIsASignInAttempt(int requestCode) {
        //
    }

    private static boolean signedInSucccessfully(int resultCode) {
        //
    }

    private static boolean signInFailed(int resultCode) {
        //
    }

    private void dealWithResult(Intent data) {
        //
    }

    private static Tokens getAccessTokens(Intent data) {
        //
    }

    private void startRefusalActivity() {
        //
    }

@Override
protected void onStop() {
    super.onStop();
}
}
\$\endgroup\$
3
  • \$\begingroup\$ I think this is slightly off topic but on a personal note, I tend to group my methods by public, private, protected regions. But everyone's different and probably comes down to the accepted standards of the project TBH. \$\endgroup\$
    – dreza
    Oct 10 '12 at 19:20
  • 1
    \$\begingroup\$ Yeah so if I start the project I choose the standard :-) I don't see why this was closed as not constructive :-/ clearly those people don't have enough OCD over their code \$\endgroup\$
    – Blundell
    Oct 10 '12 at 20:13
  • \$\begingroup\$ haha I agree, I am also pretty OCD about code beauty \$\endgroup\$ Oct 11 '12 at 7:41
1
\$\begingroup\$

I don't have strong feelings, but I keep methods in calling order. The argument for "public up top" seems to be that someone wants to see what methods they can call on an Object as opposed to what a particular method does. However in today's world, this is generally accomplished by an IDE or javadoc. If that's not possible, the time lost jumping through a file looking for the public methods is minimal.

When I'm looking at code for a class I haven't written, I'm inevitably trying to find out how it works. And when reading the content of a method, jumping to private methods becomes more important, and slightly more time is lost if the class isn't ordered for callability.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.