Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where do you add new methods?

Tags:

coding-style

When you add a new method to a class where do you put it? At the end of the class...the top? Do you organize methods into specific groupings? Sorted alphabetically?

Just looking for general practices in keeping class methods organized.

Update When grouped where do you add the new method in the group? Just tack on the end or do you use some sort of sub-grouping, sorting?

Update 2 Mmmm...guess the question isn't as clear as I thought. I'm not really looking for class organization. I'm specifically interested in adding a new method to an existing class. For example:

public class Attendant
{
    public void GetDrinks(){}
    public void WelcomeGuests(){}
    public void PickUpTrask(){}
    public void StrapIn(){}
}

Now we're going to add a new method PrepareForCrash(). Where does it go? At the top of the list, bottom, alphabetically or near the StrapIn() method since it's related.

like image 669
Paul Alexander Avatar asked Aug 09 '26 01:08

Paul Alexander


2 Answers

Near "StrapIn" because it's related. That way if you refactor later, all related code is nearby.

Most code editors allow you to browse method names alphabetically in another pane, so organizing your code functionally makes sense within the actual code itself. Group functional methods together, makes life easier when navigating through the class.

like image 174
razzed Avatar answered Aug 12 '26 14:08

razzed


For goodness sake, not alphabetically!

I tend to group my functions in the order I expect them to be called during the life of the object, so that a top to bottom read of the header file tends to explain the operation of the class.

like image 34
Alex Brown Avatar answered Aug 12 '26 15:08

Alex Brown



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!