Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class member order in source code

This has been asked before (question no. 308581), but that particular question and the answers are a bit C++ specific and a lot of things there are not really relevant in languages like Java or C#.

The thing is, that even after refactorization, I find that there is a bit of mess in my source code files. I mean, the function bodies are alright, but I'm not quite happy with the way the functions themselves are ordered. Of course, in an IDE like Visual Studio it is relatively easy to find a member if you remember how it is called, but this is not always the case.

I've tried a couple of approaches like putting public methods first but that the drawback of this approach is that a function at the top of the file ends up calling an other private function at the bottom of the file so I end up scrolling all the time.

Another approach is to try to group related methods together (maybe into regions) but obviously this has its limits as if there are many non-related methods in the same class then maybe it's time to break up the class to two or more smaller classes.

So consider this: your code has been refactored properly so that it satisfies all the requirements mentioned in Code Complete, but you would still like to reorder your methods for ergonomic purposes. What's your approach?

(Actually, while not exactly a technical problem, this is problem really annoys the hell out of me so I would be really grateful if someone could come up with a good approach)

like image 243
Tamas Czinege Avatar asked Jan 21 '09 10:01

Tamas Czinege


3 Answers

Actually I totally rely on the navigation functionality of my IDE, i.e. Visual Studio. Most of the time I use F12 to jump to the declaration (or Shift-F12 to find all references) and the Ctrl+- to jump back.

The reason for that is that most of the time I am working on code that I haven't written myself and I don't want to spend my time re-ordering methods and fields.

P.S.: And I also use RockScroll, a VS add-in which makes navigating and scrolling large files quite easy

like image 72
Dirk Vollmar Avatar answered Sep 30 '22 07:09

Dirk Vollmar


If you're really having problems scrolling and finding, it's possible you're suffering from god class syndrome.

Fwiw, I personally tend to go with:

class
{
  #statics (if any)

  #constructor

  #destructor (if any)

  #member variables

  #properties (if any)

  #public methods (overrides, etc, first then extensions)

  #private (aka helper) methods (if any)
}

And I have no aversion to region blocks, nor comments, so make free use of both to denote relationships.

like image 45
annakata Avatar answered Sep 30 '22 06:09

annakata


From my (Java) point of view I would say constructors, public methods, private methods, in that order. I always try to group methods implementing a certain interface together.

My favorite weapon of choice is IntelliJ IDEA, which has some nice possibilities to fold methods bodies so it is quite easy to display two methods directly above each other even when their actual position in the source file is 700 lines apart.

I would be careful with monkeying around with the position of methods in the actual source. Your IDE should give you the ability to view the source in the way you want. This is especially relevant when working on a project where developers can use their IDE of choice.

like image 26
Jeroen van Bergen Avatar answered Sep 30 '22 08:09

Jeroen van Bergen