Jump to content

  • Log In with Google      Sign In   
  • Create Account


Code Formatting - Method Organization


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 litta_gator   Members   -  Reputation: 116

Like
0Likes
Like

Posted 18 April 2013 - 01:32 PM

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.



Sponsor:

#2 Indifferent   Members   -  Reputation: 521

Like
1Likes
Like

Posted 18 April 2013 - 03:59 PM

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.



#3 Andy474   Members   -  Reputation: 633

Like
1Likes
Like

Posted 19 April 2013 - 04:14 AM

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)



#4 David Perfors   Members   -  Reputation: 222

Like
1Likes
Like

Posted 23 April 2013 - 11:34 AM

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...)



#5 litta_gator   Members   -  Reputation: 116

Like
0Likes
Like

Posted 25 April 2013 - 12:02 PM

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






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS