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;
orreturn result != 0;
>>62111828
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?
>>62111851
>>62111843
>>62111845
Then it is more efficient that way? How much more efficient is it? Would it make a big difference in a big project?
Wouldn'treturn 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 likeif (result == false) { return false; }.
>>62111919
No shit Sherlock
>>62111919
t. brainlat
>>62111885
It will make no difference whatsoever. If you compile without optimisations then maybe you'll get an imperceptible difference.
>>62111938
Thanks.
>>62111933
Wait... is this bait that I just fell for?????/
>>62111933
>>62111935
In what languages can you implicitly cast an integer to a boolean? I can only think of C.
>>62111981
Please leave this board right now.
>>62111994
Pascal
>>62111845
is he not trying to only return if result is 0?
>>62111996
no u
>>62111828
>Why is this code bad?
It's not. It just shows your inexperience
>>62111921
What's wrong with that? I know you can useif (!result), but what if you don't want to return if it's true?
>>62111994
c, python, go, brainfuck, bash, the list goes on
>>62112618
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
>>62112618
I was just using an example. I don't even code in C.
>>62111845
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
>>62111828
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.
>>62113200
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
>>62111828
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.
>>62111828
>>62113666
Also, any decent optimizing compiler made in the past 20 years shouldn't make a difference between any of the statements ITT.
>>62111994
JavaScript.
>>62114273
Internally it's probably represented as a string.