Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Possible Duplicate:
How do you normally layout a class's regions?

It often happens that I'm stuck in front of my code editor searching the best place to put a new function.

I've no norm nor rules to do that. My question is simple : does some exists, if not how would order your function to structure your files or classes : by type (private, public, ...), by relations or by something else ?

share|improve this question

marked as duplicate by ChrisF Jan 23 '12 at 15:30

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
The language really matters here, since some standard styles of using different languages separate the declarations from the definitions and others do not. –  Jonathan Henson Feb 9 '12 at 4:45
    
When you are constrained by the language this question doesn't matter, does it ? –  AsTeR Feb 9 '12 at 17:48
    
It certainly matters. In my opinion it matters much more in C++ and C than in C# and JAVA, since in the former I am trying to find the implementations of an interface, structure, or class that were declared in a different file. In C# and JAVA, the order is much less important since the declarations are implicit in the definitions themselves and only in one place. –  Jonathan Henson Feb 9 '12 at 17:55

1 Answer 1

up vote 3 down vote accepted

Honestly, I don't think this matters too much. Most modern IDE's will give you a listing on some panel somewhere of your function and you can usually choose multiple sort orders, includes alphabetic, scope (public/private/etc...) or as-is in the file.

If you are putting a lot of effort into this, relax! It's not worth it. The only place it might matter is languages where private functions must be delcared before they can be used. or maybe some tool/IDE that doesn't give you any nice and sorted file outline.


If you don't want to rely on your tools/IDEs, I sometimes do the layout by type (such as member variables, structs/user-defined-types, properties, functions, methods) and then by scope. Also, I tend to put the constructors (public OR private) right before the first public method. When there's overloads, I list them in order of the fewest arguments to most. So it might go something like this;

  • private member
  • protected member
  • private type
  • public type
  • protected enum
  • private function
  • public constructor
  • public property
  • public function

I don't know exactly where I picked this method up over the years, but I see similar layouts often in other people's code. I don't always strictly enforce it because sometimes it's just easier to use tools to navigate my code.

share|improve this answer
    
Thanks. I can sleep without an answer but into big classes that's a problem. I would like not to rely on my IDES or tools. –  AsTeR Jan 23 '12 at 16:30
    
@AsTeR: I updated with a general layout I often use. –  FrustratedWithFormsDesigner Jan 23 '12 at 16:42
    
I like your order, however I wouldn't recomend relying on the IDE unless this is higher level code that is only written in an IDE. For instance, I will probably never edit significant amounts of C# code in anything other than Visual Studio or large amounts of JAVA in anything other than Eclipse. However, c and c++? I may sometimes use VS or Eclipse, but I often need to open the code via vi, emacs, or a text editor. The order really matters to me at that point. –  Jonathan Henson Feb 9 '12 at 4:42

Not the answer you're looking for? Browse other questions tagged or ask your own question.