From: Kedar Mhaswade Date: 2011-01-13T10:57:55+09:00 Subject: Re: Parallel Assignments and Elegance/Complexity Ratio. >> I do like/understand such application of parallel assignment. > > Strangely, that's the situation (number of lvalues == number of > rvalues) when I definitely dislike its use (unless it might be > necessary to ensure all the rvalues are calculated before any > assignment takes place - might side effects make this necessary?), for > two (and a quarter) reasons. Interesting (yeah, it's a matter of taste). But when number of variables is <=3, it looks very readable and easily understandable. > First, whenever I've benchmarked parallel assignment against > individual assignment, I've found the parallel assignment somewhat > slower. The swap is more elegant, but even that seems slower. That's a good point, but it's judgmental based on how frequently the code gets called. > # a, b, c, d, e, f, g, h = q, r, s, t, u, v, w, x > If it is instantly obvious, try more l and r values and/or try longer > variable names and/or values and/or split the assignment expression > over multiple lines! Well, one can always abuse a feature. I think number of variables should be <=3, e.g. a, b, c = q, r ,s @d, @e, @f = t(a), u(b), v(c) # ... > > As an actual example, this from Date: > def initialize(ajd=0, of=0, sg=ITALY) > @ajd, @of, @sg = ajd, of, sg > # ... > end > Admittedly it's easy here to see what's being assigned to what, but > even so is that really better than the alternative just below? Is > there a reason for using the parallel assignment in the actual > example, other than aesthetics? > def initialize(ajd=0, of=0, sg=ITALY) > @ajd = ajd; @of = of; @sg = sg > # ... > end > > The quarter is the positive side of the negative point of the second, > that explicit individual assignment is easier visually/cognitively > than parallel assignment. Yeah. And the (others') wisdom suggests that if and when you find a situation to apply it, it (usually) renders an elegant solution. -Kedar -- Posted via http://www.ruby-forum.com/.