|
Can't see the forest for the trees
Last post 12-01-2012 4:25 PM by Zecc. 38 replies.
-
11-21-2012 3:06 PM
|
|
-
DrPepper


- Joined on 10-26-2012
- Posts 57
|
Can't see the forest for the trees
Anonymized code from a method I'm working on right now. Why do people continue to write code obtusely like this? if (isUserInDb(user)){ if (isUserAccountInOrder(user){ if (isUserAccountNegativeBalance(user) { return status.Negative; return status.Positive; } return status.Invalid; } return status.MissingAccount; instead of the way I'd write it: if (isUserInDb(user) == false) return status.MissingAccount; if (isUserAccountInOrder(user) == false) return status.Invalid; if (isUserAccountNegativeBalance(user)) return status.Negative; return status.Positive; So much easier to read.
|
|
-
-
TGV


- Joined on 10-09-2005
- Posts 552
|
Re: Can't see the forest for the trees
Apart from the truly horrible boolean == false, your version is a lot better.
|
|
-
-
DrPepper


- Joined on 10-26-2012
- Posts 57
|
Re: Can't see the forest for the trees
Without going too far down the path; I prefer (if x() == false) rather than if (!x()) -- to me, it's too hard to miss that ! when I'm reading the code. Of course, I'd really like to rename the method/return value: instead of if (x.isInvalid() == false) I'd write if (x.isValid()) but sometimes I don't get to change the API.
|
|
-
-
ObiWayneKenobi


- Joined on 03-23-2007
- Posts 314
|
Re: Can't see the forest for the trees
The reason why is because someone, somewhere, must have said to never have an if/else statement without the braces, and whoever wrote this code took it to heart that you MUST always have braces even if it hurts readability.
The Daily WTF Forums. You will never find a more wretched hive of scum and villainy.
|
|
-
-
TGV


- Joined on 10-09-2005
- Posts 552
|
Re: Can't see the forest for the trees
DrPepper:Without going too far down the path; I prefer (if x() == false) rather than if (!x()) -- to me, it's too hard to miss that ! when I'm reading the code.
It never occurred to me that people might actually overlook that. I think my brain is so hard-wired from many years of C programming, that every non alphabetical character following a parenthesis is suspect. On the other hand, recently I used "true" and "false" in an awk script, which in awk are just undefined variables, so both false.
|
|
-
-
emurphy


- Joined on 01-14-2005
- Granada Hills, CA
- Posts 568
|
Re: Can't see the forest for the trees
ObiWayneKenobi:The reason why is because someone, somewhere, must have said to never have an if/else statement without the braces, and whoever wrote this code took it to heart that you MUST always have braces even if it hurts readability.
And you MUST always nest them even if that hurts readability, too. $DEITY forbid we get code like if (isUserInDb(user) == false) { return status.MissingAccount; } if (isUserAccountInOrder(user) == false) { return status.Invalid; } if (isUserAccountNegativeBalance(user)) { return status.Negative; } return status.Positive;
|
|
-
-
spamcourt


- Joined on 01-28-2008
- Posts 310
|
Re: Can't see the forest for the trees
DrPepper:Anonymized code from a method I'm working on right now. Why do people continue to write code obtusely like this?
if (isUserInDb(user)){
if (isUserAccountInOrder(user){
if (isUserAccountNegativeBalance(user){
return status.Negative;
return status.Positive;
}
return status.Invalid;
}
return status.MissingAccount; Woah, you're right! That code uses multiple returns! Now that's hard to follow! Better add a return_status variable for clarity.
|
|
-
-
superjer


- Joined on 10-05-2007
- Seattle
- Posts 293
|
Re: Can't see the forest for the trees
This problem clearly calls for more metaprogramming.
#define check(op,f,s) if (op f(user)) return status.s;
check( !, isUserInDb , MissingAccount )
check( !, isUserAccountInOrder , Invalid )
check( , isUserAccountNegativeBalance, Negative )
return status.Positive;
#undef check
|
|
-
-
Seahen


- Joined on 04-02-2009
- Posts 43
|
Re: Can't see the forest for the trees
I interned with a major search engine at its Mountain View headquarters, and we had this rule for all Java code. They didn't even allow one-line curly pairs (except for array literals). I would have had to work around it with a cascade through ternary operators.
|
|
-
-
flabdablet


- Joined on 04-09-2011
- Posts 445
|
Re: Can't see the forest for the trees
DrPepper:Anonymized code from a method I'm working on right now. Why do people continue to write code obtusely like this? if (isUserInDb(user)){ if (isUserAccountInOrder(user){ if (isUserAccountNegativeBalance(user) { return status.Negative; return status.Positive; } return status.Invalid; } return status.MissingAccount;
Yeah, that sucks. It should clearly be written as
return
isUserInDb(user)?
isUserAccountInOrder(user)?
isUserAccountNegativeBalance(user)?
status.Negative:
status.Positive:
status.Invalid:
status.MissingAccount;
No bug due to unbalanced braces. No early returns. What's not to like?
|
|
-
-
realmerlyn


- Joined on 01-16-2007
- Cyberspace
- Posts 78
|
Re: Can't see the forest for the trees
What I think when I see "foo == false"?
Guy doesn't get booleans. Guy needs to stay away from my upstream code.
|
|
-
-
da Doctah


- Joined on 02-20-2010
- Posts 1,070
|
Re: Can't see the forest for the trees
emurphy:And you MUST always nest them even if that hurts readability, too. $DEITY forbid we get code like if (isUserInDb(user) == false) { return status.MissingAccount; } if (isUserAccountInOrder(user) == false) { return status.Invalid; } if (isUserAccountNegativeBalance(user)) { return status.Negative; } return status.Positive; Your corrected version would be fine if every if save the first were changed to elseif.
|
|
-
-
Bulb


- Joined on 07-29-2008
- Prague, Czech Republic
- Posts 188
|
Re: Can't see the forest for the trees
da Doctah:Your corrected version would be fine if every if save the first were changed to elseif.
Since each then branch contains a return without further condition, else wouldn't make a difference.
|
|
-
-
dhromed


- Joined on 04-13-2005
- Dutchland
- Posts 10,080
|
Re: Can't see the forest for the trees
realmerlyn:What I think when I see "foo == false"?
Guy doesn't get booleans. Guy needs to stay away from my upstream code. Good job ignoring his very reasonable explanation. A very astute and original insight!
 boomzilla: I think the obvious answer is for everyone to just stop programming.
|
|
-
-
ASheridan


- Joined on 11-23-2010
- Posts 433
|
Re: Can't see the forest for the trees
DrPepper: if (isUserAccountNegativeBalance(user) { return status.Negative; return status.Positive; }
This does not do what the author thinks it does. The return status.Positive will never be reached because it's already returned Negative. The second return should be outside the braces belonging to this if.
|
|
-
-
dhromed


- Joined on 04-13-2005
- Dutchland
- Posts 10,080
|
Re: Can't see the forest for the trees
ASheridan:The return status.Positive will never be reached
There's a brace mismatch there.
 boomzilla: I think the obvious answer is for everyone to just stop programming.
|
|
-
-
aihtdikh


- Joined on 02-07-2006
- Posts 154
|
Re: Can't see the forest for the trees
dhromed: ASheridan:The return status.Positive will never be reached
There's a brace mismatch there.
And the second and third lines have missing close-parentheses. I trust that is just careless anonymization, though, since it presumably compiles. Or maybe DrPepper did it on purpose to make us all mentally trip over the invalid open brace before the if condition is closed?
Elicit this site's eponymous ejaculation.
|
|
-
-
UpNDown


- Joined on 12-14-2006
- Posts 29
|
Re: Can't see the forest for the trees
DrPepper:Without going too far down the path; I
prefer (if x() == false) rather than if (!x()) -- to me, it's too hard
to miss that ! when I'm reading the code. Of course, I'd really
like to rename the method/return value: instead of if (x.isInvalid()
== false) I'd write if (x.isValid()) but sometimes I don't get to
change the API. This is interesting. Yes, it's easy to miss a lonely exclamation mark. Maybe what we should be doing is making it more obvious: if (!!!x()) or if (!!!!!x()) Then, as long as you have an odd number of exclamation marks, you probably won't miss it
|
|
-
-
Franco


- Joined on 11-22-2012
- Posts 1
|
Re: Can't see the forest for the trees
dhromed: realmerlyn:What I think when I see "foo == false"?
It'd better be
foo != true
|
|
-
-
DrPepper


- Joined on 10-26-2012
- Posts 57
|
Re: Can't see the forest for the trees
aihtdikh:And the second and third lines have missing close-parentheses.
Yep; comes from typing into the message box, rather than cut/paste real code. Picky, Picky.
|
|
-
-
Zemm


- Joined on 11-26-2007
- Gold Coast, Australia
- Posts 1,168
|
Re: Can't see the forest for the trees
realmerlyn:What I think when I see "foo == false"?
Guy doesn't get booleans I use $foo === false a bit. Yes I work with PHP. No I'm not insane.
Filed Under: Can you think of something that talks, other than a person?
|
|
-
-
Ben L.


- Joined on 12-22-2010
- HELP I'M TRAPPED IN A COMMUNITY SERVER FACTORY
- Posts 1,374
|
Re: Can't see the forest for the trees
Zemm: realmerlyn:What I think when I see "foo == false"?
Guy doesn't get booleans I use $foo === false a bit. Yes I work with PHP. No I'm not insane.
strpos: Hey, let's make a function that can return 0 or false for completely different results but make it in a language where 0 == false
|
|
-
-
Zecc


- Joined on 06-12-2007
- and hasn't left since.
- Posts 1,671
|
Re: Can't see the forest for the trees
Ben L.: Zemm: realmerlyn:What I think when I see "foo == false"?
Guy doesn't get booleans I use $foo === false a bit. Yes I work with PHP. No I'm not insane.
strpos: Hey, let's make a function that can return 0 or false for completely different results but make it in a language where 0 == false
I share(d) your pain. On a completely unrelated note, I like Zemm's new avatar. A bird? Yeah!
If mixed metaphors were illegal, I'd be having an indigestion. typeof NaN == 'number' var ò_ó, ಠ⁔ಠ, ᄒᆺᄒ, ᅙᅳᅙ, ᖛᨓᖜ, ꖴᅩꖴ, ఠᨋఠ; // Naming your variables is serious business
|
|
-
-
Zemm


- Joined on 11-26-2007
- Gold Coast, Australia
- Posts 1,168
|
Re: Can't see the forest for the trees
Zecc: Ben L.: Zemm: realmerlyn:What I think when I see "foo == false"?Guy doesn't get booleans I use $foo === false a bit. Yes I work with PHP. No I'm not insane.
strpos: Hey, let's make a function that can return 0 or false for completely different results but make it in a language where 0 == false
I share(d) your pain.
Well I'm starting a new job tomorrow. Apparently there's some Perl involved, so I'm not yet sure if that's better or worse than PHP. I guess I'll find out in less than 12 hours.
Zecc:On a completely unrelated note, I like Zemm's new avatar. A bird? Yeah!
Did the sig make it too obvious? :P Was tossing up between that one and " What does that mean?"
Filed Under: Can you think of something that talks, other than a person?
|
|
-
-
Zecc


- Joined on 06-12-2007
- and hasn't left since.
- Posts 1,671
|
Re: Can't see the forest for the trees
Zemm:
Zecc:On a completely unrelated note, I like Zemm's new avatar. A bird? Yeah!
Did the sig make it too obvious? :P Was tossing up between that one and " What does that mean?"
To be honest, I only noticed the signature afterwards. I just recognised the bird for some reason, then noticed the kid which confirmed my suspicion.
If mixed metaphors were illegal, I'd be having an indigestion. typeof NaN == 'number' var ò_ó, ಠ⁔ಠ, ᄒᆺᄒ, ᅙᅳᅙ, ᖛᨓᖜ, ꖴᅩꖴ, ఠᨋఠ; // Naming your variables is serious business
|
|
-
-
Zemm


- Joined on 11-26-2007
- Gold Coast, Australia
- Posts 1,168
|
Re: Can't see the forest for the trees
Zecc:I just recognised the bird for some reason, then noticed the kid which confirmed my suspicion.
Do you have the entire album? It did sell over a million copies. My non-single favourites are Etoh and Live at Dominoes. "Flight 22 is off to Honolulu"
Filed Under: Can you think of something that talks, other than a person?
|
|
-
-
Zecc


- Joined on 06-12-2007
- and hasn't left since.
- Posts 1,671
|
Re: Can't see the forest for the trees
No, I don't have their album. I just know them from Frontier Psychiatrist's videoclip, which I've seen a couple of times on TV, and from YouTube where I heard a couple of songs more. Namely, I remember Electricity and Run DNA. I also remember having saved the FLV for Frontier Psychiatrist somewhere.
If mixed metaphors were illegal, I'd be having an indigestion. typeof NaN == 'number' var ò_ó, ಠ⁔ಠ, ᄒᆺᄒ, ᅙᅳᅙ, ᖛᨓᖜ, ꖴᅩꖴ, ఠᨋఠ; // Naming your variables is serious business
|
|
-
-
too_many_usernames


- Joined on 12-09-2005
- The Mitten
- Posts 467
|
Re: Can't see the forest for the trees
I would personally probably do something evil like <type> status_codes[ = {status.MissingAccount , status.Positive, status.MissingAccount, ... };
return status_codes[ (isUserInDb(user)) | (isUserAccountInOrder(user)<<1) | (isUserAccountNegativeBalance<<2) ]; No branching at all! (And only slightly dangerous by assuming the isUserX() functions return 0 or 1).
|
|
-
-
Ben L.


- Joined on 12-22-2010
- HELP I'M TRAPPED IN A COMMUNITY SERVER FACTORY
- Posts 1,374
|
Re: Can't see the forest for the trees
too_many_usernames:(isUserAccountNegativeBalance<<2) ];
I'm deeply hoping that this isn't a typo and you really would OR a function pointer in with the other two bits.
|
|
-
-
pkmnfrk


- Joined on 05-10-2010
- Posts 366
|
Re: Can't see the forest for the trees
Ben L.: too_many_usernames:(isUserAccountNegativeBalance<<2) ];
I'm deeply hoping that this isn't a typo and you really would OR a function pointer in with the other two bits.
It's an optimization trick.
|
|
-
-
DrPepper


- Joined on 10-26-2012
- Posts 57
|
Re: Can't see the forest for the trees
too_many_usernames:I would personally probably do something evil like <type> status_codes[ = {status.MissingAccount , status.Positive, status.MissingAccount, ... };
return status_codes[ (isUserInDb(user)) | (isUserAccountInOrder(user)<<1) | (isUserAccountNegativeBalance<<2) ]; No branching at all! (And only slightly dangerous by assuming the isUserX() functions return 0 or 1).
That's really horrible code; requiring an enum for each possible combination of bit values. And, you expect the caller to be able to interpret the results (i.e., which enums mean "isUserInDB"? ) In C#, you could at the least return an anonymous object: return new { isUserInDb= isUserInDb(user), isUserInAccountInOrder = isUserAccountInOrder(user), isUserAccountNegativeBalance = isUserAccountNegativeBalance(user) }; Then the caller can look at the object's members and see what might have happened
|
|
-
-
DescentJS


- Joined on 02-23-2009
- Posts 393
|
Re: Can't see the forest for the trees
DrPepper: too_many_usernames:I would personally probably do something evil like <type> status_codes[ = {status.MissingAccount , status.Positive, status.MissingAccount, ... };
return status_codes[ (isUserInDb(user)) | (isUserAccountInOrder(user)<<1) | (isUserAccountNegativeBalance<<2) ]; No branching at all! (And only slightly dangerous by assuming the isUserX() functions return 0 or 1).
That's really horrible code; requiring an enum for each possible combination of bit values. And, you expect the caller to be able to interpret the results (i.e., which enums mean "isUserInDB"? ) In C#, you could at the least return an anonymous object: return new { isUserInDb= isUserInDb(user), isUserInAccountInOrder = isUserAccountInOrder(user), isUserAccountNegativeBalance = isUserAccountNegativeBalance(user) }; Then the caller can look at the object's members and see what might have happened Or you could just use the enum as a flags object and declare it properly.
|
|
-
-
flabdablet


- Joined on 04-09-2011
- Posts 445
|
Re: Can't see the forest for the trees
DrPepper: too_many_usernames:I would personally probably do something evil like <type> status_codes[ = {status.MissingAccount , status.Positive, status.MissingAccount, ... };
return status_codes[ (isUserInDb(user)) | (isUserAccountInOrder(user)<<1) | (isUserAccountNegativeBalance<<2) ]; No branching at all! (And only slightly dangerous by assuming the isUserX() functions return 0 or 1).
That's really horrible code; requiring an enum for each possible combination of bit values.
Bollocks. Doesn't need a separate enum for each combination of bit values, merely the correct enum values in the status_codes[] initializer:
status_code_t status_codes[] = {
status.MissingAccount, /* --- */
status.Invalid, /* t-- */
status.MissingAccount, /* -t- */
status.Positive, /* tt- */
status.MissingAccount, /* --t */
status.Invalid, /* t-t */
status.MissingAccount, /* -tt */
status.Negative /* ttt */
};
Inefficient though, as it can't do short-circuit evaluation of the Boolean expression; this may also make it incorrect, depending on what UserAccountNegativeBalance() does when isUserAccountInOrder() returns 0, and what isUserAccountInOrder() does when isUserInDb() returns 0.
The nested ternary conditional operator solution does not suffer from this problem.
|
|
-
-
joe.edwards


- Joined on 08-14-2006
- Dallas, TX
- Posts 1,083
|
Re: Can't see the forest for the trees
flabdablet:The nested ternary conditional operator solution does not suffer from this problem.
But I want it to suffer.
I spend most of my life pressing buttons to make the pattern of lights change however I want.
|
|
-
-
thosrtanner


- Joined on 04-08-2008
- Posts 29
|
Re: Can't see the forest for the trees
C++ allows if (not ) which is really hard to miss. I've started using it and it drives one of our programmers wild "because it's different - people won't know what it does" ....
|
|
-
-
joe.edwards


- Joined on 08-14-2006
- Dallas, TX
- Posts 1,083
|
Re: Can't see the forest for the trees
#define if(x) if(!x)
I spend most of my life pressing buttons to make the pattern of lights change however I want.
|
|
-
-
DrPepper


- Joined on 10-26-2012
- Posts 57
|
Re: Can't see the forest for the trees
joe.edwards:#define if(x) if(!x)
The WTFs that can come from improper or nefarious use of the C preprocesser deserver their own sidebar. Best not go down into that hole.
|
|
-
-
PJH


- Joined on 02-14-2007
- Newcastle, UK
- Posts 3,129
|
Re: Can't see the forest for the trees
thosrtanner: C++ allows if (not ) which is really hard to miss.
It really isn't hard to miss.
3 logicians go into a bar.; the barman says ‘Would you all like a drink?’. The first says 'I’m not sure', the second says 'I’m not sure', and the third says 'Yes'.
|
|
-
-
Zecc


- Joined on 06-12-2007
- and hasn't left since.
- Posts 1,671
|
Re: Can't see the forest for the trees
PJH: thosrtanner: C++ allows if (not ) which is really hard to miss.
It really isn't hard to miss.
I missed the fact it was a keyword,but I wouldn't miss it if it wasn't.
If mixed metaphors were illegal, I'd be having an indigestion. typeof NaN == 'number' var ò_ó, ಠ⁔ಠ, ᄒᆺᄒ, ᅙᅳᅙ, ᖛᨓᖜ, ꖴᅩꖴ, ఠᨋఠ; // Naming your variables is serious business
|
|
Page 1 of 1 (39 items)
|
|
|