rubocop/ruby-style-guide

Avoiding use of class references built via strings/`constantize`

Skipants opened this issue · 5 comments

The problem:

As a developer, if I am changing how a class is used or named, I would need to grep against the codebase with the name of the class. If this class is ever referenced via constantize then I will not find that reference without already knowing it's already there.

eg.

# Class definitions
class AdminUser; end
class RegularUser; end

# bad usage
user_klass = "#{admin_accessible? ? "Admin" : "Regular")}User".constantize

# good usage
user_klass =
  if admin_accessible?
    AdminUser
  else
    RegularUser
  end

The solution:
I think using constantize is the issue here. I think there are cases where it needs to be used, but in general it causes the problem I outlined.

pirj commented

Do you think this is specific to Rails?
I can transfer it to the Ruby Style Guide issue tracker for you.

I'm not too sure. constantize is a ActiveSupport method and I think my decision to put it here was based on making a rule around the usage of that method somehow. What's your thoughts on it, @pirj ?

pirj commented

I mean constantize is definitely a Rails-related method, but is it about constantize or instrumenting class names?

> self.class.const_get("Obj" + "ect")
=> Object

Makes sense. Transfer away!

PR welcome! :-)