- 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
Admin
My C++ compiler throws a warning if a switch statement has no default case, even if the default case is impossible to hit (e.g. the input is an enum and every possible case is covered.)
Maybe the coder wants their code to run without warnings?
Admin
OMG this is more common than I expected! I had recently a member of my team who coded all IFs this way:
if ($a==$b ? true :false) { // do something }
At least he rectified them all when I told why that was so wrong.
Admin
The "default" clause is a WTF in it's own, but switch condition WTF is WTF'ier.
He is running an IF clause with the "?" operand. So if $_POST["operation"] == "editUser" , it creates a 1, if not, a 2... it would be much more wiser to:
if ($_POST["operation"] == "editUser") { // editUser } else { // saveUser }
Also, this is PHP so there are no compiler warnings.
Admin
A former colleague was fond of if($boolean == TRUE) ...
Admin
The default case is necessary if the ternary operator returns FileNotFound.
Admin
What if one of the operands in the iif is null?
Admin
It's not wrong, it's redundant.
That's redundant, not wrong.
Admin
Admin
Just to give a possible explanation: the switch statement could well be left over from the "good old days"(tm) when the "operation" query param would carry integers instead of strings (operation == 1: edit user, operation == 2: save user etc).
And for some reason, a developer found it easier to add an on-the-spot conversion of the string values into their (deprecated) integer versions, because it meant changing only one line of code instead of lots (gut's feeling is telling me, "// SNIP" stands for a lot of lines and not some single method call).
And in certain environments, this can actually make sense: calling diff on the source files shows only the relevant change, not the noise generated by replacing the switch with the more natural if ... else.
So yes, as it stands it is a WTF, but not necessarily one that should be changed.
Admin
A colleague of mine used the same "logic", until I asked him: "Nice, but why do you arbitrarily stop at the first level? Why not if((boolean==true)==true)? Or even if(((boolean==true)==true)==true)?
He got enlightened!
CAPTCHA: erat: how long since my last homework on Latin verbs!
Admin
That's one of the biggest wtfs on behalf of programmers, and the one that pretty much causes most of the stress and headaches of maintainers.
Admin
Not really a WTF, it adds clarity, especially in a weak-typed language.
Admin
It still amazes me sometimes how little value some managers see in a maintainable code base...
Admin
But then a type-safe operator should be used. if ($boolean === TRUE) {...}
Admin
In the case of php, sometimes I do that, yes, but that way: if ($boolean === TRUE)
Admin
I usually type if $boolean == TRUE in my code. I use it dependent on whether the code reads like a statement - if the variable name reads a proper statement - then I dont use it. Other wise for better readability I use it.
so if the variable name is userIsAvailable - then I write the code as if userIsAvailable: do x
If the variable name is availabilityOfUser - then I write the code as if availabilityOfUser == True do x
or I might even create enums to make it more readable.
The point is - shouldnt we be writing code to make it more readable than care about such arbritary coding standards - especially since in many cases compiler itself might remove it - or even otherwise what performance impact does it have?
Admin
And to be really on the save side, obviously isTrue's implementation must go all the way down: bool isTrue(bool aBool) { return isTrue(aBool); }
Adding file_not_found to the solution can then be assigned as homework ;-)
Admin
In PHP I do this by convention as it is a necessary defense mechanism against the WTF that is "truthy" and "falsy" comparisons.
Admin
I've seen a lot of this recently
if var1=false or var2=false or ... var30=false then return false else return true endif
Admin
I do the same and it has a purpose. I read "If $boolean is equal to true then...". Without that I read "if $boolean..." ... if boolean WHAT?
Admin
Is it me or we have a new design?
Admin
Okay - "...even if the default case is impossible to hit under normal/ideal conditions ..."
Happy now? :-P
Admin
Richardson's First Law of Computational Unpredictability states: "The fact that a given occurrence is impossible does not imply that it will not occur."
Admin
A snippet from a piece of Java code in "my" system:
Boolean lbBusinessEbox = Boolean.TRUE; ... if (lxUserInfo.getBusinessFlag().equals(Boolean.FALSE)) lbBusinessEbox = Boolean.FALSE;
It took me several passes and a quick consulting by a friend to make I sure I really understood what was going on.
Admin
Maybe the coder should have used an IF statement instead of wrapping a ternary operation inside a switch statement.
Admin
Admin
and I feel fine...
Admin
The Second Law states: "If something is possible, it will happen. Especially if users are involved."
I've had many conversations with account managers that run along these lines: Me: "What is the expected behavior in scenario XYZ where a user chooses options A and B?" (note: there is nothing at all preventing users from choosing both) Them: "But that will never happen!" Me: "..."
Admin
Not a WTF, merely an idiosyncrasy based upon a rigorously enforced house style. I'd have absolutely no problem if I were to see this in code maintained by my staff; it doesn't even require a comment, beyond an indulgent chuckle at what cards they all are.
Admin
So you're saying that you're basically Hard of Understanding?
Admin
Admin
In that case, why don't you name your Boolean variable to something useful, like $userIsAdmin or $TransactionCompleted? Then the code reads:
"if $userIsAdmin"
and it makes sense.
Admin
Adding cruft because of naming variables properly does not make adding cruft any better...
Admin
Admin
Admin
If you are really interested in the readability of the code then rename the damn variable.
Admin
The real WTF is weak-typed languages.
CAPTCHA: inhibeo: the spell that causes you to not get laid
Admin
No joke about president's daughter yet.
BTW- margaret thacher is dead. please mourn her for 1 minute.
Admin
If your $boolean has a name that actually means something - e.g. $nukeReady, or $reticulatingSplines, then the if statement reads naturally:
if ($nukeReady) --> if the nuke is ready, then...
if (!$reticulatingSplines) --> if not reticulating splines, then...
Admin
(comment fix FTW)
If your $boolean has a name that actually means something - e.g. $nukeReady, or $reticulatingSplines, then the if statement reads naturally:
if ($nukeReady) --> if the nuke is ready, then...
if (!$reticulatingSplines) --> if not reticulating splines, then...
Admin
Very useful pattern in C, in conjunction with:
#define TRUE 0 #define FALSE 1
Admin
Admin
Admin
It's still the best PHP I've ever seen, WTFs and all.
Admin
I solve that easy.
I only name a variable "PropertyOfX" if it is not a boolean. This means a comparison is necessary. It flags me to look for the enum values, or such. If it is a boolean I always use the "IsPropertyValue". If it is a nullable I put "nullable" in the name.
IsUserAvailable is a boolean.
IsUserAvailableNullable is a nullable boolean.
UserAvailability is an enum. (AvailableWeekdays, AvailableWeekends, AvailableAfterHours).
UserAvailabilityFlags is an enum with flag attribute set.
It's what hungarian notation should have been.
Admin
Well, knowing that $boolean does not eally needs to be holding a boolean (the infamous 'True, false or File_not_found' joke comes to mind) I guess it would not bea too bad an idea to explicitily name it. But than, as other posters here already remarked, when using PHP rather with the type-safe comparision ("===").
Especially if many people will be editing/adding to that code ...
... or ofcourse use a strong-typed language, and raising a red flag when a non-boolean is used where one is expected
Admin
So they wiped out the real values all with false, then always return false.
TRWTF is languages without a distinction between value and comparison operations.
Admin
Two of my favorite PHP WTFs involving bolean logic:
which is equivilent to: While I appreciate what the coder was trying to do and it would be helpful at times to have a switch structure that supported expressions, it's still a WTF.The second is this lovely expression (I really hate negative 'or' comparisons):
which is equivilent to: For those of you about to call WTF, the expression can't be converted to: Because the two values are results of PHP functions which either return false on failure or "something else" on success.Admin
Then, when would you ever not use "==="? When you need to know if "Dog" is equivalent to 23?