Tabs Versus Spaces: Defining a Coding Standard
Tags: programming, discuss, watercooler, coding
Published: 2021-05-24 06:05:27

<div class="modal-body">
<p>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&nbsp;spaces?</p>
<h3><strong>Defining a Coding&nbsp;Standard</strong></h3>
<p>Why is a coding standard important?</p>
<ul>
<li>It creates a consistent look across the entire code&nbsp;base.</li>
<li>Enables readers to understand the code more&nbsp;quickly.</li>
<li>Easier to maintain the code base during&nbsp;merges.</li>
<li>Easier to check for following best practices.</li>
</ul>
<p>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&nbsp;found:</p>
<ul>
<li><a href="https://google.github.io/styleguide/cppguide.html">Google’s C++ Style&nbsp;Guide</a></li>
<li><a href="https://docs.microsoft.com/en-us/dotnet/csharp/fundamentals/coding-style/coding-conventions">Microsoft’s C# Coding Conventions</a></li>
<li><a href="http://contribute.jquery.org/style-guide/js/">jQuery Foundation’s Javascript Style&nbsp;Guide</a></li>
<li><a href="https://kotlinlang.org/docs/coding-conventions.html">Kotlin’s Coding Conventions</a></li>
<li><a href="https://www.python.org/dev/peps/pep-0008/">Python’s Style&nbsp;Guide</a></li>
<li><a href="https://swift.org/documentation/api-design-guidelines/">Swift Design&nbsp;Guide</a></li>
<li><a href="https://github.com/Kristories/awesome-guidelines">Way more are listed&nbsp;here!</a></li>
</ul>
<h3><strong>Tips for Following the&nbsp;Standard</strong></h3>
<p>I really only have one tip for helping to follow a style guide. <strong>Make it impossible to fail</strong>. 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.</p>
<p><a href="https://www.jetbrains.com/help/idea/configuring-code-style.html#editorconfig">Follow this tutorial</a> for how to add your coding standard to your&nbsp;IDE</p>
<p>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:</p>
<figure><img alt="" src="https://cdn-images-1.medium.com/max/651/0*JOUwWK12JNatO7qK"></figure><h3><strong>Tabs Versus&nbsp;Spaces?</strong></h3>
<p>The answer is spaces. Why? Because…. It just is. Honestly, I’m fine with either. Just never mix the two. <strong>Be consistent.</strong></p>
<p><strong>So do you follow a written coding standard or any standard convention? Let me know in the comments&nbsp;below!</strong></p>
<img src="https://medium.com/_/stat?event=post.clientViewed&amp;referrerSource=full_rss&amp;postId=c17a2269d2e1" width="1" height="1" alt="">
</div>