Yep, PHP. Though some people consider this to be a horror on itself, that's not what I am getting at here :)
Jeroen De Dauw (blog | Twitter | Identi.ca)
Yep, PHP. Though some people consider this to be a horror on itself, that's not what I am getting at here :)
Jeroen De Dauw (blog | Twitter | Identi.ca)
Came across this "class" during a code review. My reaction is pretty much "the fuck is this". Do you think this is good code or not?
class Listalizer {
public static function listalize( $convert, &$data ) {
if ( is\_object( $data ) ) {
try {
$data = call\_user\_func( $convert, $data );
} catch ( \\Exception $ex ) {
try {
trigger\_error( "listalize: Callable " . self::callable2String( $convert )
. " caused an exception: " . $ex->getMessage(), E\_USER\_WARNING );
} catch ( \\Exception $exx ) {
// Argh! We \*can't\* throw an exception!
$exx = (object)$exx; // this is just a breakpoint anchor.
}
$data = false;
}
}
if ( is\_array( $data ) || $data instanceof \\ArrayObject ) {
foreach ( $data as $key => &$value ) {
self::listalize( $convert, $value );
}
}
}
public static function objectify( $convert, &$data, $role = null ) {
if ( is\_array( $data ) ) {
try {
$data = call\_user\_func( $convert, $data, $role );
} catch ( \\Exception $ex ) {
try {
trigger\_error( "objectify: Callable " . self::callable2String( $convert )
. " caused an exception: " . $ex->getMessage(), E\_USER\_WARNING );
} catch ( \\Exception $exx ) {
// Argh! We \*can't\* throw an exception!
$exx = (object)$exx; // this is just a breakpoint anchor.
}
$data = false;
}
}
if ( is\_array( $data ) || $data instanceof \\ArrayObject ) {
foreach ( $data as $key => &$value ) {
self::objectify( $convert, $value, $key );
}
}
}
public static function callable2String( $callable ) {
if ( is\_array( $callable ) && count( $callable ) === 1 ) {
$callable = array\_pop( $callable );
}
if ( is\_string( $callable ) ) {
return $callable;
} elseif ( is\_object( $callable ) ) {
return get\_class( $callable ) . '->\_\_invoke';
} elseif ( is\_array( $callable ) ) {
$target = $callable\[0\];
You should commend them! Seriously, this is good work. If you consider that most people touching code are idiots, it's always good to duplicate code around, so that if someone messes up one copy, only a small part of your app gets broken, rather then the whole thing.
Jeroen De Dauw (blog | Twitter | Identi.ca)
One free interwebs, you just won it! :)
Jeroen De Dauw (blog | Twitter | Identi.ca)
He forgot to wrap each line in a try catch block! o_O
Jeroen De Dauw (blog | Twitter | Identi.ca)
A true classic. I got thought to not do this first weeks in programming class, before learning stuff like functions.
Jeroen De Dauw (blog | Twitter | Identi.ca)
Believe it or not, but my high school teacher recommended doing this "because it's more clear". I was the only one ignoring that and only got flak for it.
Jeroen De Dauw (blog | Twitter | Identi.ca)
#1, if you start counting at the first number.
Jeroen De Dauw (blog | Twitter | Identi.ca)
Why is he copying the code?! He could just call the function this stuff is in again, and have it be properly recursive as a bonus.
Jeroen De Dauw (blog | Twitter | Identi.ca)
Oh, it still works when omitting the opening tag? Cool, then I can omit that one in my code and have faster page loading!
Jeroen De Dauw (blog | Twitter | Identi.ca)
If it's the real name, it's a bigger horror then the relatively small mistake of redirecting to the wrong page...
Jeroen De Dauw (blog | Twitter | Identi.ca)
I just went in to fix a compatibility issue in some code and ran into this beautiful 27 lines long list of variables set to an empty string. Then the code has a loop going through the provided data, which has a list of if statements in it, of which you can probably guess the length. All but 5 or so of these ifs have duplicate code. After the loop all these variables are passed to a constructor, which holds another nice long list of if statements... Code: http://pastebin.com/uz3uMy0g[^] It's GPL v2, so do feel free to use it!
Jeroen De Dauw (blog | Twitter | Identi.ca)
modified on Sunday, June 5, 2011 10:42 AM
#doingitwrong
Jeroen De Dauw (blog | Twitter | Identi.ca)
How else would students learn to deal with crappy code? When they graduate, the crappy teacher will just be replaced by a crappy boss and crappy colleagues :)
Jeroen De Dauw (blog | Twitter | Identi.ca)