Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a standard way to organize methods within a class? [duplicate]

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?

like image 502
Matthew Avatar asked Jan 28 '10 16:01

Matthew


People also ask

When you are organizing the elements in a class which order is preferred?

Within a class, struct, or interface, elements must be positioned in the following order: Fields. Constructors. Finalizers (Destructors)


1 Answers

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.

like image 105
Joey Avatar answered Sep 20 '22 14:09

Joey