Click here to Skip to main content

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrasing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
GeneralRe: Drinking and coding Pinmemberjoe_j1-Feb-13 3:05 
GeneralRe: Drinking and coding PinmemberScott Burkow1-Feb-13 3:56 
GeneralRe: Drinking and coding PinmemberRob Grainger6-Feb-13 22:31 
GeneralRe: Drinking and coding PinmemberNagy Vilmos6-Feb-13 22:34 
GeneralRe: Drinking and coding PinmemberGary Wheeler1-Feb-13 0:33 
GeneralRe: Drinking and coding Pinmemberjoe_j1-Feb-13 1:11 
GeneralRe: Drinking and coding PinmemberS Houghtelin1-Feb-13 2:52 
GeneralIs this bad or am I just picky? PinmemberJeroen De Dauw29-Jan-13 2:44 
Came across this "class" during a code review. My reaction is pretty much "the f*** 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];
            $method = $callable[1];
 
            if ( is_string( $target ) ) {
                return "$target::$method";
            } elseif ( is_object( $target ) ) {
                $class = get_class( $target );
                return "$class->$method";
            } else {
                return '(array)';
            }
        }
 
        return '(' . var_export( $callable, true ) . ')';
    }
}

GeneralRe: Is this bad or am I just picky? PinmemberBrisingr Aerowing29-Jan-13 2:56 
GeneralRe: Is this bad or am I just picky? PinmemberJeroen De Dauw29-Jan-13 3:21 
JokeRe: Is this bad or am I just picky? PinmvpMarcus Kramer29-Jan-13 4:23 
GeneralRe: Is this bad or am I just picky? PinmemberBob G Beechey1-Feb-13 17:10 
GeneralRe: Is this bad or am I just picky? PinmvpMarcus Kramer1-Feb-13 18:28 
GeneralRe: Is this bad or am I just picky? PinmemberBob G Beechey1-Feb-13 18:31 
GeneralRe: Is this bad or am I just picky? PinmemberBrady Kelly1-Feb-13 23:56 
GeneralRe: Is this bad or am I just picky? PinmemberH.Brydon1-Mar-13 9:39 
GeneralRe: Is this bad or am I just picky? PinmemberGary Huck1-Feb-13 3:01 
GeneralRe: Is this bad or am I just picky? Pinmemberihoecken29-Jan-13 22:00 
GeneralRe: Is this bad or am I just picky? [modified] Pinmembers_mon30-Jan-13 10:03 
GeneralRe: Is this bad or am I just picky? PinmemberShameel30-Jan-13 22:25 
GeneralRe: Is this bad or am I just picky? Pinmemberjim lahey1-Feb-13 1:19 
GeneralRe: Is this bad or am I just picky? PinmemberS Houghtelin1-Feb-13 1:59 
GeneralRe: Is this bad or am I just picky? PinmemberSitalkes1-Feb-13 3:35 
GeneralRe: Is this bad or am I just picky? PinmemberScott Burkow1-Feb-13 3:36 
GeneralRe: Is this bad or am I just picky? Pinmembersatovey1-Feb-13 13:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.


Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 30 Aug 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid