DEV Community

Akshay Birajdar
Akshay Birajdar

Posted on

1

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!

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

Top comments (0)

Image of Datadog

Create and maintain end-to-end frontend tests

Learn best practices on creating frontend tests, testing on-premise apps, integrating tests into your CI/CD pipeline, and using Datadog’s testing tunnel.

Download The Guide

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay