Class member order in source code

笑着哭i 提交于 2019-12-21 10:18:13

问题


This has been asked before (question no. 308581), but that particular question and the answers are a bit C++ specific and a lot of things there are not really relevant in languages like Java or C#.

The thing is, that even after refactorization, I find that there is a bit of mess in my source code files. I mean, the function bodies are alright, but I'm not quite happy with the way the functions themselves are ordered. Of course, in an IDE like Visual Studio it is relatively easy to find a member if you remember how it is called, but this is not always the case.

I've tried a couple of approaches like putting public methods first but that the drawback of this approach is that a function at the top of the file ends up calling an other private function at the bottom of the file so I end up scrolling all the time.

Another approach is to try to group related methods together (maybe into regions) but obviously this has its limits as if there are many non-related methods in the same class then maybe it's time to break up the class to two or more smaller classes.

So consider this: your code has been refactored properly so that it satisfies all the requirements mentioned in Code Complete, but you would still like to reorder your methods for ergonomic purposes. What's your approach?

(Actually, while not exactly a technical problem, this is problem really annoys the hell out of me so I would be really grateful if someone could come up with a good approach)


回答1:


Actually I totally rely on the navigation functionality of my IDE, i.e. Visual Studio. Most of the time I use F12 to jump to the declaration (or Shift-F12 to find all references) and the Ctrl+- to jump back.

The reason for that is that most of the time I am working on code that I haven't written myself and I don't want to spend my time re-ordering methods and fields.

P.S.: And I also use RockScroll, a VS add-in which makes navigating and scrolling large files quite easy




回答2:


If you're really having problems scrolling and finding, it's possible you're suffering from god class syndrome.

Fwiw, I personally tend to go with:

class
{
  #statics (if any)

  #constructor

  #destructor (if any)

  #member variables

  #properties (if any)

  #public methods (overrides, etc, first then extensions)

  #private (aka helper) methods (if any)
}

And I have no aversion to region blocks, nor comments, so make free use of both to denote relationships.




回答3:


From my (Java) point of view I would say constructors, public methods, private methods, in that order. I always try to group methods implementing a certain interface together.

My favorite weapon of choice is IntelliJ IDEA, which has some nice possibilities to fold methods bodies so it is quite easy to display two methods directly above each other even when their actual position in the source file is 700 lines apart.

I would be careful with monkeying around with the position of methods in the actual source. Your IDE should give you the ability to view the source in the way you want. This is especially relevant when working on a project where developers can use their IDE of choice.




回答4:


My order, here it comes.

  • I usually put statics first.

  • Next come member variables and properties, a property that accesses one specific member is grouped together with this member. I try to group related information together, for example all strings that contain path information.

  • Third is the constructor (or constructors if you have several).

  • After that follow the methods. Those are ordered by whatever appears logical for that specific class. I often group methods by their access level: private, protected, public. But I recently had a class that needed to override a lot of methods from its base class. Since I was doing a lot of work there, I put them together in one group, regardless of their access level.

My recommendation: Order your classes so that it helps your workflow. Do not simply order them, just to have order. The time spent on ordering should be an investment that helps you save more time that you would otherwise need to scroll up and down.

In C# I use #region to seperate those groups from each other, but that is a matter of taste. There are a lot of people who don't like regions. I do.




回答5:


I place the most recent method I just created on top of the class. That way when I open the project, I'm back at the last method I'm developing. Easier for me to get back "in the zone."

It also reflected the fact that the method(which uses other methods) I just created is the topmost layer of other methods.

Group related functions together, don't be hard-pressed to put all private functions at the bottom. Likewise, imitate the design rationale of C#'s properties, related functions should be in close proximity to each other, the C# language construct for properties reinforces that idea.



P.S.
If only C# can nest functions like Pascal or Delphi. Maybe Anders Hejlsberg can put it in C#, he also invented Turbo Pascal and Delphi :-) D language has nested functions.


回答6:


A few years ago I spent far too much time pondering this question, and came up with a horrendously complex system for ordering the declarations within a class. The order would depend on the access specifier, whether a method or field was static, transient, volatile etc.

It wasn't worth it. IMHO you get no real benefit from such a complex arrangement.

What I do nowadays is much simpler:

  1. Constructors (default constructor first, otherwise order doesn't matter.)
  2. Methods, sorted by name (static vs. non-static doesn't matter, nor abstract vs. concrete, virtual vs. final etc.)
  3. Inner classes, sorted by name (interface vs. class etc. doesn't matter)
  4. Fields, sorted by name (static vs. non-static doesn't matter.) Optionally constants (public static final) first, but this is not essential.



回答7:


i pretty sure there was a visual studio addin that could re-order the class members in the code.

so i.e. ctors on the top of the class then static methods then instance methods... something like that

unfortunately i can't remember the name of this addin! i also think that this addin was for free! maybe someone other can help us out?




回答8:


My personal take for structuring a class is as follows:

I'm strict with

  • constants and static fields first, in alpha order
  • non-private inner classes and enums in alpha order
  • fields (and attributes where applicable), in alpha order
  • ctors (and dtors where applicable)
  • static methods and factory methods
  • methods below, in alpha order, regardless of visibility.

I use the auto-formatting capabilities of an IDE at all times. So I'm constantly hitting Ctrl+Shift+F when I'm working. I export auto-formatting capabilities in an xml file which I carry with me everywhere.

It helps down the lane when doing merges and rebases. And it is the type of thing you can automate in your IDE or build process so that you don't have to make a brain cell sweat for it.

I'm not claiming MY WAY is the way. But pick something, configure it, use it consistently until it becomes a reflex, and thus forget about it.



来源:https://stackoverflow.com/questions/464751/class-member-order-in-source-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!