4 ms·
> Use of self to refer to instance methods (the text here refers to "model attributes", but that's just a particular use of instance methods) is appropriate and
by rezonant 2y ago
> Use of self to refer to instance methods (the text here refers to "model attributes", but that's just a particular use of instance methods) is appropriate and often desirable, because failure to do so as the example demonstrates earlier can result in bugs if the code is ever changed to introduce a local variable of the same names as the instance method.
This was my rationale originally, but after working on a big Rails codebase for so long, and hearing a lot of push for the opposite conclusion from Rails devs more experienced than I am, I have relaxed my opinion here. It is certainly true that always using self will avoid the type of bugs you speak of, but if you also aim for short declarative methods over long imperative ones (as you mentioned), this issue becomes pretty rare, and in a language with so many other ways to shoot yourself in the foot, really the only good way to insulate yourself against bugs is with automated tests.
What you gain from not using self is more compact expressions that are less likely to bleed past the line length limits or need to be split up into temporary variables, which does help to look at the big picture of an operation with less syntactic load on the reader of the code.
All that said, if you have junior Rails devs on your team, it might make sense to tailor your style closer to overuse of self to ensure they can more easily grok it.