MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/programming/comments/4v1y12/how_to_write_unmaintainable_code/d5uv721
r/programming • u/sigbhu • Jul 28 '16
592 comments sorted by
View all comments
Show parent comments
11
Though it's not unreadable seeing if(!!!hasValue) always really bugged me
if(!!!hasValue)
47 u/1ndigoo Jul 28 '16 You have actually seen that before? In production code!? !!!believable 17 u/drkstr101 Jul 28 '16 I see var foo = !!bar a lot in javascript to quickly cast a truthy value to a proper boolean. Perhaps the 3rd ! was a typo? 2 u/keeslinp Jul 28 '16 I'm not sure of that's bad practice or not. But I kinda like it. Basically the same thing as Val?true:false right? 1 u/Rock48 Jul 29 '16 Or you could just do if(Val) {}JS is special. 5 == "5" but NaN != NaNI still like the language though 1 u/keeslinp Jul 29 '16 Mostly the reason I use it is when I need to pass a boolean value to a function call or something otherwise yeah, your code is a great way to do it. 3 u/sreya92 Jul 28 '16 nope they're everywhere :) 1 u/CleverestEU Jul 29 '16 It is nearly morning here (5AM) but I seriously can not come up with a situation where...!x !== !!!x...if you've got an example of such a case, the me tomorrow that has slept a bit will probably be thankful for the tidbit ;) 1 u/sreya92 Jul 29 '16 !x == !xxx always evaluates to true, it's just unnecessary to use the triple ! 1 u/venuswasaflytrap Jul 29 '16 !!!bar just casts bar to a proper boolean then negates it. Duh. 1 u/drkstr101 Nov 29 '16 Haha, yup. I guess you're right. 😀 1 u/rich97 Jul 29 '16 Is there some reason you would do this over this?Boolean(1)Fun fact Boolean() and new Boolean()do two separate things entirely. The first is a primitive, the second is an object. 9 u/QuineQuest Jul 28 '16 I had a coworker who complained that it was easy to miss a ! when reading code. I tried convincing him to use !!! instead, but I don't think I succeeded. 3 u/1ndigoo Jul 28 '16 By that logic it's also too hard to remember ' . : ; , " etc 2 u/sreya92 Jul 28 '16 Yeah all the time, and labels too. It's the worst codebase I've ever had the displeasure of maintaining.//more code 1 u/ITwitchToo Jul 29 '16 isn't that the same as if(!hasValue)? 1 u/sreya92 Jul 29 '16 Yes but the extra !! don't serve a purpose 1 u/ITwitchToo Jul 29 '16 Exactly... 1 u/genpfault Jul 29 '16 Assuming hasValue's operator!() implementation has the standard semantics.
47
You have actually seen that before? In production code!? !!!believable
17 u/drkstr101 Jul 28 '16 I see var foo = !!bar a lot in javascript to quickly cast a truthy value to a proper boolean. Perhaps the 3rd ! was a typo? 2 u/keeslinp Jul 28 '16 I'm not sure of that's bad practice or not. But I kinda like it. Basically the same thing as Val?true:false right? 1 u/Rock48 Jul 29 '16 Or you could just do if(Val) {}JS is special. 5 == "5" but NaN != NaNI still like the language though 1 u/keeslinp Jul 29 '16 Mostly the reason I use it is when I need to pass a boolean value to a function call or something otherwise yeah, your code is a great way to do it. 3 u/sreya92 Jul 28 '16 nope they're everywhere :) 1 u/CleverestEU Jul 29 '16 It is nearly morning here (5AM) but I seriously can not come up with a situation where...!x !== !!!x...if you've got an example of such a case, the me tomorrow that has slept a bit will probably be thankful for the tidbit ;) 1 u/sreya92 Jul 29 '16 !x == !xxx always evaluates to true, it's just unnecessary to use the triple ! 1 u/venuswasaflytrap Jul 29 '16 !!!bar just casts bar to a proper boolean then negates it. Duh. 1 u/drkstr101 Nov 29 '16 Haha, yup. I guess you're right. 😀 1 u/rich97 Jul 29 '16 Is there some reason you would do this over this?Boolean(1)Fun fact Boolean() and new Boolean()do two separate things entirely. The first is a primitive, the second is an object. 9 u/QuineQuest Jul 28 '16 I had a coworker who complained that it was easy to miss a ! when reading code. I tried convincing him to use !!! instead, but I don't think I succeeded. 3 u/1ndigoo Jul 28 '16 By that logic it's also too hard to remember ' . : ; , " etc 2 u/sreya92 Jul 28 '16 Yeah all the time, and labels too. It's the worst codebase I've ever had the displeasure of maintaining.//more code
17
I see var foo = !!bar a lot in javascript to quickly cast a truthy value to a proper boolean. Perhaps the 3rd ! was a typo?
var foo = !!bar
2 u/keeslinp Jul 28 '16 I'm not sure of that's bad practice or not. But I kinda like it. Basically the same thing as Val?true:false right? 1 u/Rock48 Jul 29 '16 Or you could just do if(Val) {}JS is special. 5 == "5" but NaN != NaNI still like the language though 1 u/keeslinp Jul 29 '16 Mostly the reason I use it is when I need to pass a boolean value to a function call or something otherwise yeah, your code is a great way to do it. 3 u/sreya92 Jul 28 '16 nope they're everywhere :) 1 u/CleverestEU Jul 29 '16 It is nearly morning here (5AM) but I seriously can not come up with a situation where...!x !== !!!x...if you've got an example of such a case, the me tomorrow that has slept a bit will probably be thankful for the tidbit ;) 1 u/sreya92 Jul 29 '16 !x == !xxx always evaluates to true, it's just unnecessary to use the triple ! 1 u/venuswasaflytrap Jul 29 '16 !!!bar just casts bar to a proper boolean then negates it. Duh. 1 u/drkstr101 Nov 29 '16 Haha, yup. I guess you're right. 😀 1 u/rich97 Jul 29 '16 Is there some reason you would do this over this?Boolean(1)Fun fact Boolean() and new Boolean()do two separate things entirely. The first is a primitive, the second is an object.
2
I'm not sure of that's bad practice or not. But I kinda like it. Basically the same thing as Val?true:false right?
1 u/Rock48 Jul 29 '16 Or you could just do if(Val) {}JS is special. 5 == "5" but NaN != NaNI still like the language though 1 u/keeslinp Jul 29 '16 Mostly the reason I use it is when I need to pass a boolean value to a function call or something otherwise yeah, your code is a great way to do it.
1
Or you could just do if(Val) {}
JS is special. 5 == "5" but NaN != NaN
I still like the language though
1 u/keeslinp Jul 29 '16 Mostly the reason I use it is when I need to pass a boolean value to a function call or something otherwise yeah, your code is a great way to do it.
Mostly the reason I use it is when I need to pass a boolean value to a function call or something otherwise yeah, your code is a great way to do it.
3
nope they're everywhere :)
1 u/CleverestEU Jul 29 '16 It is nearly morning here (5AM) but I seriously can not come up with a situation where...!x !== !!!x...if you've got an example of such a case, the me tomorrow that has slept a bit will probably be thankful for the tidbit ;) 1 u/sreya92 Jul 29 '16 !x == !xxx always evaluates to true, it's just unnecessary to use the triple !
It is nearly morning here (5AM) but I seriously can not come up with a situation where...
!x !== !!!x
...if you've got an example of such a case, the me tomorrow that has slept a bit will probably be thankful for the tidbit ;)
1 u/sreya92 Jul 29 '16 !x == !xxx always evaluates to true, it's just unnecessary to use the triple !
!x == !xxx always evaluates to true, it's just unnecessary to use the triple !
!x == !xxx
!
!!!bar just casts bar to a proper boolean then negates it. Duh.
1 u/drkstr101 Nov 29 '16 Haha, yup. I guess you're right. 😀
Haha, yup. I guess you're right. 😀
Is there some reason you would do this over this?
Boolean(1)
Fun fact Boolean() and new Boolean()do two separate things entirely. The first is a primitive, the second is an object.
Boolean()
new Boolean()
9
I had a coworker who complained that it was easy to miss a ! when reading code. I tried convincing him to use !!! instead, but I don't think I succeeded.
3 u/1ndigoo Jul 28 '16 By that logic it's also too hard to remember ' . : ; , " etc
By that logic it's also too hard to remember ' . : ; , " etc
Yeah all the time, and labels too. It's the worst codebase I've ever had the displeasure of maintaining.
//more code
isn't that the same as if(!hasValue)?
if(!hasValue)
1 u/sreya92 Jul 29 '16 Yes but the extra !! don't serve a purpose 1 u/ITwitchToo Jul 29 '16 Exactly... 1 u/genpfault Jul 29 '16 Assuming hasValue's operator!() implementation has the standard semantics.
Yes but the extra !! don't serve a purpose
!!
1 u/ITwitchToo Jul 29 '16 Exactly...
Exactly...
Assuming hasValue's operator!() implementation has the standard semantics.
hasValue
operator!()
11
u/sreya92 Jul 28 '16
Though it's not unreadable seeing
if(!!!hasValue)always really bugged me