What is the most common way to organize Java methods?

Ordering Java methods is perhaps the least important issue in code style, but I'm trying to develop a code style similar to what most people do. This is by far the most popular approach for first declaring all fields and then all methods, so I will only ask about the methods.

Now I am wondering how to order Java methods. I can think of two reasonable basic approaches:

  • Ordering methods by visibility (i.e. first open, then secure, then private or vice versa)
  • Dependency ordering methods (i.e. if the a () method calls the b () method, place them as close to each other as possible)

As far as I understand, the second approach seems to be more popular far. However, in both cases there is the question of direction , and it is not so clear what most people use. In the second approach, you can either put () above or below b (). I think placing b () over a () (and ultimately main () at the bottom of the class file) is more common in C, but not sure about C ++. Another way is IMO is better to read from top to bottom. What is the most common approach in Java? Any special rules about static fields / methods?

+4
source share
5 answers

Usually I prefer option 2 - with an overview of the class in the IDE, showing public and private methods and automatically terminating, showing only visible to the class, but that matters, though, as @Visage mentions.

0
source

I abandoned “logically” when I discovered that “logically” is a relative term; which is logical for one programmer, not always logical for another programmer. If I need to know what your “logically sorted” means, then it fails as a “logically sorted” method.

I prefer to use my methods and my data in alphabetical order. I believe that the alphabetical, pseudo-linear search for a “natural” technique is for people who think English. For example, try logically sorting any of the following: encyclopedia, phone book, contact list for contacts.

The classic argument against the alphabet [speaks with a Russian accent of the Soviet era] "Ve haf IDE for alfabet searchz! No code needed!" My answer to such arguments is a kind of "Yes, Mr. Stalin, but not everyone uses the IDE to view the code. Some people still use (etc.) vi. Maybe I'm not one of these people, but I know that they exist. Other people have learned that the up and down keys work well when the code is organized in alphabetical order. "

+6
source

I think the addition of getters, seters, equals, hashCode and toString at the bottom is useful, since this is usually not something that the developer cares a lot about once he is in place.

+1
source

I prefer to use option 1, but with a decent IDE it shouldn't matter.

0
source

I also use option 1, and also try to organize them logically. toString at the bottom of the simple thing at the top and working with more specialized methods.

More importantly, you are using the correct JDoc comment format, then a decent IDE should be able to automatically generate jdoc HTML for you, or you can use your application. Once you have the proper documentation, your order methods are not many.

0
source

All Articles