{"action":"created","issue":{"url":"https://api.github.com/repos/github/rubocop-github/issues/7","repository_url":"https://api.github.com/repos/github/rubocop-github","labels_url":"https://api.github.com/repos/github/rubocop-github/issues/7/labels{/name}","comments_url":"https://api.github.com/repos/github/rubocop-github/issues/7/comments","events_url":"https://api.github.com/repos/github/rubocop-github/issues/7/events","html_url":"https://github.com/github/rubocop-github/issues/7","id":214161937,"node_id":"MDU6SXNzdWUyMTQxNjE5Mzc=","number":7,"title":"Explain why `class << self` is preferred over `def self.foo`","user":{"login":"alexanderadam","id":372620,"node_id":"MDQ6VXNlcjM3MjYyMA==","avatar_url":"https://avatars1.githubusercontent.com/u/372620?v=4","gravatar_id":"","url":"https://api.github.com/users/alexanderadam","html_url":"https://github.com/alexanderadam","followers_url":"https://api.github.com/users/alexanderadam/followers","following_url":"https://api.github.com/users/alexanderadam/following{/other_user}","gists_url":"https://api.github.com/users/alexanderadam/gists{/gist_id}","starred_url":"https://api.github.com/users/alexanderadam/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/alexanderadam/subscriptions","organizations_url":"https://api.github.com/users/alexanderadam/orgs","repos_url":"https://api.github.com/users/alexanderadam/repos","events_url":"https://api.github.com/users/alexanderadam/events{/privacy}","received_events_url":"https://api.github.com/users/alexanderadam/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2017-03-14T18:16:04Z","updated_at":"2018-09-20T18:48:49Z","closed_at":null,"author_association":"NONE","body":"As your styleguide recommends\r\n\r\n```ruby\r\nclass TestClass\r\n  # bad\r\n  class << self\r\n    def first_method\r\n      # body omitted\r\n    end\r\n\r\n    def second_method_etc\r\n      # body omitted\r\n    end\r\n  end\r\n\r\n  # good\r\n  class << self\r\n    attr_accessor :per_page\r\n    alias_method :nwo, :find_by_name_with_owner\r\n  end\r\n\r\n  def self.first_method\r\n    # body omitted\r\n  end\r\n\r\n  def self.second_method_etc\r\n    # body omitted\r\n  end\r\nend\r\n```\r\n\r\nI was wondering *why*. If it's just a matter of taste for you: fine, please write this in the styleguide as well.\r\n\r\nIt isn't obvious here why you are preferring this because especially in cases with many class methods the block and therefore also the additional indention give a good *visual hint* that methods are *encapsulated* and belong together. They share a similar context and blocks are the usual way of creating contexts.\r\n\r\nBesides from that it can also help to avoid to have class methods spread over a file. It is easier (at least for people I know) to have class methods together in one place than having a bunch of instance methods in between.\r\n\r\nThe Eigenclass context also makes sense in your example as `per_page` and `first_method` both will create class methods. But in your example just `attr_accessor` and `alias_method` are using the Eigenclass context whereas `first_method` and `second_method_etc` give an 'defined from the outside' feeling.\r\n\r\nThese points makes it more understandable for me to follow [bbatsovs / rubocops styleguide](https://github.com/bbatsov/ruby-style-guide#classes--modules) which says\r\n```ruby\r\n  # Also possible and convenient when you\r\n  # have to define many class methods.\r\n  class << self\r\n    def first_method\r\n      # body omitted\r\n    end\r\n\r\n    def second_method_etc\r\n      # body omitted\r\n    end\r\n  end\r\n```"},"comment":{"url":"https://api.github.com/repos/github/rubocop-github/issues/comments/423292523","html_url":"https://github.com/github/rubocop-github/issues/7#issuecomment-423292523","issue_url":"https://api.github.com/repos/github/rubocop-github/issues/7","id":423292523,"node_id":"MDEyOklzc3VlQ29tbWVudDQyMzI5MjUyMw==","user":{"login":"maxim","id":7758,"node_id":"MDQ6VXNlcjc3NTg=","avatar_url":"https://avatars0.githubusercontent.com/u/7758?v=4","gravatar_id":"","url":"https://api.github.com/users/maxim","html_url":"https://github.com/maxim","followers_url":"https://api.github.com/users/maxim/followers","following_url":"https://api.github.com/users/maxim/following{/other_user}","gists_url":"https://api.github.com/users/maxim/gists{/gist_id}","starred_url":"https://api.github.com/users/maxim/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/maxim/subscriptions","organizations_url":"https://api.github.com/users/maxim/orgs","repos_url":"https://api.github.com/users/maxim/repos","events_url":"https://api.github.com/users/maxim/events{/privacy}","received_events_url":"https://api.github.com/users/maxim/received_events","type":"User","site_admin":false},"created_at":"2018-09-20T18:48:49Z","updated_at":"2018-09-20T18:48:49Z","author_association":"NONE","body":"I second this, would actually love to have an optional cop for enforcing the `class << self` style as opposed to `self.foo`. Summary of benefits:\r\n\r\n- indent serves as the visual que\r\n- helps keep all class methods grouped together\r\n- searching `def foo` returns both class and instance methods\r\n- adding class-level code requires `class << self` anyway, might as well use it for methods too"}}
{"actor":{"display_login":"maxim"}}