61
u/mike5973 Apr 17 '15
Is that a shitty way to convert to a Boolean? I like it.
32
u/danieltobey Apr 17 '15
Nah, it's a shitty way to assign a boolean. For example, if ret is false:
ret = false && true;which becomes:
ret = false;If ret is true:
ret = true && true;which becomes:
ret = true;It's redundant and a waste of machine cycles that will probably be optimized out by a clever compiler - unless I'm missing something.
130
u/mlk Apr 17 '15 edited Apr 17 '15
a waste of machine cycles
It's ridiculous how many worry about stuff like that and don't bat an eye loading a 10MB library to use a simple function.
I had a collegue worrying about performance of doing "too many ifs" before inserting in a DB table until I showed him how many millions booleans you can compare in a second and how irrelevant that is compared to the insert.
Edit: I'm not saying that code is clean, but I'm always skeptical when people worry about micro optimizations that usually destroy the code base in the long run. Show me that it's slow, and then make it fast. Don't write shit code because the clean way is slow. Because it usually isn't.
43
u/CodeIt Apr 17 '15
Too many ifs is a sign of bad code for other reasons.
15
u/mlk Apr 17 '15
I was expecting this reply, that's why I wrote that his concern was about performances, not about the design. It was actually auto generated code checking for nulls or something like that, used to validate input.
15
u/CodeIt Apr 18 '15
A relevant quote you may have heard before:
"premature optimization is the root of all evil." - Donald Knuth
A larger version:
"Programmers waste enormous amounts of time thinking about, or worrying about, the speed of noncritical parts of their programs, and these attempts at efficiency actually have a strong negative impact when debugging and maintenance are considered. We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. Yet we should not pass up our opportunities in that critical 3%."
6
u/_teslaTrooper Apr 18 '15
Until you end up with something O(n²)
10
u/Nialsh Apr 18 '15
As long as n<1000 and the call corresponds to a single user action, you shouldn't bat an eye at O(n²).
6
u/_teslaTrooper Apr 18 '15
I mentioned it because I ran into this problem once, in my case n was between 9.3 and 5.6 million.
6
u/imsometueventhisUN Apr 18 '15
Between 9.3 million and 5.6 million, or between "9.3" and "5.6 million"? 'cos, either way you have problems...
4
5
u/calsosta Apr 17 '15
Yea wtf. Get it working then worry about performance. In fact as long as you don't intentionally try to ruin performance the only time you need to worry is when people complain.
2
u/Jester_Don Apr 18 '15
Like when people use
++iinstead ofi++in their for loops1
u/birjolaxew Apr 20 '15
Except using ++i isn't any more of a conscious effort than using i++. You almost never have to actually think about which one to use, so most just use whichever one they always do.
14
u/Websly Apr 17 '15
In C it can be used to convert a non-zero value to TRUE (1)
14
1
Apr 17 '15
What about x & 1? That seems a lot more clear to me.
8
u/Websly Apr 17 '15
if x was an even value, the result would be FALSE.
3
Apr 17 '15
Oh yeah. Why not (bool)t then? According to the c standard:
When any scalar value is converted to _Bool, the result is 0 if the value compares equal to 0; otherwise, the result is 1
1
u/Websly Apr 17 '15
Ah I didn't know that. I like that way the best, but I would be a bit worried about others reading my code not knowing it.
1
Apr 20 '15
That's what comments are for
2
u/Websly Apr 20 '15
I'd use a macro for that. Placing comments like that all around the place will make things messy.
1
u/batmansavestheday Apr 18 '15
Is that from one of the newer C standards? A lot of people still write C89.
1
Apr 18 '15
Really? Why would you do that, unless you were in the embedded or windows space? (And if you're on windows, you should be using a C subset of C++ anyways (if you wanted to use msvc), or use clang or mingw)
2
u/ThickAsABrickJT Apr 19 '15
Us embedded developers are everywhere, in places you never notice. Just like embedded systems.
0
3
10
u/mike5973 Apr 17 '15
I've been using mostly weak typed languages, so it can convert to a Boolean. If this is a strong typed language than its just stupid.
Kinda like
x = !!x;7
u/Elnof Apr 17 '15
Except
x = !!x;isn't stupid... for some languages. C for example.#typedef unsigned char BOOL; BOOL a = 1; BOOL b = 90; if(a) { // This evaluates } if(b) { // This evaluates } if(a == b) { // This does not evaluate } b = !!b; if(a == b) { // This evaluates }2
10
u/recursive Apr 17 '15
But what if ret isn't initially a boolean?
-4
u/d03boy Apr 17 '15
then its just shitty code
5
u/recursive Apr 17 '15
Yes. That's the idea of this subreddit.
1
u/d03boy Apr 17 '15
But not this post... otherwise that piece of code would have been provided
1
u/recursive Apr 17 '15
You just got done saying it's shitty code. You're disagreeing with your own self.
0
5
u/errorkode Apr 17 '15
I guess this is probably a dynamic language like JavaScript where "something" is true, but not a boolean. So you can convert it to a boolean like that.
3
u/6890 Apr 17 '15
In my brain I see it as a way of writing this:
if ( ret >= 1 ) { ret = 1; } else { ret = 0; }...which I suppose could have valid use somewhere if we're really grasping for straws here
3
u/Elnof Apr 17 '15
For languages without a true boolean type (C) it's a way of making sure two true values are actually equal. It doesn't take much grasping for that.
Edit: 'true' is syntax highlighted. That suggest that this isn't C, but it doesn't necessarily mean that it isn't since someone could reasonably
#define true 10
Apr 17 '15 edited Apr 18 '15
We do have a boolean type... _Bool
And for anyone who down votes me, yes it is a true boolean type, not a typedef, it is defined to be 0 or 1.
1
2
u/baggyzed Apr 17 '15
waste of machine cycles
Nope. Fortunately, in this case, most compilers are smarter than humans. :)
Unless you mean those cycles are wasted during compilation, in which case you are right.
1
u/OrionBlastar Apr 17 '15
It is sort of like adding NOPs in machine language and assembly to slow down the program with a delay loop. NOP = No Operation.
Some programmers do this on purpose to make their code run slower, and then remove the delays later on and claim to have debugged and optimized the code.
Need I say that most managers judge productivity by the numbers of lines of code written instead of the quality of the code. They don't care how slow it is as long as it works.
1
u/sleepahol Apr 20 '15
You're assuming that
retis originally a boolean.This is a shitty pattern because (warning: javascript):
'abc' && true => true
'' && true => ''
1 && true => true
0 && true => 0
null && true => null
undefined && true => undefinedAnd so on...
If the first value is evaluated as false (e.g.
''), that value is returned. Otherwise, true.0
u/sugar_free_haribo Apr 17 '15
Assuming this is JS, it would only work if ret is actually false, and not merely falsy. The proper way to coerce a boolean is
ret = !!ret;2
Apr 17 '15
No. If ret is 0 or empty string or null or undefined, then maybe you want those values returned.
3
u/sugar_free_haribo Apr 17 '15
That would be a highly unusual pattern (If truthy, return true. If falsy, return the actual value. Wtf.)
1
u/TinBryn Apr 18 '15
Maybe it's to indicate what exactly makes it falsy
2
u/sugar_free_haribo Apr 18 '15
Yeah I mean you can obviously contrive a reason to justify this code, but it doesn't seem like that's what anyone would ever want to do.
1
u/mike5973 Apr 17 '15
I prefer
x = Boolean(x), as its more readable. Though !! isn't exactly confusing...6
u/until0 Apr 17 '15
Though !! isn't exactly confusing...
It's also adds some excitement to your code!!
2
3
Apr 17 '15
Side-note: never use the
newkeyword withBoolean.new Boolean(false)evaluates to an Object, which is always truthy.WTF JAVASCRIPT??
5
u/Tidher Apr 17 '15
... I don't see anything wrong with this. Objects are truthy in JavaScript, if
new Boolean(false)was falsey then that would be inconsistent.-1
u/dead-fish Apr 17 '15
If this is JS, it would be the equivalent of doing:
ret = Boolean(ret);2
3
Apr 17 '15
It converts, but only if it's truthy. If ret is null, it will stay null instead of becoming false.
2
u/mike5973 Apr 17 '15
Oh, you're right. I got my PHP and JS confused. In PHP that would always end up as true or false.
0
13
u/mebob85 Apr 17 '15
This reminds me of when I wrote this little gem when I was really tired:
bool ret;
// other code...
return ret ? true : false;1
6
2
1
u/_ShieldGuy Apr 25 '15
This obviously is the new half life 3 engine.
3
u/WhenisHL3 Apr 25 '15
By mentioning Half Life 3 you have delayed it by 1 Month. Half Life 3 is now estimated for release in July 2153
I am a bot, this action was performed automatically. If you have feedback please message /u/APIUM-
0
-3
u/straatman Apr 17 '15
I'm not really sure some of you get it by reading the comments...
ret and true, now say that fast out loud. ie. return true;
I may be wrong though...
0
Apr 17 '15
[deleted]
5
u/BikerBoon Apr 17 '15
Why?
ret && Output true true true false true false This is essentially ret = ret.
9
u/dead-fish Apr 17 '15
Or if ret is not a boolean to begin with:
var ret = 100; ret = ret && true; console.log(ret); // true6
u/recursive Apr 17 '15
You missed a bunch of possible input values, like dates, strings, arrays, function references and objects.
2
-4
u/BikerBoon Apr 17 '15
True, I never contemplated anyone doing something so awful but then again I've not been hanging around in /r/shittyprogramming for long.
2
2
29
u/[deleted] Apr 17 '15
I'm going to take a guess and say this is JavaScript, in which case this is a "cast any truthy value to boolean"
Not necessary, but usually any means, but it prevents potentially leaking objects to other objects that shouldn't have access to them, and if you're ever inspecting it's better to see "true" than some object, function, string, etc.