13 ms·
[question]: Is the main benefit of "Rightward assignments" readability and does this provide more benefits?
by hhsnopek 6y ago
[question]: Is the main benefit of "Rightward assignments" readability and does this provide more benefits?
- zamalek 6y agoRuby currently supports "Rightward conditionals": user.delete() if deleteUser It is probably thought that this feature would result in similar readability improvements.
- choward 6y agoI don't find that more readable at all. I prefer: if deleteUser user.delete() end It's easier for me to read. It doesn't try to hide away logic. And if the line gets longer if the conditional gets more complex or I want to add an else I can without reformatting.
- zamalek 6y agoIt's subjective, the point is that some people prefer it.
- kevincox 6y agoIt works well for some cases. You end up with return if a.nil? return if attempts < max_attempts In this case "return if" becomes like a keyword and you can ignore it and focus on the rest of the expression. It also seems very readable for other simple values like `return false if ...`. But I agree, as soon as the code to the left of the `if` gets non-trivial it is harder to read.
- war1025 6y agoPython has the same "rightward conditional" thing. Any time I use it, I wish python had just gone with the normal ternary (? :) operator.
- dragonwriter 6y ago> Python has the same "rightward conditional" thing. No, python writes the ternary operator with words, it doesn't have rightward conditionals. The key difference is that the else clause is required in the Python ternary, where it's optional in a conditional.
- war1025 6y agoDoesn't make it any less irritating.
- tylerscott 6y agoI am also struggling to think of a situation where I would want to reach for this feature. The only examples I can muster are just minor improvements to readability, like you pointed out. I would love to see a solid example where this is a must have feature.
- imglorp 6y agoI think it would make sense for chained operations in a mental data flow sense. makejunk().filter().last(4) => fourthings feels better than fourthings = makejunk().filter().last(4)
- syspec 6y agoNot to me, and other people who use Ruby sparingly
- Someone 6y agoI’m not sure. In shell, I think makejunk | grep foo | tail -4 > fourthings is more popular than > fourthings makejunk | grep foo | tail -4 Having said that, I don’t understand why you would add this, but then, I don’t understand the popularity of ruby or other languages that think adding alternative ways to do things most of the time is a good thing.
- Izkata 6y agoIn shell outside of file manipulation, fourthings=$(makejunk | grep foo | tail -4)
- ultrarunner 6y agoI think I would use this if I were writing a cpu emulator in Ruby (which I wouldn't). It mirrors assembly, but I'm not sure that's a good thing.
- spc476 6y agoIt depends upon the assembly language. Intel defines its opcodes as `dest,src` and Motorola did `src,dest`.