What is the most common way of ordering Java metho

2019-06-24 01:11发布

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 (i.e. first public, then protected, then private or the other way around)
  2. Order methods by dependency (i.e. 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?

5条回答
三岁会撩人
2楼-- · 2019-06-24 02:01

I tend to use option 1, but with a decent IDE it shouldnt really matter.

查看更多
对你真心纯属浪费
3楼-- · 2019-06-24 02:07

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.

查看更多
太酷不给撩
4楼-- · 2019-06-24 02:11

I think adding getters, setters, equals, hashCode and toString at the bottom is useful since that's usually not something a developer cares a lot about once it's in place.

查看更多
\"骚年 ilove
5楼-- · 2019-06-24 02:12

I would generally prefer option 2 - with a class overview in the IDE showing public and private methods and auto-complete only showing the visible ones for a class, it does matter less though, as @Visage mentions.

查看更多
姐就是有狂的资本
6楼-- · 2019-06-24 02:12

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."

查看更多
登录 后发表回答