Code Formatting - Method Organization

Started by
3 comments, last by Joshua D Farrow 11 years ago

Is there a way that my methods should be organized?

Ex: By order that they are called, by name, by the order that they pop up in my brain (haha).

I currently use NetBeans as my primary IDE. If anyone else uses it, do you know if there is a way of using the GUI to move methods around, something that is the equivalent to moving folders/files around in Windows Explorer/Finder? I know that it has a handy method list that shows all the current methods, as well as allowing the coder to quickly move between methods by simply clicking on the one of their choice. However, there doesn't seem to be a way to reorganize the methods, short of simply copying and pasting them into place.

Advertisement

I place constructors at the top the class and then I'll usually group public and private methods together and within those groups I'll try to keep closely related methods (accessor methods, for example) in close proximity to one another. Granted, all methods in a class should be related but some are more so than others. wink.png

In regards to NetBeans' refactoring options, I don't believe you can reorder methods in a class automatically.

Personally i structure my classes as so:

public class FooBar
{
    //Static Fields
    public static int SomeNumber = 32;
    private const string SomeString = "Blllarrghhh";
    
    //Fields
    public string MyName;
    
    // Properties (C#)
    public int MyAge { get; set;}
    
    //Constructors
    public FooBar()
    {
    
    }
    
    
    //Methods
    private void SetName() { ... }
    public void GetName { ... }
    
    public void IncrementAge() {... }
    
    public int SomeReallyLongFunction(int param1, int param2, int param3, string param4, object[] param5) { ... }
 
    #region Any Interface Implementations
    #endregion

}

The way i order functions is as follows:

- I Try to keep Private functions at the top of the methods section and public below

- Group methods todo with the same operation (E.g. Set/Get Name)

- Order them by NUmber of parameters where possible

- Interface implementations at the bottom of the class (regardless of Methods / Fields)

With most IDE's it isn't necessary to organize methods at all, since you have several representations of the complete class structure and you can easily jump to the method you want.

That said however, it is possible that not everybody reading your code will use an IDE. So it pays off to organize your code in some way (other than how they popup in your head ;)).

I actually have serveral different ways of organizing my code, they depend on the language (C#, C++, PHP), the kind of project (work related, hobby or learning) and the mood I am in... but I try to organize methods with the same functionality together.

Typicly I don't mark regions of functionality with comments or the C# #region keyword. (because I think unnecessary comments are evil...)

Typicly I don't mark regions of functionality with comments or the C# #region keyword. (because I think unnecessary comments are evil...)

After the responses about using comments on my other post, I went back and looked at all of the comments that I had placed within my code. For the majority of them, all it took was re-reading them to realize that they were completely unnecessary laugh.png

This topic is closed to new replies.

Advertisement