Giter Site home page Giter Site logo

Comments (6)

maxim avatar maxim commented on August 16, 2024 4

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:

  • indent serves as the visual que
  • helps keep all class methods grouped together
  • searching def foo returns both class and instance methods
  • adding class-level code requires class << self anyway, might as well use it for methods too

Edit: didn't realize coming from google that this was github's styles, not rubocop itself. Still might as well throw in my 2c.

from rubocop-github.

rmosolgo avatar rmosolgo commented on August 16, 2024

I don't agree with the preference expressed in the guide, but I think I understand the issue. Consider this Ruby code:

class A 
  class << self 
    attr_accessor :some_attribute 
  end 
end 

class B < A 
end 

As far as I know, that's the only way to support usage like this:

A.some_attribute = :value_1 
B.some_attribute # => nil 
B.some_attribute = :value_2 
A.some_attribute # => :value_1 

I mean, using a class variable would cause it to be shared by A and B, which we don't want.

Does that sound right to you? Is there another way to implement that snippet above?

I think the same thing is true for alias, it only works for class methods inside class << self.

So, I don't agree with the preference, but I think that explains the difference, does that make sense?

from rubocop-github.

alexanderadam avatar alexanderadam commented on August 16, 2024

@rmosolgo first of all: thank you for responding! 🎉
That's awesome!

However, I just wrote about

  def self.foo
    
  def self.bar
    
  def self.baz
    

vs

  class << self
    def foo
      
    def bar
      
    def baz
      

The case that you alias class methods or use class variables shouldn't occur in every class, right? Especially as class variables are kind of dangerous.

I rather would accept if some class would exceptionally not comply to a style guide than having a less readable variant established as a standard in a style guide.

Even if someone decided that this is the way to write class methods on GitHub, and added it to the style guide because of the points you just mentioned it should probably contain an explanation à la "Hey folks, we write class methods this way because we use class method aliasing and class variables so often".

Besides from that: these aliases don't mention the class methods at all. Are you sure that we are talking about the same issue here?

I really just opened this issue to write about the method definition of the class methods.

from rubocop-github.

alexanderadam avatar alexanderadam commented on August 16, 2024

I'm sorry. I just recognized that the title of the issue was actually the inverse to what I wrote to in the description 🙈
I'm sorry in case that this was the cause of the misunderstanding here.

By the way, the particular text snipped I referenced here, is this sentence

Avoid class << self except when necessary, e.g. single accessors and aliased attributes.

from rubocop-github.

mmahalwy avatar mmahalwy commented on August 16, 2024

I agree. I think the cop should avoid having both class << self and def self.blah in the same file

from rubocop-github.

composerinteralia avatar composerinteralia commented on August 16, 2024

RuboCop added a cop for this in 2020 that can be configured either way: rubocop/rubocop#8381. So I'd say this one is mostly personal preference, and our style guide now matches the default configuration in RuboCop.

from rubocop-github.

Related Issues (18)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.