I am WTF Seven of Nine of "Ternary" Adjunct...
Submitted by phrax on Fri, 2004-12-31 11:46.
Fugly Code
Thanks to Alex @ TheDailyWTF for forwarding this WTF to me. The WTF is originally from James who found it as a patch for OSCommerce. OSCommerce is one of the most popular and oldest PHP projects. The project started out under a different name but it became popular enough that the project was renamed and remarketed as OSCommerce.
James said:
This patch (attached) for the popular OScommerce shopping cart software adds the functionality to resize images to thumbnail size on demand and cache it for later use. It works - but oh my!
<?php
!isset ($image)
? DIE ('Es wurde kein Bild angegeben!')
: !file_exists($image)
? DIE ('Die angegebene Datei konnte nicht auf dem Server gefunden werden!')
: false;
?>
Oh my indeed! This is not what the ternary operator is ment for. The ternary operator is ment to be a shortcut to using if/else to assign a variable one of two values. It is not ment to be a general short form of if/else. Also using die()
to handle exceptions is the lazy programmer's way out, especially in a popular application like OSCommerce.
This is probably a better way to do it. Look ma, less code too!
<?php
if (!isset($image))
die('Es wurde kein Bild angegeben!');
elseif (!file_exists($image))
die('Die angegebene Datei konnte nicht auf dem Server gefunden werden!');
?>