When recently I got a little distressed about the different coding styles across projects (and departments) and variable naming went crazy again, I decided I would not rest before we implemented a style guide, and this time I would really push through!
Why is this important?
Why is having a style guide so important to me? I started writing down the reasons why I think having a style guide is a good thing (next to keeping me from being annoyed). I shared them with my team and I would like to share 'm with you too:
1. For consistency
This is the most obvious one, but we should write and style code in a consistent manner. Not only in your department, but across all departments in your company that produce code. This way we enforce a readable style that everyone in the company (and third parties) can understand and read. I really like the idea that when someone reads code, he or she should not be able to identify who wrote it.
2. For the new kid
When a new employee starts in your team, a new style guide will give him or her a guideline on how the team writes code. This will get the newbie up and running fast.
3. To learn how to be a better coder
4. For simplicity & focus
When having a style guide in place, we all know how to style our code, and we don’t have to think about styling and basic stuff anymore. We should not be thinking about whether we should write our functions as an expression or as a declaration. Let's focus on the interesting parts!
5. As a guideline for code reviews
Although code reviews are not solely about style, to me styling is important when reviewing one's code. I truly believe that 'x' is not a proper variable name, but some people think it is. I've had a lot of these discussions on coding style during code reviews. When you have all agreed on a style guide, there is not a lot to discuss anymore. When you’ve documented that
x is not a descriptive and clear name for a variable, we do not have to discuss this over and over again during code reviews.
I got it. Now what style guide should we use?
I am a big fan of the Airbnb Style Guide, because it's well-documented and they seem to have thought everything through. Plus we can integrate the settings directly into our Webstorm (or Pycharm) environment and automate stuff using JSCS. They also updated their beloved guide to ES6, so that's cool. But in the end, choosing a style guide is just a matter of taste. You can't choose a wrong one, as long as you choose.
When I first suggested the Airbnb style guide to the team, the tabs vs. spaces indentation discussion started all over again. People asked whether we could do some exceptions on the given style guide. Well, of course we could. We'd just have to fork Airbnb's repo and change it to our liking.
But I strongly recommended to stick with their guide, because I don't want our guide to get outdated when they choose to update theirs. We had to give in one some small issues, but the team agreed!
Of course discussing certain styling decisions is always educative and interesting, so you should never stop questioning your style guide.
Btw, if you are serious about styling and clean code, you should check out Clean Code: A Handbook of Agile Software Craftsmanship by Robert C. Martin.