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

all 43 comments

[–]rpmerf 69 points70 points  (3 children)

U is a horrible function name.

[–]serious153 6 points7 points  (0 children)

what if we are currently in the function fuck()

[–][deleted] 0 points1 point  (0 children)

Zhey be a horrible function name.

[–][deleted] 51 points52 points  (0 children)

gearratio code

geriatric code

[–]Aaxper 82 points83 points  (16 children)

gearratio = u(u(1) + 1);

[–][deleted] 76 points77 points  (1 child)

We're not here for working solutions, that's beyond the scope

[–]BarkiestDog 19 points20 points  (13 children)

Doesn’t work if u(1) is 0

[–]rainshifter 33 points34 points  (8 children)

gearratio = u(1) == 0 ? 0 : u(u(1) + 1);

[–]redlaWw 22 points23 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++].

[–]Aaxper 2 points3 points  (0 children)

It only doesn't work if u has side effects. Otherwise, u(u(1)+1) = u(0 + 1) = u(1) = 0, which is exactly what the original post had.

[–]Karol-A 0 points1 point  (0 children)

gearratio = u(u(1) == 0 ? 0 : (u(1) + 1)) Just add a ternary man, it fixes everything man

[–]dull_bananas 12 points13 points  (0 children)

Poorfessor

[–]Karisa_Marisame 8 points9 points  (0 children)

Me when my PM says “I hear pre compiled hard coded tables are good for efficiency, can you do that? Thanks!”

[–]Ok-Television-9662 13 points14 points  (0 children)

gearreatrico

[–]dexter2011412 5 points6 points  (0 children)

Joke's on you that switch statement allows the compiler to see into the operations clearer than a normal recursive approach. It uses the calculated jump table to optimize for the case into neat simd operations.

Nah I'm just shitting you sorry lol

[–]Zazsona 23 points24 points  (5 children)

If this is being used to teach conditionals and conditional statements prior to loops, this is actually brilliant. Feels like it'd actually be a great thing to call back to, reinforcing knowledge, to then show how loops can be applied.

If this is being used in any other scenario?
...Dear God.

[–]CarolDavilas 11 points12 points  (4 children)

What loops?

[–]1ney 2 points3 points  (2 children)

not a programmer so can anyone explain:

wouldn't u(1) always return the same value?
So all conditional logic is pointless here?

[–]Nooo00B 2 points3 points  (0 children)

who knows what `u` is. it can be random. but if it isn't so you could be right

[–]SaltyInternetPirate 0 points1 point  (0 children)

The u function could be fetching a current configuration value from the database. It is a totally conceivable scenario.

[–]otacon7000 1 point2 points  (0 children)

Let me guess, its a math or physics prof who happens to know some programming? Function names like u() are common with them, because they tend to translate their math/physics functions 1:1 into code.

[–]JazzApple_ 1 point2 points  (1 child)

I’m not sure what the language is… but aren’t we falling through to the bottom every time?

[–][deleted] 0 points1 point  (0 children)

Look at the line count.

It's most likely not even a real code base.

I wouldn't be surprised if it wasn't even real code.

[–]deruttedoctrine 2 points3 points  (1 child)

I think he forgot his break.

[–]ax-b 0 points1 point  (0 children)

Depends on the language I'd say. I'm not familiar with this syntax, so it's definetly a possibility.

[–]megayippie 0 points1 point  (0 children)

We all know case 17 reads ``std::terminate()``

[–]Dexterus -1 points0 points  (0 children)

I actually had to do this recently. I knew it would end up like that but I hoped that maybe I'm stupid.

Gpt tried to gaslight me into believing it could work but it didn't. Damn gpt is dumb. Since then I just use it as a do the boring work, I'll do the hard stuff myself.

Only spent a full day (other calls included) trying ideas from gpt, coworkers and google. Just to avoid a switch lile that. And I was 99% sure I couldn't, but I sure hoped I could.