Question

Are there any conventions as to whether a method called by another method should generally be above or below it? E.g. say caller() was refactored into two methods - where would be the more standard place out of aboveCaller() or belowCaller()?

private void aboveCaller() { /*...here?...*/ }

public void caller() {
    aboveCaller();
    belowCaller();
}

private void belowCaller() { /*...or here?...*/ }

Although this is in Java, it is a general programming question and not really aimed at a specific language. (Apologies if it has been asked before - tried searching on here but didn't come up with anything).

Was it helpful?

Solution

Following the top-down design, I first write the methods calls, then implement them, so they'll be under.

Others prefer them to be above the caller since it's clearer (You first encounter the implementation, then see the call).

One thing for sure, follow your convention.

OTHER TIPS

I don't think there is a convention. However, I usually put belowCaller below. It's a supporting function of the main function and I don't want to look at it until I come across it in the main function's content. That being said, I put all public functions first, followed by all private functions. I put class variables at the top. Also, javadoc for each non obvious function will help a lot in understanding the code.

20 people are going to answer this question, and you're going to end up with 25 different answers.

I don't think there's a standard convention, and in Java it doesn't matter. You could list them alphabetically, or group related methods together, or list private methods first and then public methods, or group setters first and then getters, or list getters and setters together in pairs, etc.

You should do whatever feels most natural to you, unless your professor or boss has defined another convention.

Edit: to see just how arbitrary it is, take a look at all the similar questions in the "related" bar to the right.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top