• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do you Order your Methods?

 
Wally Hartshorn
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any preferred order for methods in a class? Some possibilities include:

  • alphabetically
  • getters, then setters, then others (alphabetically)
  • getter/setter pairs, then others (alphabetically)
  • public methods, then protected, then default, then private
  • other

  • I realize that anything will work so long as we're consistent, but if there's a defacto standard that most people follow, I figure we might as well follow it.

    So, how do you order your methods?
     
    Ernest Friedman-Hill
    author and iconoclast
    Marshal
    Pie
    Posts: 24208
    35
    Chrome Eclipse IDE Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Of these, only the public/protected/private one seems practical at all; ordering methods alphabetically sounds like terrible busy work.
     
    Jim Yingst
    Wanderer
    Sheriff
    Posts: 18671
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I prefer grouping the methods by functionality. Generally I'll have a public method, followed by any private methods used by the previous public method. Then another public method, followed by private methods which are called by that public method. And so on. Or I might have several public methods in a row, followed by several private methods which are used by all of the preceding group of public methods. These methods may be static or not; that doesn't affect the order much, for me. The goal is to try to place methods near to the methods they call, or that call them. Usually there is no perfect solution to this, but I find it's easier to read if I try to get related methods close to each other.

    Note that this style is also recommended in Sun's Coding Conventions. You can take these or leave them; in other contexts I have no problem deviating from these conventions if I think it's worthwhile. But here I fully agree with their recommendation.

    Many IDEs allow you to display a list of method names in alphabetical order, and/or grouped by access level. So if you want this sort of list, you can obtain one easily enough, while leaving the source file itself in a different order (grouped by functionality, preferably). As EFH notes, sorting the file manually is probably not worth the effort. However there are code reformatting tools out there which can be used to reformat the source automatically, including putting the methods in a particular order if you think that's desireable. I don't recommend it, but you can.
    [ March 01, 2006: Message edited by: Jim Yingst ]
     
    Robert Hill
    Ranch Hand
    Posts: 94
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I use public/protected/private but also group them as type(get, set, ect). I will deviate like in a tree that searches recursivly, I will put the public search method and the private recursive method together.

    I think the main thing is to do it logically, and be consistant, and only break that convention when that convention doesn't make sense. As opposed to breaking it if something is slightly better.
     
    Stan James
    (instanceof Sidekick)
    Ranch Hand
    Posts: 8791
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I go with Jim on putting functionality together. I come from many years of structured stuff before object stuff and I still find "making code express what it's doing" is a lot like "functional decomposition" some times. If a method calls three other methods to get the job done I'll put them in the order they're called. It makes an imaginary calling tree within the class. And it doesn't work very well when methods are reused and called from several places. Then I just move them down somewhere.

    Getters and Setters are annoying noise. They go right to the bottom.

    F3 in Eclipse is so cool the order matters less all the time.
     
    Ilja Preuss
    author
    Sheriff
    Posts: 14112
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I prefer classes that are so small that method ordering basically is a non-issue.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic