Zend Coding Standards and Curly Brackets.

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Zend Coding Standards and Curly Brackets.

Post by onion2k »

http://framework.zend.com/manual/en/cod ... style.html

Maybe someone here can figure this out. Why do Zend specify that classes and functions should have the opening brace on a seperate line, and if blocks and switch case blocks have the brace on the same line? Eg

Code: Select all

class MyClass
{
//stuff
}

function MyFunction()
{
//stuff
}

if ($var===0) {
//stuff
}

switch ($var) {
  case 1:
    //stuff

  case 2:
    //stuff
}

I can't think of any good reason not to keep them all the same.
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

I don't know... Ask them :P

Personally, I prefer the "one true brace" form and I use it for classes, functions and conditional statements.
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

I've always done it the "Zend way". It's nice to be able to quickly distinguish between business logic and the main method to which it belongs, especially when you're dealing with large classes. A normal if block will only consume a few lines, and I feel like the extra brace just takes up space unnecessarily. For method declarations, which may encapsulate hundreds of lines, it's much easier to spot when you're scrolling up a 500 line file at coffee-induced speeds trying to find what the argument variable names are. I guess it's mostly a matter of taste, though.
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

It's a small compromise between two competing styles. The PEAR conventions are basically the "One True Brace" style. The main advantage is to fit as much code as possible into a screen. You'll notice it's always accompanied by an "80 character" line limit - the width of a typical terminal display... This was amended later to exclude class and method declarations - far more important to segregate them visually!

The other is BSD/Allman - all braces occupy a single line. The advantage here is to improve readability, and further visually separate blocks of code. Unfortunately it's rarely applied in PHP - every time it's debated someone turns up who actually seems to use a limited console editor over and IDE or wrapping editor where screen space is limited.

Of course the debate is moot in a braceless language - the fun argument there is over camelcasing ;).

I use the BSD/Allman where possible - the other is reserved for only a few cases where coding standards like PEAR or ZF require it. At the end of the day it's a preference, and is usually best to apply whichever you want consistently in a project. For those exceptions I find a find/replace works pretty well, or having two autocomplete settings depending on the standard.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Well, the reason I ask is because I can see myself moving on from my current job sometime in the not too distant future, so I'm looking for a documented standard coding style I can shift over too instead of "what Onion likes". Just something nice to show in code samples should anyone ask for them. I was reading the documentation and wondered why it might be the case.
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

It's not a bad standard - it's consistent, and been in use in PHP for years. Adopting it is as easy as configuring your IDE for it (if needed) and banging away at your code as usual. If you really really like consistency there's a PEAR analyser which checks PHP files for adherence to the PEAR standard.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

onion2k wrote:Well, the reason I ask is because I can see myself moving on from my current job sometime in the not too distant future, so I'm looking for a documented standard coding style I can shift over too instead of "what Onion likes". Just something nice to show in code samples should anyone ask for them. I was reading the documentation and wondered why it might be the case.
Ask in your interview or when you start (when/if you get one) if they have a coding standard.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Develop your own and make it the company standard. That is what I did and it took to the rest of the developers pretty well.
wei
Forum Contributor
Posts: 140
Joined: Wed Jul 12, 2006 12:18 am

Post by wei »

This may seem obvious. If there is legacy code, follow the coding style in that code. Having multiple styles in different file sounds terrible, but having different styles in the same file is horrible to read. The same should apply to code usage, e.g. some code will prefer to use one set way of doing things, some others. Consistency is a good thing.

You can alway buy a widescreen and turn it sideway to give you more screen length.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Legacy code can be updated. When I came into my role as the web developer for my company, the first thing I did was establish standards for PHP, CSS and HTML. Then I developed a standard for code control, then code implementation. All legacy code was to updated to the new standard every time it was touched.

Yes, there is some old code style around, but as soon as it gets opened again, it is getting updated to the standard.
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

Legacy code rarely has a standard anyway ;). Most of the legacy PHP stuff I've seen tends just to be a messy clump. Cleaning it up to a standard is almost a necessity before doing anything else.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

I like the Zend braces because you can easily tell from a glance what is a function or class and what is a control block. So for me its more than a compromise its better than either on their own.
User avatar
dreamscape
Forum Commoner
Posts: 87
Joined: Wed Jun 08, 2005 10:06 am
Contact:

Post by dreamscape »

ole wrote:I like the Zend braces because you can easily tell from a glance what is a function or class and what is a control block.
umm... the keywords "class" and "function" are dead giveaways and more easily spotted than brace position.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

bah its just not the same.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

For the record, I am not a fan of the Zend coding standard (or the PEAR standard). I implemented a modified phpBB standard myself.

Not to bash. I would rather have a standard I don't like than no standard, but if I was given a choice, I would not choose the Zend/PEAR standard.
Post Reply