• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

How do you Order your Methods?

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
     
    author and iconoclast
    Posts: 24207
    46
    Mac OS X Eclipse IDE Chrome
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    Wanderer
    Posts: 18671
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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 ]
     
    Ranch Hand
    Posts: 94
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    (instanceof Sidekick)
    Posts: 8791
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    author
    Posts: 14112
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I prefer classes that are so small that method ordering basically is a non-issue.
     
    If you two don't stop this rough-housing somebody is going to end up crying. Sit down and read this tiny ad:
    Gift giving made easy with the permaculture playing cards
    https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
    reply
      Bookmark Topic Watch Topic
    • New Topic