views:

140

answers:

4

Possible Duplicate:
Which Coding convention to follow for PHP?

After coding for a while similar in terms to the coding style offered by zend, im constantly finding myself getting told to change my code at work. They prefer a different coding style. For example a huge annoyance for them is using:

funtction testFunction
{

}

instead of

function testFunction {

}

To me the first option is much more readable. The same for class declerations.

Anyway there are more little things like this and overall i feel that the code i write at work is much less readable than other code i write using my own style.

Are there any industry wide best practices for code standards?

+5  A: 

Zend Framework and PEAR standards are pretty much the most common coding conventions. If your company adopted another one, stick to the your company's convention though. Better than having no convention at all. And they only work if everyone sticks to them.

Also see:

Basically, all of the major frameworks have a coding convention somewhere in their documentation. The official (but mostly unknown IMO) PHP Coding Guidelines can be found at

If you need to validate code against a coding convention, consider using CodeSniffer. Some IDEs also offer automatic sourcecode formatting by templates. For instance Zend Studio has the ZF coding guidelines built-in, so it's just a click to format code to that convention.

Gordon
+1 Tragically his company *don't* use these conventions, but as you say they are the effective standard.
middaparka
@Gordon - Ah yes, just seen your edit. Just made pretty much the same point myself. :-)
middaparka
@middaparka yeah, sorry for not being clearer. Still too early :)
Gordon
+1  A: 

As Gordon says, the Zend and PEAR standards are the effective industry standard.

However, the company's code quite possibly pre-dates these so depending on the size of the code base there may be little value in investing the time to make the move to one of these. (That said, if they ever want to use static code analysis tools you could possibly use this as an impetus to seriously consider moving to Zend, etc.)

However, being realistic, as long as they have a sensible standard that they stick to there's no real issue here - you'll find yourself adjusting how you "see" the code accordingly.

middaparka
+1 `as long as they have a sensible standard that they stick to there's no real issue here - you'll find yourself adjusting how you "see" the code accordingly.`
George Marian
A: 

Coding styles vary between groups and it isn't a one size fits all type of thing. The most important thing is having a standard that's followed consistently and not going overboard. Too many rules can be just as bad not enough.

I used to prefer the K&R style (the second one). After having to adjust to the Allman style (your preference) I know feel that it makes code more readable and have changed my preference.

This Wikipedia article is a decent place to start: http://en.wikipedia.org/wiki/Programming_style

(It also includes a link to the PEAR Coding Standards, among others.)

George Marian
A: 

There are pros and cons to any coding style. I spend a lot of time working with code from many sources doing integrations so sometimes end up seeing many different styles in a single day (different naming conventions, braces placement, tabs vs spaces etc)

As far as I'm concerned - the most important thing if you are working with existing code is to follow the style of the code that you are editing. If you don't you make things harder for anyone following after you.

If you are writing new code than you should have freedom to do it the way that makes you most efficient.

I find that company coding guidelines are often far to detailed and end up being forgotten after a few years and a bit of churn in the software team ;-)

Adam