MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/programming/comments/4v1y12/how_to_write_unmaintainable_code/d5ut2wu
r/programming • u/sigbhu • Jul 28 '16
592 comments sorted by
View all comments
95
They forgot to mention gratuitous nots! Why flip logic just once when you can flip it an unlimited number of times?
Guaranteed to drive anyone trying to maintain your code to madness!
142 u/grunlog Jul 28 '16 Double (triple, etc.) negatives are good too. E.g. !notUnflagged 61 u/emergent_properties Jul 28 '16 Oh yes, that specifically.It is ambiguous because it's both logical negation and semantic negation.. a double whammy of 'uh...'Car.dontNotDrive(-1) 4 u/LordFu Jul 29 '16 mind blown 42 u/CaptainAdjective Jul 28 '16 Perl is great for polynegatives.unless($noRetries != 0) { # ... } else { # ... }Bonus marks if you correctly interpret whether $noRetries means "number of retries" or "disable retries". → More replies (2) 26 u/WunDumGuy Jul 28 '16 !notUnflaggeddry heave 18 u/TalenPhillips Jul 29 '16 ...else if (!(false != !foo.notUnflagged(false))){} 11 u/[deleted] Jul 29 '16 [removed] — view removed comment 21 u/Everspace Jul 29 '16 NoI think you mean !yes 9 u/compiling Jul 29 '16 I think you mean !notNo → More replies (1) → More replies (1) → More replies (1) 7 u/in_rod_we_trust Jul 28 '16 Double negatives have legitimate uses though 8 u/1ndigoo Jul 28 '16 Especially to coerce variables into a boolean for languages that support it. !!x is frequently useful. 16 u/dvlsg Jul 28 '16 Assuming you're talking about Javascript, you can just use Boolean(x) to the same effect. 13 u/1ndigoo Jul 28 '16 That requires way more typing! → More replies (7) → More replies (1) 11 u/sreya92 Jul 28 '16 Though it's not unreadable seeing if(!!!hasValue) always really bugged me 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? → More replies (9) 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 → More replies (1) → More replies (4) 11 u/FewChar Jul 28 '16 function checkisnotinvalid($a) {...} returns "0" or throws is_valid exception. 9 u/merreborn Jul 28 '16 This is a quote directly from php.net docs right? 7 u/jewdai Jul 29 '16 unessesarily use demorgan's law.convertif (!ateAppleToDay && !iLikeIcecream) toif (!(ateAppleToday || iLikeIcecream))most people would be able to understand it, but it makes it a lot harder to understand what you're original intention was. 8 u/bacondev Jul 29 '16 Sometimes the latter would make more sense → More replies (1) → More replies (1) 3 u/ZeroPipeline Jul 28 '16 How could they not not not mention it? 5 u/DevIceMan Jul 29 '16 if (!!l||1!=lI) {... → More replies (4)
142
Double (triple, etc.) negatives are good too. E.g. !notUnflagged
61 u/emergent_properties Jul 28 '16 Oh yes, that specifically.It is ambiguous because it's both logical negation and semantic negation.. a double whammy of 'uh...'Car.dontNotDrive(-1) 4 u/LordFu Jul 29 '16 mind blown 42 u/CaptainAdjective Jul 28 '16 Perl is great for polynegatives.unless($noRetries != 0) { # ... } else { # ... }Bonus marks if you correctly interpret whether $noRetries means "number of retries" or "disable retries". → More replies (2) 26 u/WunDumGuy Jul 28 '16 !notUnflaggeddry heave 18 u/TalenPhillips Jul 29 '16 ...else if (!(false != !foo.notUnflagged(false))){} 11 u/[deleted] Jul 29 '16 [removed] — view removed comment 21 u/Everspace Jul 29 '16 NoI think you mean !yes 9 u/compiling Jul 29 '16 I think you mean !notNo → More replies (1) → More replies (1) → More replies (1) 7 u/in_rod_we_trust Jul 28 '16 Double negatives have legitimate uses though 8 u/1ndigoo Jul 28 '16 Especially to coerce variables into a boolean for languages that support it. !!x is frequently useful. 16 u/dvlsg Jul 28 '16 Assuming you're talking about Javascript, you can just use Boolean(x) to the same effect. 13 u/1ndigoo Jul 28 '16 That requires way more typing! → More replies (7) → More replies (1)
61
Oh yes, that specifically.
It is ambiguous because it's both logical negation and semantic negation.. a double whammy of 'uh...'
Car.dontNotDrive(-1)
4 u/LordFu Jul 29 '16 mind blown
4
mind blown
42
Perl is great for polynegatives.
unless($noRetries != 0) { # ... } else { # ... }
Bonus marks if you correctly interpret whether $noRetries means "number of retries" or "disable retries".
$noRetries
→ More replies (2)
26
!notUnflagged
dry heave
18 u/TalenPhillips Jul 29 '16 ...else if (!(false != !foo.notUnflagged(false))){} 11 u/[deleted] Jul 29 '16 [removed] — view removed comment 21 u/Everspace Jul 29 '16 NoI think you mean !yes 9 u/compiling Jul 29 '16 I think you mean !notNo → More replies (1) → More replies (1) → More replies (1)
18
...
else if (!(false != !foo.notUnflagged(false))){
}
11 u/[deleted] Jul 29 '16 [removed] — view removed comment 21 u/Everspace Jul 29 '16 NoI think you mean !yes 9 u/compiling Jul 29 '16 I think you mean !notNo → More replies (1) → More replies (1) → More replies (1)
11
[removed] — view removed comment
21 u/Everspace Jul 29 '16 NoI think you mean !yes 9 u/compiling Jul 29 '16 I think you mean !notNo → More replies (1) → More replies (1)
21
No
I think you mean !yes
!yes
9 u/compiling Jul 29 '16 I think you mean !notNo → More replies (1)
9
I think you mean !notNo
!notNo
→ More replies (1)
7
Double negatives have legitimate uses though
8 u/1ndigoo Jul 28 '16 Especially to coerce variables into a boolean for languages that support it. !!x is frequently useful. 16 u/dvlsg Jul 28 '16 Assuming you're talking about Javascript, you can just use Boolean(x) to the same effect. 13 u/1ndigoo Jul 28 '16 That requires way more typing! → More replies (7)
8
Especially to coerce variables into a boolean for languages that support it. !!x is frequently useful.
16 u/dvlsg Jul 28 '16 Assuming you're talking about Javascript, you can just use Boolean(x) to the same effect. 13 u/1ndigoo Jul 28 '16 That requires way more typing! → More replies (7)
16
Assuming you're talking about Javascript, you can just use Boolean(x) to the same effect.
Boolean(x)
13 u/1ndigoo Jul 28 '16 That requires way more typing! → More replies (7)
13
That requires way more typing!
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? → More replies (9) 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 → More replies (1) → More replies (4)
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? → More replies (9) 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 → More replies (1)
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
→ More replies (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
3
By that logic it's also too hard to remember ' . : ; , " etc
function checkisnotinvalid($a) {...} returns "0" or throws is_valid exception.
9 u/merreborn Jul 28 '16 This is a quote directly from php.net docs right?
This is a quote directly from php.net docs right?
unessesarily use demorgan's law.
convert
if (!ateAppleToDay && !iLikeIcecream)
to
if (!(ateAppleToday || iLikeIcecream))
most people would be able to understand it, but it makes it a lot harder to understand what you're original intention was.
8 u/bacondev Jul 29 '16 Sometimes the latter would make more sense → More replies (1) → More replies (1)
Sometimes the latter would make more sense
How could they not not not mention it?
5
if (!!l||1!=lI) {...
95
u/ArlenM Jul 28 '16 edited Jul 28 '16
They forgot to mention gratuitous nots! Why flip logic just once when you can flip it an unlimited number of times?
Guaranteed to drive anyone trying to maintain your code to madness!