Working in a team, it is important to agree and stick to code standards. This helps a team in many ways:
- No disagreement between team members about code style - it's agreed already
- No need to spend your time thinking about code style - once it's agreed, you can focus on writing the code
Perhaps most importantly though:
- Once code standards are set, a Git commit shows only what changed in the way the code works and not what changed in the way the code looks
Consider a team of two developers - one insists on using semicolons at the end of each line, while the other believes it's better practice to omit them. Every time one team member works on code that was written by their partner, their auto-formatter will remove all the semicolons, or add all the semicolons to every line of code in the file meaning that every time one of them git commit
s their work, the commit looks as if every line of code in the file has changed
Install prettier
extension for your code editor and set up auto-format on save. Try auto-formatting your code from this week.
- Does it change? How?
- What do you like about using this?
- Is there anything you dislike?
- How do you choose your own settings for Prettier?
Take a look at some existing style guides to see what conventions are commonly used.
- Airbnb Javascript style guide
- Google JS Style Guide
- StandardJS Rules (beware the name, this isn't necessarily "standard")
We need to agree as a group what our code standards / style will be. If possible, involve your Beamery buddies in the conversation so you can get a sense of what is the commonly agreed style at Beamery. The W3 Schools page on JS standards has a lot of useful info.
- ES6 function definitions
const myFunc = () => {}
- when should you use them? Always? Or in certain cases? - Semicolons or no semicolons?
- Line indent - tabs? spaces? how many?
==
/!=
allowed? or not allowed?var
- allowed? not allowed?- Naming conventions - camelCase? kebab-case? Where might you use them?
Come up with a list as a group of 10 or so rules that we will stick to in our code this week. Make sure all the code in your project meets these rules by the end of today.