views:

205

answers:

10

What is the best way to sort class members?

I'm in conflict with a team member about this. He suggests that we should sort the members alphabetically. I think it's better to organize in a semantic manner: important attributes first, related methods together, etc.

What do you think?

+11  A: 

I like semantic. Alphabetical doesn't seem to make a lot of sense to me, cause when you're looking for a member, you rarely know exactly what it's called. Also, if you're using any sort of naming convention (eg: Hungarian), alphabetical is going to lead to grouping by type, which may not be what you want.

Nick
After all, we don't write words in alphabetical order. Or, as my colleague says: "After all alphabetical don't in order we words write.""
S.Lott
Colleague your win is. :)
Nick
I that's think "Colleague is win your".
S.Lott
+3  A: 

Group related class members together. I think this will help other programmers understand your interface more easily when they see it for the first time.

Some also find it helpful to organize accessors and modifiers together in separate sections.

Jeff Miller
+2  A: 

I've studied this exact issue as part of my master's thesis.

An alphabetical organization or organization based on public/private is better for being able to find specific things. However, in some IDEs you can set the outline tool to sort alphabetically and to use special indicators for public/private.

My approach was to group methods based on what members they use: there is often a conceptual connection between methods that use the same fields.

I actually created a visualization from that, which helped to quickly navigate and understand the structure of huge classes.

Uri
Interesting idea about sorting/grouping by the use of members. Could suggest new encapsulations.
harpo
True. When I looked at popular APIs, that wasn't too common, but it's quite possible in (bad) user code.
Uri
Sounds like an interesting thesis! Is it available online?
fatcat1111
My thesis is available online at: http://www.cs.cmu.edu/~udekel/papers/udekel_mscthesis.pdfI did most of this stuff manually. I bunch of students built a very unstable tool about 6 years ago that used classfile analysis. IF I ever get the time, I'll build it using the new facilities in Eclipse.
Uri
+1  A: 

This is just my opinion, which I am sure will be unpopular, but the problem with semantic sorting is its subjective. Each person will have a different opinion of what methods should be close together.

Alphabetical has the advantage that it is entirely objective. It also reduces large diffs for small changes, which is common when one coder chooses a different semantic ordering.

Most IDEs have outlines, or hyperlinks to make navigation easier.

EDIT: A clarification- I still sort by public first to private, but alphabetical within the same access level. In fact, I don't do any sorting - I let my IDE resort the file for me when saving.

toolkit
+1  A: 

I never look for a member by going through the code. When I want to jump to a member definition, I either select it from the navigation bar / document outline / class view, or I right-click and select "Jump to definition". You don't need to sort the members if you have a decent IDE. This works very good in Visual Studio and the other IDE I use if needed, KDevelop, supports at least the basics of this.

Anyway, I tend to group members by functionality, i.e. all fields / properties / methods that are part of some specific functionality are together. And since classes shouldn't be too long, this is enough.

OregonGhost
A: 

Are you writing a phone book?

With a semantic approach you can easily show what are the most important methods. I generally go with Constructor, Destructor first, then important methods followed by getters and setters and eventually misc. methods. Finally, I take a similar approach for internal parts (private methods, attributes...).

Alphabetical order does not convey any useful information about your class. If you really want to see methods sorted alphabetically, you should rely on a function of your IDE.

Kenji Baheux
A: 

You can go from semantic to alphabetic by sorting the "methods display" in your IDE.

You can't go (automatically) from alphabetic to semantic.

Hence: semantic.

rlerallut
A: 

Assuming you are using a modern IDE, finding the method you want is rarely more than two mouse clicks away, so I am not sure what having a particular way of organizing your methods would get you. I do use stylecop (http://code.msdn.microsoft.com/sourceanalysis) which has me ordering by public / private / method / properties - I have found that to be anal enough.

The only time I ever truely thought this was important is when I wrote a very large jscript program and the editor at the time didn't offer any help in finding functions. Alphabetic organization was very helpful. When alphabatized, it isn't hard to figure out which way in the file you need to go to find a method. Semantic organization would have been completely unhelpful.

A: 

At a higher level, I would organize my class this way:

  1. Constructor
  2. Destructor
  3. Private Fields
  4. Properties
  5. Methods/Functions

Then for methods/functions I would break it down again by functionality. e.g. I would put methods that implement an interface into one region, I would put event handlers methods into one region, etc...

RWendi

RWendi
A: 

I guess I'm one of the oddball cases who favors alphabetical listings.

First and foremost, it's been my experience that grouping methods together "semantically" tends to be a time-sink. Now, if we're talking about grouping them by scope/visibility, that's another thing. But then, if the member changes it's scope, you have to sink time into moving the member to keep the code current. I don't want to have to waste time shuffling code around to observe a guideline like that.

I am also not a big fan of regions. When properties and methods are grouped by scope, they tend to shout out for enclosure in a region. But enclosing code in collapsing regions tends to hide badly written code. As long as you don't have to look at it, you won't be bothered to think about refactoring it to make it maintainable.

So, I favor alphabetical organization. It's simple, direct, and to the point. I'm not tempted to enclose groups into regions. And since the IDE makes it easy to leap to a function or property definition anyway, the physical layout of the code is moot. It used to be that you wanted folks to focus on your public members first. Modern IDEs make that largely a pointless argument in favor of scope-based layouts.

But the biggest advantage of alphabetical layouts is this: printed code samples during code reviews. And I use them alot. It makes finding a function or a property a snap. If you've ever had to wade through a lot of code to find a function or a property when things weren't just alphabetically listed, you'll know what I'm talking about.

But, as they say, those are my subjective views on the subject. Your mileage may vary.

Mike Hofer