Writing clean and maintainable code is pivotal to any production project. And yet even the most functional code is frowned upon if it doesn’t follow the coding standards that a company sets. A good engineer knows that any big changes need to go through a code review before being merged into the mainline. Yet the most frequent comments made in those types of reviews are often about violations of any coding standards (not the functionality). So why do we care so much about coding standards? Do people really debate tabs versus spaces?
Defining a Coding Standard
Why is a coding standard important?
- It creates a consistent look across the entire code base.
- Enables readers to understand the code more quickly.
- Easier to maintain the code base during merges.
- Easier to check for following best practices.
Most of the time when working at a company they already have a coding standard provided that hopefully is written in a way that is easy to understand. But let’s say you are starting a pet project that you plan to open up to the community as open source. What standard do you follow and how do you communicate it to the people that will hopefully be contributing? Well you don’t have to reinvent the wheel. There are plenty of pre-made coding standards out there. Here is a list of some of the good ones I found:
- Google’s C++ Style Guide
- Microsoft’s C# Coding Conventions
- Kotlin’s Coding Conventions
- Python’s Style Guide
- Swift Design Guide
- Way more are listed here!
Tips for Following the Standard
I really only have one tip for helping to follow a style guide. Make it impossible to fail. Setup your IDE so that it yells at you every time it finds something that violates the standard. Is that super annoying when you’re just trying to get the code to work? Absolutely, but it is also the only way that you will learn to conform to the standard. Plus some of the rules are annoying.
Follow this tutorial for how to add your coding standard to your IDE
Pro tip: Most standards define a max number of characters per line. Setup your editor to display a vertical line at that mark like the line on the right in the screenshot below:
Tabs Versus Spaces?
The answer is spaces. Why? Because…. It just is. Honestly, I’m fine with either. Just never mix the two. Be consistent.
So do you follow a written coding standard or any standard convention? Let me know in the comments below!