views:

439

answers:

7

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  A: 

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.

annakata
No god classes here: as I mentionned, everything is refactored properly and there are not oversized classes but I still feel that I am not really happy with the way class members are organized.
DrJokepu
Methods have a network kind of relationship, not strict heirarchal one, so I think any attempt to place "related" items together is kind of flawed, especially when "find all instances" is so good in VS. I find it more relevant to see the things which share scope together.
annakata
+3  A: 

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

0xA3
Oh yes RockScroll is great help, I use it all the time. Unfortunately, it tends to crash Visual Studio a few times a day.
DrJokepu
Are you sure RockScroll is the cause for the crash? Never experienced that with VS2008...
0xA3
divo: Oh yes, definitely. The crashes started right after I installed RockScroll. It only happens when I close a solution so it's not that annoying, but it's definitely RockScroll.
DrJokepu
@DrJokepu, I've never had any problems either - are you sure you haven't other add-ins installed?
Benjol
@Benjol: Nope, but it's definitely RockScroll. Uninstalling it made the crashes go away. Maybe I'm just unlucky.
DrJokepu
@DrJokepu, @Benjol: Btw, I moved to MetalScroll, an open-source variant with some additional configuration options. Get it here: http://code.google.com/p/metalscroll/
0xA3
@0xA3: Oh awesome that looks really cool! Thanks!
DrJokepu
@DrJokepu, @0xA3. I've been jinxed, I tried MetalScroll yesterday, gave up in favour of rockscroll, and now rockscroll is bombing on me. Switching to MetalScroll (won't be available in VS2010, apparently, sob...)
Benjol
+1  A: 

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.

Jeroen van Bergen
I usually try to avoid regions or folding method bodies because I hate clicking on those tiny boxes :)
DrJokepu
Anyway, consider this: you put your abstract methods at the top, maybe even over the constructor, since it makes sense, they do not have function bodies, right? Now where do you put virtual methods (that is, methods that are expected to be overridden)? Next to abstract methods? Or somewhere else?
DrJokepu
+1  A: 

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.

Treb
+1  A: 

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.

Michael Buen
Yes, as long as you keep it bottom-up, without diving in top-down in between ;-)
Treb
I miss nested functions too in C#... There is probably some reason why C# does not have nested functions but I still wished they were supported.
DrJokepu
A: 

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.
finnw
A: 

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?

toebens