Should I use if defined?
return @current_user_session if defined?(@current_user_session)
@current_user_session = UserSession.find
Or ||=
@current_user_session ||= UserSession.find
I noticed the if defined?
method being used more and more recently. Is there any advantage to one over the other? Personally, I prefer ||=
for readability. I also think Rails might have a memoize
macro which provides this behavior transparently. Is this the case?
Rails does have memoization, check out the screencast below for a great introduction:
http://railscasts.com/episodes/137-memoization
class Product < ActiveRecord::Base
extend ActiveSupport::Memoizable
belongs_to :category
def filesize(num = 1)
# some expensive operation
sleep 2
12345789 * num
end
memoize :filesize
end
Be careful: x ||= y assigns x = y if x returns false. That may mean that x is undefined, nil, or false.
There are many times variables will be defined and false, though perhaps not in the context of the @current_user_session instance variable.
If you desire conciseness, try the conditional construct:
defined?(@current_user_session) ?
@current_user_session : @current_user_session = UserSession.find
or just:
defined?(@current_user_session) || @current_user_session = UserSession.find
if you just need to initialize the variable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With