[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

if (result == 0) {return false} Why is this code bad?

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 29
Thread images: 1

File: 1501689098976.jpg (156KB, 1024x576px) Image search: [Google]
1501689098976.jpg
156KB, 1024x576px
 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;
>>
>>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'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; }
.
>>
>>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 use
if (!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.
Thread posts: 29
Thread images: 1


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.