DEV Community

Akshay Birajdar
Akshay Birajdar

Posted on

Should Gem assumes that other gem is loaded?

We have a gem where we have a method #display_name, this method returns localized name if I18n gem is defined else falls back to default_display_name.

Following is the current implementation:

  def display_name
    if Module.const_defined?(:I18n)
      localized_display_name
    else
      default_display_name
    end
  end
Enter fullscreen mode Exit fullscreen mode

We want to refactor it as on every method call we are calling Module#const_defined?.

Following is the proposed implementation.

if Module.const_defined?(:I18n)
  def display_name
    localized_display_name
  end
else
  def display_name
    default_display_name
  end
end
Enter fullscreen mode Exit fullscreen mode

On surface level this looks ok but we have a concern that now we are assuming the require order of gem. So, my question is, should the gem assume the other gem is loaded (in this case I18n) or it should lazily checks like current implementation.

And any other suggestions for refactoring/optimizations are welcome. Thanks in advance!

Top comments (0)