Two Laptop Bag
The moose likes Beginning Java and the fly likes How do you Order your Methods? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How do you Order your Methods?" Watch "How do you Order your Methods?" New topic

How do you Order your Methods?

Wally Hartshorn
Ranch Hand

Joined: Jan 30, 2003
Posts: 77
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?

    Wally Hartshorn
    Ernest Friedman-Hill
    author and iconoclast

    Joined: Jul 08, 2003
    Posts: 24199

    Of these, only the public/protected/private one seems practical at all; ordering methods alphabetically sounds like terrible busy work.

    [Jess in Action][AskingGoodQuestions]
    Jim Yingst

    Joined: Jan 30, 2000
    Posts: 18671
    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 ]

    "I'm not back." - Bill Harding, Twister
    Robert Hill
    Ranch Hand

    Joined: Feb 24, 2006
    Posts: 94
    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

    Joined: Jan 29, 2003
    Posts: 8791
    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.

    A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
    Ilja Preuss

    Joined: Jul 11, 2001
    Posts: 14112
    I prefer classes that are so small that method ordering basically is a non-issue.

    The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    subject: How do you Order your Methods?
    It's not a secret anymore!