2 ms·
1. Verbosity The recommended fix here is…needlessly verbose. Better would be: actor = case response when 1 "Groucho" when 2 "Chico"
by dragonwriter 2y ago
1. Verbosity
The recommended fix here is…needlessly verbose. Better would be:
actor =
case response
when 1
"Groucho"
when 2
"Chico"
when 3
"Harpo"
else "Zeppo"
end
or even:
actors = {1=>"Groucho", 2=>"Chico", 3=>"Harpo"}
actors.default = "Zeppo"
actor = actors[response]
2. Safe navigation operator
Yes, use this for sure, this is good.
3. "Unnecessary" use of self
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. Now, sure, generally I'd prefer to avoid doing that, and in a very short method (especially a one-line defining what is essentially a getter for a derived trait), sure, don't bother, its cleaner and easier to comprehend without additional noise and no one is going to change it to add a local variable without seeing the other use and fixing it. But, in general, self is clear and safer.
4. Collecting results in temp vars
Yes, avoid this by knowing and using the methods on Enumerable that are designed for this, as they clearly and concisely express what you want to do.
But also, remember the first rule about Verbosity, so avoid stuttering and superfluous parens.
h.transform_values {_1 &. *2}
- 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.
- rezonant 2y agoRe (1, assignment of conditional), I tend to avoid this construction, though I don't stress about it too much in code review. It's just fairly easy to lose track of the fact that an assignment is occuring except in the most simplest of cases. I also extend this to implicit return -- like with explicit self I used to be militant that explicit return was always clearer and safer. However, in an "idiomatic" short method I think it's fine, you must understand and be cognizant of Ruby's return semantics no matter what. However, once it becomes clear that there is a chance someone will lose sight of the fact that we're doing a bigass return statement in this huge block of conditionals, I employ explicit return. I'm also a huge fan of using early return to handle edge cases and exceptional cases. This is something a lot of developers disagree with me on, but I've never been sufficiently convinced to avoid it.