- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
The biggest :wtf:: a Boolean called
count
Admin
That doesn't count!
Admin
Could be worse. Could have used six Booleans.
Admin
I know of some languages where bool is actually an int. And then everything other than zero is True. So it could have started as a int, and been simpliefied to a bool and then... it is still a wtf though
Admin
Seems like automatically-named variables by a Reflector-like tool to me, as also shown by directly using IEnumerator rather than a proper foreach control structure... now TRWTF is decompiled code appearing in someone's codebase.
Admin
This is my FRIST post, and the programming language of the code in the article is not one of the many I have some fluency for. That said, and treating as a pseudo language, I can only see two things that could be given a (very low) WTF Score:
Of course, there are a host of other things I could mention but they would be very language, environment or requirement dependant - stuff I know nothing about or have any context for.
Admin
Admin
Which if the decompiled code hypothesis is correct, is probably exactly what happened. Same location->same variable name.
Admin
Really the only one I saw (I don't C#, so the IEnumerable stuff doesn't stand out to me). I usually name those
ok
.Admin
It occurs to me that:
count
is misnamed, as others have said.if()
statements.finally
block. And WTF, guys, a language where you have to check that an object is not NULL before deleteing it. (In C++ this is not necessary!)if()
.So overall, the problem isn't whether there is one bool for the whole routine or one per test, but the fact that they used variables at all. Or is that an optimiser/decompiler artefact as well?
Admin
Admin
Well, of course, allocating six booleans is so expensive that it is better to misname one and over use it, again and again. NO.
Misnaming variable is the paving the path to Hell.
Admin
Wouldn't the better solution be to use no booleans and inline the check? :)
Admin
The pattern here seems pretty obvious: a code convention forbiding function calls inside a control structure. So… Just make the function calls before, store the result in a temporary variable and check this variable in the control structure. Well, as we didn't want the variable in the first place, just throw a random name at it and we're good to go.
Admin
That seems like a personal style preference to me. I'd accept either as being fine.
Yeah...I would do the equivalent in Java, but pretty weak sauce all around on this one. Really the naming is the most problematic thing here for me.
Admin
Then again, it is purely style; any decent compiler will optimise away the variable anyway.
Admin
While I agree that is likely to be what happens, something in my I-remember-when-it-was-bipolar-ttl-around-here rebels a little at compilers who insist on knowing better than I do. Just because they do, doesn't make it good.
(As personal preference, I avoid actual booleans wherever it doesn't make things untidy. If I have to store a boolean result and use it later, something tells me that there is something wrong with the program flow.)
Admin
Admin
Then why use a variable at all?
They are using it with this pattern:
They could have just done:
Edit: Now I see many made similar comments before me. That'll teach me to read to the end next time. (Maybe.)
Admin
I'd be a little more charitable to the developer if the variable were named something like "myBool" or "allPurposeBool" and re-used for the sake of economy. However, naming it "count" is like saying, "Semantics are important to me! Sort of!", while re-using it says "SCREW semantics! I do what I want!"
Inconsistency bugs me.
Admin
This brings back memories of something I once encountered ... http://what.thedailywtf.com/t/not-shortcutting-logic-never-heard-of-those/4140
Admin
I guess it would be a matter of style or preference. Sure, the code could be rewritten with inline checks, and no Booleans, and it would certainly reduce the line count and possible speed up the execution. However, it would also increase the cost of refactoring for scalability, enhancement or maintainability.
My "preference" is to identify a default State, then determine if conditions warrant a new State, then present that State to the business logic. This means that I can "mess" about with the determination, or the logic independently; I can make either or both Functions, Classes, or Methods in their own right; either or both can be reused (if required) - without having to recalculate. And Finally, when I return to it at some future time, I would have a better chance of reacquiring the original thought processes behind it without having to recreate the chemical constitution of my brain (without or without enhancement - assuming I could figure out what I was on at the time).
One of my axioms is: The less times I type identical code, the less chances there are of me introducing an "invisible" typo.
Related to the above, in terms of doing unnecessary work and readability / maintainability: Why give the compiler extra work to do? Whether it be a "one time" compile like C etc or a "compile on demand" like Pearl etc.
That I have selected individual quotes does not mean my response is directed at them personally, just that they are good examples of an alternative view, that I wish to counter.
Admin
Admin
Even in C, bool is a separate type that only has values 0 and 1.
Admin
It can be useful when it isn't clear what the condition means. For example
This makes it clear without any comment what the if condition is actually about. It's also useful for debugging (which you will do at some point) because you can check a breakpoint and see what the value of allReceiptsPrinted is.
Admin
Actually, using multiple variables makes it easier, not harder, for an optimising compiler. If you reuse the variable, the compiler needs to figure out that there are different scopes. So for example, if you set and use the boolean, then call a function, the compiler needs to find out that the variable is set again before any future use, so it can avoid saving the variable from a register to the stack and restore it. With six variables, each one is not even used after the first use, which makes that analysis a lot easier.
Admin
Good point.
I've done something similar occasionally, but usually with expressions more complicated than this. Also, different variables. Experienced coders shouldn't find themselves writing code where they have bools that can be reused. And I generally remove the redundant variables - i.e. I only use them for debugging while writing the code. Otherwise if it's a bug I'm fixing, single-stepping through the code is easy and the variables are still unnecessary. Of course you can just set watches or use the inspector while stepping through the code.
Actually come to think of it, adding a watch for the count variable and then stepping might cause a non-brain-dead developer to say to himself, "Gosh, the variable is called count and it isn't a count at all."
Admin
Only marginally. Reachability analysis is a very well understood thing these days.
Admin
You see? It still could have been worse.
Admin
Sorry, that should have been:
Admin
Wow, no one is complaining about TRWTF IMHO... virtually (maybe literally, who has the time to check) every fricking usage of that boolean is accompanied by a 'not' condition. Soooooo many double negatives!!!
Admin
Good luck. I'm behind seven Booleans.
Admin
Yes and no.
I'll typically try to include those in the if statement. However, if I'm trying to debug something, sometimes it's a lot more convenient to inspect something in a temporary "result" variable (e.g., be it a boolean result or complex structure). Once I've got it working properly, I frequently don't bother rolling it back up again because it's working and the impact on performance is so minor. If it's in a tight loop and I need to optimise; maybe.
Also, pulling the code out of the
if
statement can also make the code easier to understand at a glance. Consider:vs
Since we're trying to make it easily human readable, we are optimizing for the human rather than for the processor (except where performance is a bigger concern).
Admin
Admin
It can also make it wrong. :smiley:
You've changed the semantics there, because you're now evaluating
this.SendMail(…)
irrespectively of whether the preceding two boolean conditions have been satisfied. Fortunately, compilers don't make this sort of mistake.Admin
Or some such.
Admin
bool cunt = True;
Admin
That's not unlike the issue that is not really the main concern of not a small number of people in this thread.
Filed Under: Double negatives aren't not hard., FUCKING HELL WHY DOES IT INSERT TWO NOTS WHEN I TYPED ONE?
Admin
Because Discourse saw the words "double negative", but only one not (using regex, which is why it missed "aren't")
Admin
Õ_o
Admin
In my head I am singing: One little, two little, three little booleans Four little, five little, six little booleans
Parenthood.
Admin
Do we have another @nαgesh?
Admin
"not unlike"... that's just evil.
Admin
:see_no_evil: :hear_no_evil: :speak_no_evil: :)
Admin
Admin
Dunno, the account was 1 minute old when that was posted and 10 mins (with just that post) when I noticed it.
Admin
Dammit!. So am I now. And it's recursive., Without a count to check it!
Admin
Reminds me of the joke:
The English Professor is busy teaching his class: "In some languages, a double negative makes a negative. In some, a double negative makes a positive. But there is no language in the world where a double positive makes a negative." Some smart ass in the back cries out: "Yeah, right"
Admin
Seven little, eight little, nine little booleans Ten little boolean flags.
Admin
Depends on your version of C. I guess you haven't worked with
typedef unsigned char bool
.