Code Standards 1

Since starting our framework (Cubex) at JDI, we decided to bring in some strict code standards, which will fail our CI builds if code does not conform.  Around the time of starting, we had not long switched over to Phabricator, who provide some great documentation on various coding standards.  We are also following the PHP-Fig and the code standards put together there.

There are some guides in PSR-2 that I don’t like because I have been doing it differently in so many years, and others that I believe make your life harder.  That said, most of the PSR-2 style guide is great.

Braces
Coming from a microsoft VB/C#/ASP background, Allman braces are one thing that I just can’t let go of.  Without it code looks far too cramped, and loses some of its readability.  In addition, this keeps all braces conforming to a single standard.  We arent putting braces on a new line if its after a class or method definition, and then on the same line on a control structures.

Character Limits
PSR-2 specifies a soft limit of 120 characters, and a recommendation of 80.  This I do like, but we are a little stricter on the limits, 120 characters should be your absolute character limit, while any code over 80 should warn you.  Switching out from our previous “whatever” limit, to a strict 80 took a few days/weeks to really settle with, but there is a great benefit in doing so. If not only to stop your code from getting too complex in a short block.  With indenting rules, and the line limits, its difficult to deeply nest code within a single method, and pushes you to abstract complex code, and reduce your general coding complexity.

Tabs vs Spaces
Im not really going to get into the whole tabs vs spaces battle, because its been so widely discussed, and frankly, if you are using tabs, you are just doing it wrong.  However, I also don’t like the 4 character limit alongside the 80 character limit.  If you have 4 spaces per indent, and want to write some code within a class method, you have already lost 8 characters to your class and method definitions, leaving you with a 72 character limit.  That just sucks!.  Phabricator on the other hand, use a 2 space indent, which at first looked odd, but after taking the plunge and switching our standards and auto formatters to a 2 character indent, the world is just a better place.  Alongside allman braces and the 80 character limit, code doesn’t lose any readability, and allows you to verify an object is an instance of a class within a single line.

Share/Bookmark