Put class methods before instance methods #43

gabebw opened this Issue Jan 10, 2013 · 7 comments


None yet
6 participants

gabebw commented Jan 10, 2013

This is the opposite of what I see in the ruby sample right now.

I like this:

class Thingy
  def self.hello
    puts 'Hello'

  def hi
    puts 'Hi'

croaky commented Jan 10, 2013

Where do you put def initialize?


adarsh commented Jan 10, 2013

Aside: Can we add something about the preferring self.methodname over
class << self blocks?

On Thu, Jan 10, 2013 at 10:13 AM, Dan Croak notifications@github.comwrote:

Where do you put def initialize?

Reply to this email directly or view it on GitHubhttps://github.com/thoughtbot/guides/issues/43#issuecomment-12110487.


joshuaclayton commented Jan 10, 2013

Not sure how I feel about this. Typically we use very few class methods and they're pushed near the bottom of the file because they're rarely used. I'm also interested in what @croaky asked, as well as validations, associations, etc.

Adarsh, I totally agree re class << self and it's a convention we've followed for a very long time (the biggest reason is that indentation isn't an immediate way to illustrate that it's a class method).


gabebw commented Jan 10, 2013

I put initialize as the top method, after constants but before class methods and thus also before instance methods.

@adarshpandit : We already have that in the style guide: https://github.com/thoughtbot/guides/blob/master/style/README.md#ruby

Use def self.method, not def Class.method or class << self.


mike-burns commented Jan 11, 2013

I don't see what we get out of this. Put method definitions where they make sense for the class you're writing.


jferris commented Jan 11, 2013

Agreed on not using class << self; it makes it too easy to lose your place and the extra indentation is annoying.

I'm also not seeing how it would be helpful to group class methods together. Seems like it would just disrupt a more meaningful order.


jferris commented Jan 3, 2014

I'm closing this, since there wasn't general agreement, and it's been a long time.

jferris closed this Jan 3, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment