`if Defined?`(Foo)` in initializers instead of environment check

Hello thoughtbot folks,

Reading through upcase-exercises code, I saw something in initializers, and I’m wondering if there is a reason behind it or not.
In https://github.com/thoughtbot/upcase-exercises/blob/master/config/initializers/pusher.rb, you wrap initialization code a if defined?(PusherFake) instead of a consistent if Rails.env.test? || Rails.env.development?.

Is there a specific reason, or those are completely equivalent?

Same thing here https://github.com/thoughtbot/upcase-exercises/blob/master/app/views/application/_javascript.html.erb or in Konacha initializer https://github.com/thoughtbot/upcase-exercises/blob/master/config/initializers/konacha.rb


I’ve started doing this in initializers as well if it conveys my intent better. Sometimes my intent is “if I’m in development or test, configure this thing slightly differently.” But in some cases, as seems to the case with PusherFake here, what I mean is “If this gem is available in this environment at all, configure it like this.”

1 Like

I concluded the same.

I think it’s a better pattern since you don’t rely on environments directly, but on the presence of the class, whatever the environment is.
If we write if Rails.env.test? || Rails.env.development?, we only depend on the fact that the gem is loaded on those environments (based on what’s written in the Gemfile), which is kind of obscure.