if (result == 0) {return false}

if (result == 0) {return false}
Why is this code bad? Does the compiler even differenciate between this and a good code?

because you could do 'if not result return false' which does less to accomplish the same thing.

return result == 0 ? false : true;

or

return result != 0;

You write code equally for the compiler and for the people (even if people means you in 6 months).
Do you wanna look at your own code with loathing at some time in the future?

Then it is more efficient that way? How much more efficient is it? Would it make a big difference in a big project?

Wouldn't return result; do the same job???????????

That code in particular isn't that bad. Booleans (true/false) are much more specific types than integers. It might mean more to a programmer to see a boolean than an integer.

The code that people get mad at is more like if (result == false) { return false; }.

No shit Sherlock

t. brainlat

It will make no difference whatsoever. If you compile without optimisations then maybe you'll get an imperceptible difference.

Thanks.

Wait... is this bait that I just fell for?????/

In what languages can you implicitly cast an integer to a boolean? I can only think of C.

Please leave this board right now.

Pascal

is he not trying to only return if result is 0?

no u

>Why is this code bad?
It's not. It just shows your inexperience

What's wrong with that? I know you can use if (!result), but what if you don't want to return if it's true?

c, python, go, brainfuck, bash, the list goes on

t. person who's proud he knows you don't need to evaluate the truthiness yourself while ignoring the readability because he's never actually had a software job

I was just using an example. I don't even code in C.

This is an assumption that this is the final lines in the code.

Sometimes you want to return if the input or result is 0 but otherwise continue with some processing.

Considering there is nothing suggesting that you are returning true if result is 1, or !=0, you can't use that code

I was thinking, perhaps a better way would be to return sgn(result). Would this be more or less efficient? I guess calling sgn() could end up being less efficient.

Definitely calling sgn will result in more operations. My question was directed as how the compiler sees the code and how it is translated into asm.

Does it even differenciate between.
Return(result).
And
If result !== 0 return result

Depends.
Does anything else happen afterwards?
If so, this is the only practical solution.

Pretty much all solutions ITT will return at this statement, either true or false, but this may not always be the desired outcome.

Also, any decent optimizing compiler made in the past 20 years shouldn't make a difference between any of the statements ITT.

JavaScript.

Internally it's probably represented as a string.