Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm curious to hear opinions on this question that I've been debating with myself (and losing).

When you write a function (foo) and you need to call various sub functions, which in turn call other functions, how much thought do you give to where those functions are located.

E.g. you could keep all public methods near the top of your file and keep all privates at the bottom

public foo1()
{
}

public foo2()
{
}

And then after all publics are listed, you then list the privates.

OR, after each public method put the private methods that it uses right there.

public foo1()
{
bar1()
bar2()
bar3()
}

private bar1()
{
}

private bar2()
{
}

private bar3()
{
}

And then on with the next public
public foo2()
{

}


Option 2 seems better in that you can see the related functions right after they are used. But it gets messy, often times a number of public methods will share the same private methods etc.

Options 1 seems to be what I tend to do more in practice and with the advent of 'Go to Definition' and so on, it's less important than it once was to have called functions "right there".

Have you any preference?

Have you any other techniques?

I'm reminded of that scene from High Fidelity:

Dick: I guess it looks as if you're reorganizing your records. What is this though? Chronological?
Rob: No...
Dick: Not alphabetical...
Rob: Nope...
Dick: What?
Rob: Autobiographical.
Dick: No f*****g way.

-Rd
Posted
Updated 24-Sep-10 2:10am
v3
Comments
ARopo 24-Sep-10 8:38am    
Personnally I like to group both private, and public functions into logical regions e.g. all method that deal with a partular control on a form

Where you place methods is a function of the coding standard you are following. In most standards I have used public methods are above private and protected methods.
 
Share this answer
 
Comments
Richard A. Dalton 24-Sep-10 9:06am    
Yup.
But when you write the coding standards you end up having to answer questions like this yourself.

I'm inclined to use the Publics on Top approach, but I can certainly see the logic in grouping functions, perhaps combined with the use of Regions.

-Rd
When I started writing C programs I found it extremely tiresome to have to add function definitions to a header file for every function defined in a module below main(). I found that by moving them to the top of the file I could get away with not having them in a header file (assuming they were statics/locals), and so avoid the problem of having to change the signature in two places for minor modifications. I have never worked anywhere that enforced a standard on this so have always kept to this method. As with so many things it is largely a question of personal choice rather than a requirement of the language.
 
Share this answer
 
I group methods by according to their nature:

- Constructors
- Initialization of structures/variables
- Event handlers
- Methods according to functional category
- Uncategorized methods

I also (out of habit from coding in Pascal many years ago) put the most interally referenced methods at the top of a region:

C#
private void Helper1()
{
    // do something
}

private void Helper2()
{
    Helper1();
}

public void DoThis()
{
    Helper2();
}


Of course, corporate policy that dictates otherwise, or the current structure of the file being edited overrides my own coding style.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900