简体   繁体   中英

What is the most common way of ordering Java methods?

Ordering Java methods is probably the least important code style issue ever, but I'm trying to develop a code style as similar to what most people do as possible. It's by far the most popular approach to first declare all fields and then all methods, so I'll only ask about methods.

Now I'm wondering how to order Java methods. I can think of two sensible basic approaches:

  1. Order methods by visibility (ie first public, then protected, then private or the other way around)
  2. Order methods by dependency (ie if method a() calls method b(), put them as closely together as possible)

As far as I see it, the second approach seems to be more popular by far. Nonetheless, in both cases there is the question of the direction , and it is not as clear what most people use. In the second approach, you can either place a() above or below b(). I think placing b() above a() (and eventually main() at the bottom of the class file) is more common in C, not sure about C++. The other way around is IMO better for reading, from top to bottom. What's the most common approach in Java? Any special rules about static fields/methods?

I gave up on "logically" when I discovered that "logically" is a relative term; what is logical for one programmer is not always logical for a different programmer. If I have to learn what your "logically sorted" means, then it fails as a "logically sorted" technique.

I prefer to alphabetize my methods and my data-members. I believe that alphabetical, psuedo-linear search the "natural" technique for people who think in English. For example, attempt to logically sort any of the following: an encyclopedia, a phone book, a contact email list.

The classic argument against the alphabet is [spoken with soviet era Russian accent] "Ve haf IDE for alfabet searchz! Is nots needed in kode!" My reply to such arguments is a varient of "Yes, Mr. Stalin. But not everybody uses an IDE to view code. Some people still use (and like) vi. I may not be one of those people, but I know they exist. Other people have learned that the page-up and page-down keys work nicely when the code is organized alphabetically."

我认为在底部添加getter,setter,equals,hashCode和toString是很有用的,因为这通常不是开发人员在它到位时非常关心的事情。

我倾向于使用选项1,但是使用一个不错的IDE它真的不重要。

I tend to use option 1 as well but also try to organize them logically.
toString at the bottom simple stuff at the top and working down to more specialized methods.

What is more important is you use proper JDoc comment format, then a decent IDE should be able to automatically generate the HTML jdoc's for you or you can use their app. After you have proper documentation your method order is not as much of an issue.

我通常更喜欢选项2 - IDE中的类概述显示公共和私有方法,自动完成仅显示类的可见方法,但它确实不重要,正如@Visage所提到的那样。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM