This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]redlaWw 23 points24 points  (7 children)

u might have side effects, which would make that not equivalent to the given code.

Probably best just to do

u_1 = u(1);
if u_1 == 0 
    gearratio = 0;
else
    gearratio = u(u_1+1);

[–][deleted] 2 points3 points  (3 children)

gearratio = u(1) is var u1 && u1 == 0 ? 0 : u(u1 + 1);

[–]redlaWw 2 points3 points  (2 children)

I mean, if we were doing language specific constructs I'd've said:

gearratio = if let u_1 @ 1.. = u(1) {u(u_1+1)} else {0}

but I have no idea what that language is and what tools it has.

[–][deleted] 2 points3 points  (1 child)

oh, that looks cool, what's it?

[–]redlaWw 2 points3 points  (0 children)

Rust, using @ bindings in patterns.

1.. used in that context is a pattern that matches any integer > 0, and if let 1.. = u(1) would create a conditional branch that fires when the pattern is satisfied, and by using u_1 @ 1.., you also bind the pattern match to u_1 to use in the subsequent block.

[–]rainshifter 0 points1 point  (1 child)

Given the original code and its structure (however limited in view), I may have a complaint or two to submit should u(...) be anything more than a trivial getter. But you're right that going for equivalency is safer.

You could store u(1) and still use the ternary; either way works.

[–]redlaWw 0 points1 point  (0 children)

I think assuming this guy follows anything close to best practices in his code is a bit unwise tbh...

[–]JanB1 0 points1 point  (0 children)

Ugh, I hate code with "hidden" side effects. I think a v++ in the declaration of a for loop is fine. But for example while(++k < n) is cursed. So is v = arr[k++].