How does everyone order their methods in a class?
def a; end
def b; end
def business_logic; end
def utility_method; end
# Invocation sequence
def called_first; end
def called_second; end
@shanecav mostly importance (or by I try to group related methods together), but often it's way too "random"…
@judofyr Nice. I'd like to order by importance or relatedness but I find those criteria are hard to determine in practice, resulting in random-ish ordering, so I stick with alphabetical.
I ask bc I'm working on a rubocop extension for ordering methods (https://github.com/shanecav84/rubocop-ordered_methods) and want to see where the community is at. The cop currently only supports alphabetical ordering but I'd like to add config to check for ordering by invocation sequence.
@shanecav I tend to organize primary entry points towards the top with their supporting methods grouped around them.
@shanecav important "public API" methods at the top, hopefully there are so few of them that the order doesn't matter too much, then supporting private methods grouped by caller (similar to invocation sequence in your example).
@shanecav I try to order them so that it becomes easy for colleagues (or my future self) to follow the flow of execution. Often, that means order by invocation, but there are many exceptions.
@shanecav usually invocation sequence to have better code locality
@shanecav alphabetical. It seems like the easiest way to find a method quickly later. I make exceptions where it makes sense, though, like in a controller where each action is one step of a long procedure.
(Procedure as in a multi-step process with input from a human being at each step, not as in procedural code)
I also put initialize at the top.
A Mastodon instance for Rubyists & friends