views:

63

answers:

3

Sorting methods in class file by access modifiers is good or wrong habit?

Which are another methods for structure of class file?

+1  A: 

It depends. You could group them logically, which is the best way IMHO. I believe you could ask one dozen people and get one dozen different answers. It's like the good old 'Should I create #regions for methods/internal methods/properties/fields etc.'-question.

dkson
+2  A: 

The important thing with coding standards as that they are documented, well known by your team and that they are applied consistently.

It does also help if they aren't too "off-the-wall" compared to the coding standards adopted across the industry.

In this case, the company I am working at currently has a similar policy, which is the place the class in a particular order, with properties, constructors, public methods then private methods.

I have also worked in locations where the private methods are placed close the public methods that consume them - but I have to admit that this tends to get messy as the whole idea of extracting the logic into a separate method was to get re-use.

Sohnee
Properties followed by the constructors makes most sense to me since this is also the order the object gets initialized/executed. Good for debugging thought.
Yves M.
A: 

This is the sorting order that StyleCop uses (if I recall correctly):

  1. variables (All constant and readonly private fields must be placed before all non-constant, non-readonly private fields)
  2. constructors (static first)
  3. enums
  4. interfaces
  5. properties
  6. methods
  7. classes

Also:

  • Static members should always be placed before instance members of the same visibility.
  • Members should be sorted with: public before internal before protected before private.

Personally I removed the rule that static members should be placed before non-static members, because this often conflicts with good readability, because I like to be able to read my class like a book, but also like to adhere the rules of FxCop (yet another tool) that points me at instance methods that don't use the this argument.

Steven
@Steven: Then StyleCop has quite a strange order, with interfaces somewhere in the middle, but classes at the end, I cannot figure out what the purpose of this ordering should be. Enums, classes and interfaces are used to declare the type of a variable or field, while variable, constructors, properties and methods somehow make up the class. StyleCop seems to mix up all of these randomly and then define that as "style".
chiccodoro
@Chiccodoro: I was wrong. Enums must be placed after the constructors, but before the interfaces. Classes still at the end (I updated my answer to reflect this). I think the thing is that StyleCop tries to put definition above implementation. Enums and interfaces are definition, classes are implementation. Also note that it is not very likely that you would have any public enums, interfaces and classes INSIDE a class (FxCop warns you about this).
Steven