views:

333

answers:

12

There seem to be many different ways of organizing methods in a class. I could group methods by access, and order them alphabetically. I could group related methods together. I could use a mix of the two, or something else entirely. Is there a standard way to approach this? If not, how do you approach it?

A: 

No, most people just group methods by logical similarity, or not at all. It doesn't really matter - classes shouldn't get too big, and with a decent IDE you can just ctrl+click to goto to any method definition anyways.

BlueRaja - Danny Pflughoeft
A: 

I suggest first grouping by interface they come thru, with a nice comment saying the interface name - because these are non-obvious.

Then, by relation, and only then sorting alphabetically.

Use a lot of blank lines to reduce the reader's cognitive load.

Pavel Radzivilovsky
That's almost exactly how I organize classes, too. Additionally, if a method is overloaded, I order all its variants roughly by increasing number of parameters.
stakx
+1  A: 

I group my method by functionality, ranging from most generic to most concrete, all that inside a region, so I can hide unnecessary details.

Rubens Farias
+1 for `#region`.
Wim Hollebrandse
+1  A: 

I do not order them by access modifier, nor do I order them alphabetically. (I think this is quite a burden when you add or rename a method in that class ... ).

If necessary, I group them by functionality. I mean: I put related methods toghether.

But, if you follow the SRP (Single Responsability Principle), then I think that ordering methods is in most of the circumstances a non-issue; since, when you follow SRP, your classes wouldn't have very much methods.

Frederik Gheysels
A: 

Microsoft's StyleCop tool does a nice job of defining element order within a class. You can also write extensions to the tool to conform to your company's coding standards. That being said, StyleCop is a good starting point.

Jesse C. Slicer
A: 

Since Visual Studio has tools for navigating quickly to methods by name, I prefer to put private member variables and constructors close to the top and group methods by functionality (ie, helper methods are close by to the methods that call them). I also group properties together. If a class becomes substantial, I make use of #region directives to show the organization.

plinth
A: 

I'm not sure there's a consistently standard way... but I group related methods together, perhaps sub-grouped by accessor. Makes it the easiest for finding related things later down the track.

Marcus
+9  A: 

StyleCop enforces some things here:

Within a class, struct, or interface, elements must be positioned in the following order:

  • Fields
  • Constructors
  • Finalizers (Destructors)
  • Delegates
  • Events
  • Enums
  • Interfaces
  • Properties
  • Indexers
  • Methods
  • Structs
  • Classes

Furthermore, elements are ordered by access:

  • public
  • internal
  • protected internal
  • protected
  • private

As well as a few other rules:

  • Contants have to appear before fields
  • static elements have to appear before instance elements.

This might be a good baseline to start. As for additional ordering rules, I usually group related methods together.

Joey
This is exactly what I was looking for. Is `#region` recommended to separate each of these areas, or is that considered over-using `#region`?
Matthew
Interesting. I do the top 3, element ordering, and putting constants and static elements first by habit, but not the others.
R. Bemrose
I used to use a lot of #regions in my code, for the code-folding function in VS. But inevitably I would end up removing them because it's just a pain to not be able to quickly scan my code if everything is folded up. Eventually I just gave up on it. Your mileage may vary.
Doug R
I hate how regions collapse by default, and the only choice you have is either 1) have them collapse by default or 2) have them not just stay open, but also not have any of the little boxes available to manually collapse things. First thing I do with `#region` marked code is remove them all.
280Z28
@Doug: Even if you don't use cold-folding, `#region` is nice for letting you jump around your code quickly (at least in MonoDevelop, I assume VS does the same thing).
Matthew
@280Z28 Ctrl+M, Ctrl+L
Rob Fonseca-Ensor
Honestly, I find regions visual noise and a code smell. They hide code from sight. Why is the code hidden? From what I've seen it's to make the class more navigatable. I'm all for organizing your classes, but if you can't open your favorite IDE and see how the code is organized, the class is too large and more than likely needs to be broken down into smaller components.
Chuck Conway
@Rob: Wow, that command is pretty terribly named. Sounds like it would invert it - regions expanded by methods collapsed. I'll still prefer ordering my file structure to using regions, but this gives me a workable solution when i'm having to deal with code from other people.
280Z28
#regions were created to hide auto-generated code. Partial classes achieve that now. I think they obfuscate more than help. There's plenty of find functionality in dev tools. #regions are just clutter, imho.
Jeff Yates
A: 

I tend to find grouping of related methods far more useful when reading someone else's source code, than some arbitrary pseudo-order such as alphabetical.

Although the book is specifically about Java, Uncle Bob's Clean Code contains some excellent general principles for readability in OO classes. In particular, he uses the metaphor of a well-written newspaper article, with a headline at the top followed by a synopsis paragraph and then increasing detail as you read further down the article. By aiming for a similar top-down structure in your classes, it makes the task of reading or understanding your code much easier for others.

Phil Booth
A: 
Partha Choudhury
+2  A: 

I use NArrange which does most of the things I want: Group fields, methods... alphabetically and by access modifiers. It also enforces some Style Cop rules (e.g. order of element types, location of using statements). NArrange is configurable to quite some degree, you do not have to live with the default configuration if you do not like it.

In Addition I use partial classes if I want to group certain methods that somehow belong together (e.g. events) and I group these files with another tool: VsCommands.

Stefan Egli
+2  A: 

Whatever you do, put it in your standards and be consistent. We use a custom Regionerate configuration to order our methods. Everyone on the team uses the same configuration.

EDIT: We're now using ReSharper's Code Cleanup with a custom Type Members Layout.

TrueWill