PSR-2, Van Halen and M&M’s

So, how do you get 4 space tabs? I’ve been trying out new text-editors for the past couple of months. After using Sublime for many years I decided to see whats changed in the world of IDEs. I was recommended PHPStorm but just couldn’t get to grips with it and now I’m giving Atom a go. Atom if you don’t know is Githubs text editor and has just been moved to version 1.0.  First time I tried it, it was still in beta. Back then it was a little lacking a bit slow and not much to it. Now almost a year later I decided to give it another go.

So, how do you get four space tabs? “Its just in settings” I go in settings and yes there seems to be a way to alter tabs to be the size of four spaces but it just seems to be a tab block. So I ask again and get the same answer. I explain the problem and say but if it does this how do I make the code PSR-2 compliant? and the answer I get is why would you want to?

Van Halen and M & M’s

The band Van Halen had a clause written into all their contracts that required a bowl of M & M’s with all the brown ones removed in their dressing room. Now you’ve probably heard this story before and its regularly told as a joke about egotistical rock stars making unreasonable demands. But actually the thinking behind it is quite logical. If they walked into their dressing room and found a bowl of M & M’s with brown ones then they knew the contract hadn’t been read properly. Now the contract contained all sorts of clauses pertaining to stage safety and other requirements. With a huge touring rock band each venue needs to safe and to be able to handle the massive influx of people. The simple M&M clause showed that they hadn’t read the contract properly, this could mean they hadn’t secured the stage properly or hired the right security. This could lead to major problems and even dangers at the gig.

Coding standards can be looked at in the same way. Our goal as developers is to create easy to read usable code that can be simply handed off to other developers. Using a standard makes bugs easier to spot and the code not only more usable but also more re-usable. One way I like to think of it is like this. Going back to Van Halen say they are writing a new song. They decided to write a love ballad about a girl the lead singer met on tour. They have a meeting to discuss it and lay out some rough rules. But instead of working together they decide that this time they are going to write each part of the song separately and the pull it all together at the end. So the lead guitarist writes his section and the drummer his etc etc. So comes the final day when its time to join the song up. The drum tempo doesn’t match the guitar , the bass player is just going mental in the corner, Vocals are all over the place. Its pretty much a disaster and they have to go back to the drawing board.

Changing to the metaphor back to coding this means bugs and at the very least time and money lost through refactoring. Obviously theres no way a whole developer team can work together on one feature at a time. Thats ridiculous. But if your teams is building without standards in place then you are going to run into problems when integrating that code together. This is just a fact. If you’re part of a team sit down and decide on a coding style for whatever language you are writing and stick to it. Use code reviews to at least make sure this standard is being followed. This process doesn’t have to be painful we are all human and everyone makes mistakes. Everyone has slightly different coding styles this can just help the process run a bit smoother.

PSR-2 and other standards

To look briefly at another language I recently attended a Javascript workshop given by Kyle Simpson ( @getify ). Part of the workshop he spoke about the use of semicolons in Javascript. Recently theres been a bit of a divide in the community on whether each line of JS needs to have a semicolon at the end of it. This is because when the language compiles it automatically adds in the semi colons when you miss them. Personally I think you should because thats the way the language works. Not following these rules can lead to problems and some bugs are literally down to missing a semi colon. About this subject Kyle Simpson ( @getify ) said (and I’m paraphrasing here) “If you are writing JS without semicolons you are writing the worst code you can that still works”. This is how a feel about standards if you code without a decided style the code you write may work but will be horrible to maintain. PSR-2 and all the other standards and styles have there place to make code easier to maintain and for other developers to use. Without a style whether you realises it or not you will be causing your self more problems in the long run.

 

PSR-2 standard

You don’t know JS – Kyle Simpson

Posted by