In my app, if I have to call @user.id more than once, ill do
[email protected]
Then in the code, ill use “id” everywhere that I need it. Is this
unneccessary? Should I just be using @user.id?
In other words, does the “.id” method call more resource consuming than
passing it into a variable and does it matter?
-Aryk
i wouldn’t lose any sleep over something like this regarding resource
consumption. it really doesn’t matter.
even so,
id = user.id
=> 1
user.id.object_id
=> 3
id.object_id
=> 3
They are the same anyway, so why add the extra line of code if it
really doesn’t do anything?
Personally, @user.id is what i would use. Why? Because if I were
reading your code, everywhere you use @user.id, I would know exactly
what you meant. Using just ‘id’ can be a little ambiguous and can
lead to possible confusion as to intent. Even if you use ‘user_id’,
see the previous paragraph.
I would be more concerned about resource consumption if you were to
have a method which made a complicated calculation, in that case I
would use your example.
result_of_complicated_calculation = @obj.complicated_calculation
use result_of_complicated_calculation in the rest of the code
Chris