How do you order your class methods?

Just getting some layout ideas from other fellow devs.

If you have a class with many methods, how do you try to organize them?
Do you go alphabetical? By related function? Order in which they were
added?

On Fri, Aug 10, 2012 at 5:03 PM, masta Blasta [email protected]
wrote:

If you have a class with many methods, how do you try to organize them?

First, I try to avoid having classes with many methods. I can’t
remember having a Ruby class with so many methods that I started to
seriously think about the layout of them.

Do you go alphabetical? By related function? Order in which they were
added?

I usually try to group them according to related functionality. When
I add code later I often only append it at the bottom because that
makes comparing easier.

Cheers

robert

I generally use a top-down approach, where new methods are added
immediately beneath the method that first requires it. This is most
convenient and can be thought of as a type of logical ordering.

If I was trying to be productive, I definitely would not order methods
alphabetically, especially if the class was large. I’m trying write
code,
not rearrange my filing cabinet…

On Fri, Aug 10, 2012 at 12:36 PM, Robert K.

On Aug 10, 2012, at 09:45 , theadventmaster [email protected]
wrote:

If I was trying to be productive, I definitely would not order methods
alphabetically, especially if the class was large. I’m trying write code, not
rearrange my filing cabinet…

How does sorting methods prevent productivity?

On Aug 10, 2012, at 08:03 , masta Blasta [email protected] wrote:

Just getting some layout ideas from other fellow devs.

If you have a class with many methods, how do you try to organize them?
Do you go alphabetical? By related function? Order in which they were
added?

If you’re using a proper editor to navigate, order won’t matter. I
always just sort my methods.

2012/8/10 Ryan D. [email protected]:

How does sorting methods prevent productivity?

You can’t have several related methods visible at the screen at the same
time?

(Yes, yes, you can use a “proper editor” and see several places in the
file in separate windows. That’s like saying that code indentation
doesn’t matter because you can use a “proper editor” and reformat
everything.)

– Matma R.

On Aug 10, 2012, at 15:14 , Bartosz Dziewoński [email protected]
wrote:

2012/8/10 Ryan D. [email protected]:

How does sorting methods prevent productivity?

You can’t have several related methods visible at the screen at the same time?

(Yes, yes, you can use a “proper editor” and see several places in the
file in separate windows. That’s like saying that code indentation
doesn’t matter because you can use a “proper editor” and reformat
everything.)

Your argument is flawed.

Code indentation DOES matter, very much so. It is INDENTING code that
has been made irrelevant as your editor can (and should) do it for you.

If you use a good editor, you can see several places in the same window,
not multiple. You really shouldn’t be having to flip back and forth and
in this case, tiling (split windows) works better.

2012/8/11 Ryan D. [email protected]:

Your argument is flawed.

Code indentation DOES matter, very much so. It is INDENTING code that has been
made irrelevant as your editor can (and should) do it for you.

No it isn’t. What you said only reinforces my point: the way code
looks matters, whether is it indentation or method ordering.

If you use a good editor, you can see several places in the same window, not
multiple. You really shouldn’t be having to flip back and forth and in this case,
tiling (split windows) works better.

Yes, tiling or splitting is what I meant. Of course, there are other
ways to interact with code than with your editor – maybe you’re just
reading it on GitHub, or doing code reviews, or looking at a snippet
in documentation.

– Matma R.

I guess if you have an editor with a sorting or method listing feature
you
like, you’re free to use that.

If you’re manually cutting and pasting methods to achieve your desired
ordering, I can’t help but wonder if you work in an environment where
deadlines matter, or if you’re just programming for kicks.

Generally speaking, I have much better things to worry about during
development than whether or not my methods are sorted to my liking.

theadventmaster wrote in post #1071970:

I generally use a top-down approach, where new methods are added
immediately beneath the method that first requires it. This is most
convenient and can be thought of as a type of logical ordering.

If I was trying to be productive, I definitely would not order methods
alphabetically, especially if the class was large. I’m trying write
code,
not rearrange my filing cabinet…

On Fri, Aug 10, 2012 at 12:36 PM, Robert K.

Alphabetical ordering does makes sense. Since most text editors include
some kind of “fold function” feature, you can fold everything and just
see the method names. If they’re alphabetical it is very easy to browse
through them.

If you’re using a proper editor to navigate, order won’t matter. I
always just sort my methods.

What editor are you using?

On Mon, Aug 13, 2012 at 3:54 PM, masta Blasta [email protected]
wrote:

theadventmaster wrote in post #1071970:

I generally use a top-down approach, where new methods are added
immediately beneath the method that first requires it. This is most
convenient and can be thought of as a type of logical ordering.

Thinking about this again: wouldn’t it be better to place it below the
last method which requires it? That way one would get topological
sorting and you know that methods called from a method are placed
further down the file which is not the case with your rule.

If I was trying to be productive, I definitely would not order methods
alphabetically, especially if the class was large. I’m trying write
code,
not rearrange my filing cabinet…

Alphabetical ordering does makes sense. Since most text editors include
some kind of “fold function” feature, you can fold everything and just
see the method names. If they’re alphabetical it is very easy to browse
through them.

In modern text editors searching is usually quite fast…

Kind regards

robert