Article EG8A Representative Line: Truely Representative

Representative Line: Truely Representative

by
Remy Porter
from The Daily WTF on (#EG8A)

There's bad code, and then there's code so bad that you only need to see one line of code to understand how bad it actually is. Simon supplied this tiny horror which manages to combine all that's wrong with PHP with the worst of loose typing and a thick layer of not really understanding what you're doing.

$dtRecord->setProcessing((boolean)'true');
256px-Korean_Traffic_sign_%28Pass_Left_o

PHP is a terrible language, but it's not so terrible that it doesn't have a boolean type. It's perfectly happy to mangle juggle your types, and for that reason, it actually discourages the use of any sort of type casting.

Yes, by PHP standards, casting types is an anti-pattern, but that's not why this is as much of a WTF as it is.

PHP lets you use anything as a boolean, and follows the same general conventions as other type-mangling languages- the integer 0, the floating point value 0.0, an empty string (or the string "0") are all false.

So, now we're stacking up the WTFs- the original programmer could have just passed TRUE, instead of "true", but really, it didn't matter what they passed, so this line probably would have made just as much sense:

$dtRecord->setProcessing((boolean)'WTF');

I fortunately don't work with PHP, so I had to do a little fact checking to verify that this line was as stupid as I suspected it was, and that meant reading up on PHP's handling of booleans and their odd results. At the risk of picking on PHP, while I wanted to understand how it did booleans, I discovered that it has two different boolean operators- "OR" and "||", which isn't a WTF, and that they have totally different orders of operation.

As a result, you run into weird cases like this:

$x=TRUE;$y=FALSE;$z= $y OR $x;

$z is false at the end of that expression, because the assignment is actually evaluated before the "OR". Essentially, the final line is actually:

($z=$y) OR $x;

The "||" operator works like a normal person would expect it to.



inedo50.png[Advertisement] Use NuGet or npm? Check out ProGet, the easy-to-use package repository that lets you host and manage your own personal or enterprise-wide NuGet feeds and npm repositories. It's got an impressively-featured free edition, too! TheDailyWtf?d=yIl2AUoC8zA-bS3wQbSDsQ
External Content
Source RSS or Atom Feed
Feed Location http://syndication.thedailywtf.com/TheDailyWtf
Feed Title The Daily WTF
Feed Link http://thedailywtf.com/
Reply 0 comments