3 ms·
This reminds me of this one, which is quite common: return foo == null ? true : false;
by wreegab 13y ago
This reminds me of this one, which is quite common:
return foo == null ? true : false;
- Leszek 13y agoIt's better than return foo ? true : false; or even return foo == true ? true : false;
- deleted 13y ago[deleted]
- wreegab 13y agoSo transposing a boolean into a boolean is better than transposing a truthy value of an unspecified type into a boolean? You are missing the point, which is it's really silly to transpose a boolean into a boolean using a ternary operator. If you want to argue that "foo == null" is the problem due implicit nil value falseness, than the solution is simply to use strict comparison operator "===", transposing "foo == null" into a boolean doesn't solve the falseness issue if you think this is what the original author was trying to address.
- adamors 13y agoI sometimes do that in PHP because I hate the equivalence between "", false, null, '0' and 0. Seeing true and false written out makes it more readable.
- collyw 13y agowould a comment not be more appropriate in such circumstances?
- wreegab 13y agoThe result of "foo == null" (or better "foo === null") is boolean, I don't know what is the "equivalence" problem here.
- sixothree 13y agoMaybe this is a programmer who likes to spell things out for future readers. It's easier to convey meaning with return foo == null ? true : false;
- wreegab 13y agoLet's picture a future reader: "Hmmm, if 'foo == null' is true, than it's true... I see..." Seriously..
- jebblue 13y agoFrom a maintenance perspective, it's a bit more involved than that. Returning the explicit evaluation result says, "I do care what foo is and these are the only valid possible return values after expression evaluation, today just return null but in the future we might return something different, either way the return value at this point is critical and needs to be explicitly considered and processed".
- sixothree 13y agoYeah, it was a wild guess. I have a habit of making things more verbose for future maintainers.
- EpicEng 13y agoTo a non programmer or an absolute beginner? Sure, but then I would ask; why in the hell are you writing your code in a style catered to people who don't know what they are doing? That line simply shows that the writer doesn't understand Boolean expressions.
- sneak 13y agoWhen you find your language forces you to do stupid things, the correct response is not "get in the habit of doing stupid things". It's "find a better language".
- diaz 13y agoI did something of this kind recently and it was to enforce more of an api in my object, like internally I call some low api method that either gives me the object I'm looking for or false / null when doesn't find it, but in my interface I don't want to leak that object, just true or false. So function isAvailable(){ objInDb = findByName('Joan Carlos'); // the object or false return objInDb ? true : false; } But all this could be because I'm a noob in a language I don't really know... Php that magical land where nothing is what it looks like and is always ready to stab you in the back, can't say I'm a fan of it... Or dynamic languages, or anything magical... Ok, I'm going to places I don't want to remember, sorry...
- EpicEng 13y agoI this case, returning objInDb would be semantically different. So, if you always want to return a Boolean, you did the right thing.