Now you can go all crazy with coding standards and you can write hundreds of pages of coding standards that developers on teams should follow, and believe me I’ve seen it! Problem with this is developers are more concerned with following these standards than solving real customer problems.
You can also just use the default ‘standards’ that Visual Studio or Visual Studio Code but sometimes developers change the default settings which causes some issues and primarily deal with developers having preferences such as ‘curly brace on same line or next line’ as follows
as opposed to
When developers have different settings in their IDEs consider the following scenario. If developer A implements the first function, then Developer B has goes in the same file and modifies a different part of the file and the IDE formats the file to their local workstation standards, then GIT will not only take the code changes, but will also take the formatting changes. These are really superficial changes in my opinion.
For the web applications we build at RedBit we use React and sometimes NodeJS so if you don’t have standards in place, things could get a bit messy. Some developers like spaces instead of tabs, curly braces on the first line like above but everyone has differences.
We have implemented StandardJS because it’s simple and allows the team to focus on developing code, not worrying about standards.
The following are the ‘The Rules’ in place out of the box
To get everything installed you need to have NodeJS installed which will give ‘npm’. There are two ways to install StandardJS. First is locally for your project and it’s as simple as running the following for saving in your local project
npm install standard –save-dev
Second is globally so you can run it from anywhere in the command line on your files.
npm install standard –global
Recommend still running the local one, so your ‘package.json’ gets updated to show StandardJS within ‘devDependencies’ section.
When you do an ‘npm init’ to initialize a new ‘package.json’ you will get something similar to the following
Now you can change the ‘scripts’ section to something as follows
If you have a ‘start’ script you can do something along the lines of
Let’s assume I had the following
StandardJS would give the following warning:
One thing that we try to avoid is committing code with active warnings in the code base. This is not always possible but we try to avoid as much as possible.
To circumvent this we do two things
The StandardJS site has a perfect example on ‘pre-commit’ hook and you can find it here but here is the pre-commit hook for reference
If you don’t know what a ‘pre-commit’ hook is see Git Hooks on Atlassian site.
We have a process where we go through a code review on every PR, but our PR process also attempts to build the code that is included in the PR and runs tests and runs StandardJS as part of the build process.
In our ‘package.json’ we usually have something as follows
which will run StandardJS when we build the React project.
The two above solutions tend to work fairly well except when someone removes their pre-commit hooks or never sets them up. The automated builds we have in VSTS are acting like a ‘gateway’ for our PR process before we even start reviewing code, so if the committed code does not comply with standards, the developer will get notified.
Second options is to just run a simple command and that is
Standard will then go in and fix and spacing issues, tabbing issues etc.
Your settings should look something like the following
Now whenever you save a file, it will make sure you are adhering to ‘standardjs’ standards and life is good!
So as developers we all have our different styles of writing code, but when working with a team you should definitely look at putting some standards in place and making it as easy as possible for developers to follow those standards. This will allow developers to focus on solving customer problem and not worry so much about code formatting.
At RedBit, we have teams of people working on projects at one time. One of the key items to practice in any software development project is coding standards.
What happens if you wait too long to replace old technology?