- 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
function fristorno($in) { if($in == 'frist') return 'frist'; else return 'NO'; }
Admin
That PHP snippet is actually no that bad. It coerces the function input to one of two possible values - "YES" or "NO". In and of itself, there's nothing wrong with it. Now, it could be a hint that there's something really wrong somewhere else... but it could also be a part of a decent code that has to interface with some nasty data from a poorly designed system.
Admin
I should have coded this stupid comment.
Admin
I stupidly coded this should have comment.
Admin
I don't know why, that ternary has me giggling like a mad woman
Admin
I came here to say similar. There's certainly WTFs lurking behind this (I hate the stringly-typed booleans) but this particular function seems fine in that context.
Admin
The thing I dislike about it isn't that it coerces everything non-"YES" to "NO", or at least not as such.
No, the problem is that it coerces "yes", "Yes", "YeS", "YEs", "yEs", "yES", and "yeS" to "NO", which is a bit POLA-breaking.
Admin
That's all classical typeless language nonsense. Especially PHP is has a crazy amount of method that will return a different type when an error occurs (like return a string on success or null otherwise FALSE). Now someone could argue that this is bad practice, well, then there is no point of having a typeless language in the first place tho :-)
Admin
That's what she said.
Unfortunately.
Admin
We don't know in which language the ternary is. In some languages, == is a loose comparison.
If it is the case, then this ternary could be comparing things of wildly different nature.
Maybe the accounts variable is an enormous object, and a loose comparison with 1 is unpredictably true or false, depending on the phases of the moon, the mood of the CEO, the weather, and the network latency. Who knows?
Admin
There can be only one truth.
Admin
Always be yourself, unless you can be a dragon. Then always be a dragon.
Admin
bool result = true;
result = webServiceClient.GetBoolean(result);
XmlSerializer.Serialize("c:\file_not_found.txt", result);
result = XmlDeserializer.Deserialize<bool>("c:\file_found.txt");
return result == true ? true : !!result;
Admin
The ternary may not be a no-op, e.g. in JavaScript where "1"==1 and 1==true evaluate to true… Though that suggests another WTF earlier in the code. And, as with the PHP function, this behavior should be documented (assuming it is intentional).
Admin
As others have noted, in some languages like Javascript, the ternary might make sense. But thinking about it in a strongly typed language made me actually LOL, Remy's comment was right on point!
I wonder if it is common practice for companies working with PHP to bait and switch devs like that. Also happened to me: hired as Java dev, work was only PHP. Ran away ASAP.
Admin
The PHP bit looks like input sanitizing. I suspect the string has previously been forced to uppercase.
Admin
Naaah, it just teaches people to discipline themselves and only us ALL CAPS ALL THE TIME
Admin
Reminds me of a
format
-like program that asksType "yes" in uppercase letters"
...Admin
They're just trying to make sure you're really paying attention to what you're about to do before you commit to it. ;)
Admin
“Are you sure you want to delete your character Bobcat? Type Bobcat to confirm.”
Admin
The first one makes me think that accounts == 1 is a special case for certain reasons, and thus was singled out when giving requirements, without realising that it wasn't necessary in this case. The coder has then coded the requirements as they were given, without giving it further thought.
Admin
In the first case there can be implicit type conversions at play. In the second it does standardize to two values, even if some of the transforms may be unexpected. Neither of these are code that is safe to change without understanding the full context.