⬆️ ⬇️

Time to tie tabs to code

unicorn and tabs



Many may find the argument that gaps or tabs in the code for holy wars are better. But no, I do not want to have a discussion on this topic. I unequivocally state that spaces should be used without fail. And the conversation and the "preference of this or that" is not appropriate here. As it is not appropriate in our time to discuss what is more convenient, a computer or a typewriter. Since typewriters have ended their existence, it is at least irrational to rely further on their use. And if you still consider how convenient it is to use a computer for typing, the question of choice simply disappears.





With spaces and tabs, the situation is not so grand and ambitious, but the analogy can be traced. Next, I will explain why it is rational to switch to spaces for formatting code.





This note prompted me to write what tabs I was tired of. It seems like a trifle. But years go by, and this infection does not go away for programmers. Although I myself do not use tabs when writing code, I nevertheless regularly recall the unkind word of their fans. Do not worry, this does not disturb my sleep and appetite, but I spend some time in vain. And if you consider how many more people are confronted with similar annoying moments, then the total harm is very tangible. And if spammers benefit from spam, no one gets the benefits from using tabs. Energy goes nowhere. This one is like a dripping faucet. One drop that a programmer spends is not significant. But the amount has already flowed so much and continues to flow away, horror. I propose to read this article and, finally, to stop this leak!





In the beginning, I will list the arguments that recall when they do not want to go from tabulation to spaces. I am sure that they are sucked from the finger and the only real reason is laziness. Nevertheless, I will consider these arguments and criticize these.



')

1) The size of the files with the source code is less.





2) Replacing all spaces may result in loss of revision history. It will not be possible to compare the old and the new.





3) By changing the length of the tab, I can easily adjust the indents in the program to fit my preference.





Let us criticize pure reason.





1) The size of the files with the source code is less





As far as this is true, so is the stupid argument. In our time, saving on kilobytes is simply meaningless. This argument has lost all meaning over 10 years ago.





2) Replacing all spaces may result in loss of revision history. It will not be possible to compare the old and the new.





Formally, too, that's right. But perhaps this is too dark a picture. Do you often have to investigate the history of a file to a greater depth? Usually no one looks into the story deeper than one or two revisions of the file. Replacing tabs with spaces is done globally and once and should not interfere with further comparisons.





If you need to frequently and deeply compare files, then this is something wrong in the conservatory . And there is no place for spaces / tabs. And if a normal programmer has to study the history of one file up to the 10th generation in a normal project once a year, then WinMerge will help him. He knows how to replace tabs with a space for the difference is not considered. I think other comparison systems are not worse.





In general, also not an argument. And if the argument, then it is very weak.





3) By changing the length of the tab, I can easily adjust the indents in the program to fit my preference.





Never works in practice.





Anyone who says something like that does not understand what he is talking about. And he himself has never tried to do this, but he would not have said nonsense.





I will make a small digression. In fact, there is a sense in the statement of a grain. Here are just torn out of context, it is stupid. This can only work where the code is tabbed ONLY. Spaces for alignment are NOT USED AT ALL. In this case, yes, I am the size of the tabulation, we can change the display code. For example, one likes to look at such a code (one tab = 2 spaces):





  if (A == B)
     x = y; 


The other uses a different size and admires the same code in his favorite style (one tab = 4 spaces):





  if (A == B)
         x = y; 


But nobody does that. Follow your code and you will see something more realistic (one tab = 2 spaces):





  A = Aaaaaaaaaaaa &&
     Bbbbbbbbb; 


If now the tab becomes not equal to two spaces, then all the beauty of the design will be instantly broken.





  A = Aaaaaaaaaaaa &&
         Bbbbbbbbb; 


Or, I took and looked at someone's file. Someone tried to level:





  wcex.cbSize = sizeof (WNDCLASSEX);
 wcex.style = WS_OVERLAPPED;
 wcex.lpfnWndProc = (WNDPROC) ClipBoardWndProc;
 wcex.cbClsExtra = 0;
 wcex.cbWndExtra = 0;
 wcex.hInstance = theApp.m_hInstance;
 wcex.hIcon = 0;
 wcex.hCursor = :: LoadCursor (NULL, IDC_ARROW);
 wcex.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1); 


That's just me, in FAR with a different tab size, it looks like this:





  wcex.cbSize = sizeof (WNDCLASSEX);
 wcex.style = WS_OVERLAPPED;
 wcex.lpfnWndProc = (WNDPROC) ClipBoardWndProc;
 wcex.cbClsExtra = 0;
 wcex.cbWndExtra = 0;
 wcex.hInstance = theApp.m_hInstance;
 wcex.hIcon = 0;
 wcex.hCursor = :: LoadCursor (NULL, IDC_ARROW);
 wcex.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1); 


You can give a lot of such examples. You can change the tab size yourself and admire your code. This is especially useful if you want to challenge this text.





So, the code after changing the tab size will normally look if you use only tabs. But it is very austere. And the code is often not beautiful. Therefore, the vast majority of programmers use gaps in addition to tabs. And thereby destroying the ability to change the size of the unfortunate tabs.





As a result, we see that using tabs for abstract reasons does not make sense. I claim that by taking your code or someone else’s code and changing the size of the tabulation, in 99% of cases we will get an ugly, parted code. And if so, then the argument in defense is valid only by 1%, or even less. I have never seen the code look good with other tabs. And I watch a lot of code.





With tab defense, I’ve finished. Now I list the disadvantages of their use.





1) It is difficult to work with projects that use libraries that contain tabulation in the text. Suppose a tab in one library is 3 characters, in another 4 characters. And you in the project use 2 characters. As a result, some part of the code you will have displayed in the editor with broken formatting.





So where are the benefits of variable tab sizes? What size do not choose, yes somewhere will be crooked. If there are spaces everywhere, then everything will look beautiful. Yes, there will be different indents, but the formatting will remain beautiful everywhere!





2) It is easy to let goat in the garden. Let's say you have a tab equal to 4 spaces. Someone straightened something a bit using a different tab size or by explicitly inserting spaces. Everything looked normal with him, and your line of code would go somewhere.





3) You need to constantly adjust various editors to the tab size you need. Even if you just need to look at the code wrongly. Otherwise, everyone is leaving. This is especially not convenient when you have to do something with your code on a third-party machine. Or vice versa to look into someone else's code. It is difficult to explain all the fuss. It must be felt. Programmers are probably divided into those who know and understand about this inconvenience, and who does not. But please, those who do not understand, believe me - these inconveniences are there! And if, in general, you don't care, then use spaces! Save someone a little time and effort.





4) The extra complexity of those who work simultaneously with projects where, according to coding standards, different indents are required. If standards require the use of tabs, then it is that eternally aching tooth. In the case of spaces, again, everything is much simpler.





And lastly I will answer: “we always wrote this, it means it’s worth to continue like that”. Often, moreover, this is supported by some bearded coding standard used by programmers. Thank God, professional developers are not standing still. And they are not afraid to break the old foundations. An example of this is Google C ++ Style Guide . Here is a quote from there about the spaces and tabs.





Spaces vs. Tabs





Use only spaces, and indent 2 spaces at a time.





We use spaces for indentation. Do not use tabs in your code. When you hit the tab key.





Thank God! Though someone has a reasonable approach!





I wish you freshness in your coding standards!





UPDATE:





NOTE FOR THOSE WHO ARE IN THE TANK





To use spaces for decoration, it is not necessary to press the spacebar 4 times or more. In all editors, it is possible to customize how many spaces to insert when tapping a tab.





I never would have thought that because of this, they would put me in mind. “That's a bastard! Recommends spaces to press until blue in the face, instead of cute taba! ”

Source: https://habr.com/ru/post/118204/



All Articles