views:

83

answers:

2

I was going through some of the code for Dolphin CMS recently (a craptastic example for PHP haters) when I ran across this little gem in /grp.php:

{
    switch ( $_REQUEST['action'] ) {
        //a crapton of switch statements
    }
}

I'm curious what the wrapping curly braces, "{}" without any keywords, are about. I do PHP development primarily so maybe (hopefully??!!?) this is something I just don't know about but I've tried removing the braces and the code runs as usual.

I'm having a hard time understanding wtf the purpose for this would be.

Any ideas?

(NOTE: This is really just for my own edification. I'm almost having trouble sleeping because this isn't making any sense to me...)

+4  A: 

I've seen this in large blocks of procedural code. It seems to help group logical blocks together that might normally be abstracted to a separate function. This, somewhat, helps when editing and your IDE can quickly identify the block by the braces.

Functionally, I don't think it does anything. Visually, it helps group code.

Mike B
So the "programmer" (with quotes cause now I doubt it) effectively mucked up the code to make it easier on him? Kind of lazy and a little dickish practice if you ask me. Accepted :)
Eric Lamb
Just because this kind of off-the-cuff dismissal of other people's work irks me a bit: As a note of courtesy to other developers, I always assume first that the WTF code was put there for a good reason. I say this because I know sometimes I do things that at first glance seem somewhat tangential to solving the problem - however whenever I'm forced to do this I leave a detailed comment explaining why I did what I did. I think it's best to assume the guy knew what he was doing unless and until you gather good evidence to the contrary. Be nice, it could be your code next time. Sorry, my pet peeve
Iain Fraser
I get your point man but this wasn't meant to be off-the-cuff dismissive; it was supposed to be plain old, run of the mill, dismissive. It's one of my pet peeves to complicate things for an IDE. Seriously. I kind of wish I was being snarky but I'm really not :)Adding special markup to the code to improve a single developers IDE experience is just lazy. I agree that if you have to do something lame like this just place a comment. In fact, the lack of a comment kind of forced me to look for the solution.
Eric Lamb
Lain; upvoted your comment to show sincerity in not trying to flame.
Eric Lamb
Thanks mate ;). Someone has to be the etiquette police, haha.
Iain Fraser
A: 

Without more context, it's hard to say. They may be attempting to restrict the scope of certain variables. I don't know PHP's scoping rules specifically, but they may be trying to ensure that variables modified inside that switch statement don't affect things outside of the block. Or, they may be using a RAII pattern.

pioto