I have a lot action methods which do not need to create instance variable for rendering the view, because these method will only redirect to other actions from other controllers. I am wondering: is it a good habit to always create instance variable for the sake of following the conventions of Rails, or there is no such thing. My intuition is that local variable reduces memory costs, but the code does not look pretty.
It's not a convention to create instance variables if they're not being sent to the view.
Instance variables may be used if multiple methods act on them before the view, even if they're not used in the view, but this makes the code much harder to reason about and test in isolation.
If you find yourself using instance variables to hold intermediate calculations you probably need to rethink your flow and/or design.
Mostly we need to use instance variable in following cases,
Why should we use instance variable if our job can be done with a local 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