Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

This question already has an answer here:

Whenever I am programming in an object-oriented language, I am always faced with what order and how to group the methods for an object. Are there any standards for this, or any suggestions?

share|improve this question

marked as duplicate by MichaelT, Telastyn, gnat, Kilian Foth, JeffO Jun 9 '13 at 17:34

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1  
voting to close as impossibly broad. –  Telastyn Jun 9 '13 at 3:53
    
@MichaelT Progr. by Intention is just one approach for grouping, this question is one level above that. –  Rafael Cichocki Jun 9 '13 at 5:53
1  
This is probably a question most programmers will at some point ask themselves. However, I think for the most part this is a matter of coding style and the preference of the programmer and the way in which they like to organise their code. –  Theomax Jun 9 '13 at 9:30

1 Answer 1

up vote 4 down vote accepted

I don't know of any standard, but when I write an object I like to write out the method names first in a functional grouping, in coarse order of execution, with internal methods immediately after the method they were first written to be called by.

Were I asked to re-organize an existing class, of my own or someone else's, my first concern would be to group similar methods together, and have them in rough order of execution frequency. Different method signatures, if allowed, would always be listed together and in order of increasing complexity.

So, if I had a Box class with a constructor and methods to move the box or add contents to it, I might organize them as follows.

  • Constructors first

    1. new Box()
    2. new Box(contents)
  • Main functional group second.

    1. getContents
    2. addItem
    3. removeItem
  • Secondary functional group third.

    1. pickUp
    2. putDown
    3. move
    4. moveLeft
    5. moveRight
    6. moveUp
    7. moveDown

Of course, this is just a style question. Anyone using your code won't see the public methods or properties in internal source order, and anyone having to maintain your code will hopefully use a tool that guides them through it.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.