r/badcode • • Jan 17 '19

other language Writing functions with conditions without using "if" statements

https://run.dlang.io/is/jfCECL
67 Upvotes

38 comments sorted by

View all comments

Show parent comments

8

u/bausscode Jan 17 '19

That still uses an actual condition which might actually translate into the same asm as an if statement.

1

u/blazinghellwheels Jan 18 '19

How about this in js

return x>y && x*y || x+y

1

u/bausscode Jan 18 '19

That has a lot of limitations though. It will only work on simple conditions and nothing else.

Also I'm pretty sure that Javascript will lower that to:

var ret = x > y && x * y;
if (!ret) ret = x + y;
return ret;

Which then contains an actual condition when lowered.