<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Christos Litras</title>
    <description>The latest articles on DEV Community by Christos Litras (@christoslitras).</description>
    <link>https://dev.to/christoslitras</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F152208%2Ff28624c8-7cfc-4726-be10-2ea34e37b2e4.png</url>
      <title>DEV Community: Christos Litras</title>
      <link>https://dev.to/christoslitras</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/christoslitras"/>
    <language>en</language>
    <item>
      <title>Never use TABS for code indentation</title>
      <dc:creator>Christos Litras</dc:creator>
      <pubDate>Mon, 05 Jul 2021 13:35:06 +0000</pubDate>
      <link>https://dev.to/christoslitras/never-use-tabs-for-code-indentation-3ln2</link>
      <guid>https://dev.to/christoslitras/never-use-tabs-for-code-indentation-3ln2</guid>
      <description>&lt;p&gt;Recently I figured out there is quite a debate regarding Tabs vs. Spaces usage for indenting code and this debate goes on for decades and still there are people as of today that they’re still using tabs.&lt;/p&gt;

&lt;p&gt;I have to admit that on my early steps when I start programming back in the 90’s, I also used tabs for indentation especially when there was no difference at all using DOS IDE’s such as Quick BASIC and Turbo Pascal and later using Visual Basic and Visual C++.&lt;/p&gt;

&lt;blockquote&gt;
&lt;h2&gt;
  
  
  “And then came the WEB!”
&lt;/h2&gt;
&lt;/blockquote&gt;

&lt;p&gt;With HTML/JS/CSS and code becoming more and more open, people started to share and show their code to other people. Not everybody have the same editors nor the same editor settings and using Tabs for indentation and Spaces for alignment may work nice inside an IDE/Code Editor but inside a web page there will be a mess.&lt;/p&gt;

&lt;p&gt;Sample code using Tabs for indentation and Spaces for alignment:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--U96oBeX8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3at2mmewh221yncs2pcy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--U96oBeX8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3at2mmewh221yncs2pcy.png" alt="Tabs mess"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see, the code block is a mess! The solution to this, is to use the CSS property &lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/tab-size"&gt;tab-size&lt;/a&gt;, with a value set to &lt;code&gt;4&lt;/code&gt; rather than the initial value of &lt;code&gt;8&lt;/code&gt;, but then again, what if the users has somehow set their editor actual tab width to display as &lt;code&gt;2&lt;/code&gt; spaces? You can see where this is going and it’s not what “tab fans” are talking about, which they claim that tab invented for this purpose. &lt;strong&gt;NO&lt;/strong&gt;, Tabs where not invented for indentation purposes and usage, their main usage was for presenting &lt;strong&gt;Tabular data&lt;/strong&gt; on old screen that have had the capability of displaying mostly 80 X 25 characters, so, in order to align columns, a special character with variable and adaptive width invented:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--gA4DAF-5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nuf6hluzzn000sil2k6z.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--gA4DAF-5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nuf6hluzzn000sil2k6z.jpg" alt="DOS Tabs"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now let’s see that same code using Spaces with 4 spaces tab size width:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1L8L1h9k--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qn5y1bs0ylszrhojpqyg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1L8L1h9k--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qn5y1bs0ylszrhojpqyg.png" alt="Tabs with 4 width"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Much better and without worrying about browser default tab width nor any other tab misinterpreted!&lt;/p&gt;

&lt;p&gt;An other argument that “tab fans” claim, is that with tabs people can set the tab width to whatever they like to see on their editors, &lt;strong&gt;but&lt;/strong&gt;, that is far from reality; for example, image our &lt;code&gt;MyFunc&lt;/code&gt; code with tabs and setting CSS property &lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/tab-size"&gt;tab-size&lt;/a&gt; to &lt;code&gt;2&lt;/code&gt;, you can imagine what will happen…:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6NLp2fUd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vc5h7inzcjzb0e1revrg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6NLp2fUd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vc5h7inzcjzb0e1revrg.png" alt="Tabs with 2 width"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;“Bye bye indentation alignment… adiós indentation alignment…”&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This will be the case not only for web displaying, but &lt;strong&gt;for even custom editor tab sizes&lt;/strong&gt;. As you can see, we’ve lost the indentation alignment we though we were having using tabs with tab size set to &lt;code&gt;4&lt;/code&gt;, and this will be the case for even custom tab size into editors, it will break every alignment; and even for presenting on the web, you may argue that we won’t set the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/tab-size"&gt;tab-size&lt;/a&gt; to &lt;code&gt;2&lt;/code&gt;, we’ll set it to &lt;code&gt;4&lt;/code&gt; that we know it will work with our code, but guess what… &lt;strong&gt;code sharing platforms like Github&lt;/strong&gt; don’t &lt;em&gt;directly care&lt;/em&gt; about tab widths and these are having &lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/tab-size"&gt;tab-size&lt;/a&gt; CSS property being the value of each page the code is being displayed, with of course the default initial value of &lt;strong&gt;&lt;code&gt;8&lt;/code&gt;&lt;/strong&gt; (&lt;strong&gt;&lt;em&gt;for dev.to the &lt;code&gt;tab-size&lt;/code&gt; is set to &lt;code&gt;2&lt;/code&gt;&lt;/em&gt;&lt;/strong&gt;) and our code will look like this:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Yes, this is a Gist and it lets you change the tab width to &lt;code&gt;2&lt;/code&gt;, &lt;code&gt;4&lt;/code&gt; or &lt;code&gt;8&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--648J_eV2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r9mizsl4ny5z6p3k49v5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--648J_eV2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r9mizsl4ny5z6p3k49v5.png" alt="Gist tab width"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And even Github repositories will respect an &lt;a href="https://editorconfig.org/"&gt;&lt;code&gt;.editorconfig&lt;/code&gt;&lt;/a&gt; file with &lt;code&gt;indent_style = tab&lt;/code&gt; and &lt;code&gt;indent_size = 4&lt;/code&gt;, but why bother with all of these? To let users display the width they like? Well even though custom tab widths will break alignment, that’s a reason and &lt;strong&gt;the only reason&lt;/strong&gt; in my opinion and the fact is that this will be an issue for both editing code with custom tab widths and sharing code with other people. You can read more about Github and resolving Tab indentation issues here &lt;a href="https://stackoverflow.com/questions/8833953/how-to-change-tab-size-on-github"&gt;How to change tab size on GitHub?&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Maybe in the future there will be a wide usage of configurations like &lt;a href="https://editorconfig.org/"&gt;&lt;code&gt;.editorconfig&lt;/code&gt;&lt;/a&gt; (&lt;em&gt;which I doubt&lt;/em&gt;), but until then, people should &lt;strong&gt;use Spaces for indentation and alignment&lt;/strong&gt;, at least for code/projects they’re going to share in public.&lt;/p&gt;

</description>
      <category>coding</category>
      <category>editors</category>
      <category>tabs</category>
      <category>spaces</category>
    </item>
  </channel>
</rss>
