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.

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralIs this bad or am I just picky?memberJeroen 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?memberBrisingr Aerowing29 Jan '13 - 2:56 
Ick. That's PHP, right?

Bob Dole
The internet is a great way to get on the net.

D'Oh! | :doh: 2.0.82.7292 SP6a

GeneralRe: Is this bad or am I just picky?memberJeroen De Dauw29 Jan '13 - 3:21 
Yep, PHP. Though some people consider this to be a horror on itself, that's not what I am getting at here Smile | :)

JokeRe: Is this bad or am I just picky?mvpMarcus Kramer29 Jan '13 - 4:23 
Could be worse. It could have been vb... Smile | :)
I wasn't, now I am, then I won't be anymore.

GeneralRe: Is this bad or am I just picky?memberBob G Beechey1 Feb '13 - 17:10 
I am getting tired of silly comments about vb. Presumably by someone who has not checked out vb since vb6.
GeneralRe: Is this bad or am I just picky?mvpMarcus Kramer1 Feb '13 - 18:28 
Apparently you missed the Joke icon. I've had to do plenty of vb and vb.net work over the years so I do not speak ignorantly on the matter. Smile | :)
I wasn't, now I am, then I won't be anymore.

GeneralRe: Is this bad or am I just picky?memberBob G Beechey1 Feb '13 - 18:31 
Damn. I knew I had to get my eyes tested.
GeneralRe: Is this bad or am I just picky?memberBrady Kelly1 Feb '13 - 23:56 
Last year I translated a whole lot of VB6 code into VB.NET 10. After the project was over he complained that most of my code was unintelligible and "I had used LINQ!"
GeneralRe: Is this bad or am I just picky?memberH.Brydon1 Mar '13 - 9:39 
Bob G Beechey wrote:
I am getting tired of silly comments about vb. Presumably by someone who has not checked out vb since vb6.

I've used VB2 through VB6 and VB.Net (2002) through VB.Net 2005.
 
VB's mother wears army boots!
--
Harvey

GeneralRe: Is this bad or am I just picky?memberGary Huck1 Feb '13 - 3:01 
Okay ... what are you getting at?

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


Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 24 May 2013
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid