5

As a follow-up on Is it mandatory to escape tabulator characters in C and C++? (do note I'm not the author of said question).

I've learned such code is considered "bad practice". The comments seem to be suggesting the same thing. However, for some reason the standard allows this trickery so somebody must either found no harm in it or has a use-case for it.

Is not escaping tabulator characters widely accepted as "bad practice"?

Community
  • 1
  • 1
Mast
  • 1,788
  • 4
  • 29
  • 46
  • Probably no one has done a survey, it has long been poor practice, if for no other reason than that it is too easy to break code by accidentally converting tabs to spaces. I flag those in my code-counter http://invisible-island.net/c_count/c_count.html – Thomas Dickey Mar 07 '15 at 16:26
  • This seems like a subjective question to me! The original question was good because it asked for solid facts. – Lightness Races in Orbit Mar 07 '15 at 16:28
  • 6
    C in general doesn't try to protect programmers from doing dumb things. It's easier to allow this than to forbid it and the assumption is that the programmer knows what they're doing. "Doctor, it hurts when I do this. - Don't do that." – CodesInChaos Mar 07 '15 at 16:29
  • I'd consider it bad practice because it's easy to mistake for a couple of spaces. – Emil Laine Mar 07 '15 at 16:29
  • If the `\t` is a character then why would it be bad or invalid? – Iharob Al Asimi Mar 07 '15 at 16:30
  • 1
    @CodesInChaos that's why I am always afraid of programmers who only know python or C#. – Iharob Al Asimi Mar 07 '15 at 16:30
  • 1
    @iharob: The question is a bit confusing because the original question had an inverse title, but I think he's saying that _not_ escaping tabular characters is bad practice. – Lightness Races in Orbit Mar 07 '15 at 16:33
  • @iharob That philosophy is why I'm afraid of writing C and C++. Optimizing compilers prey on your tiniest mistakes and can wreck your whole program when they find one. – CodesInChaos Mar 07 '15 at 16:36
  • @iharob I'm not afraid of compilation failing, I'm afraid of the compiler finding some subtle case of UB. For example ensuring a program is free of integer overflows is a lot of effort. Or some out of bounds array access in a case that didn't test (so even valgrind doesn't catch it) leading to a remote code execution vulnerability. Writing C and C++ requires being a programmer than I am. – CodesInChaos Mar 07 '15 at 16:45
  • I realized that now and deleted the comment. – Iharob Al Asimi Mar 07 '15 at 16:46
  • @CodesInChaos: Actually both are pretty easy if you write idiomatic C++ – Lightness Races in Orbit Mar 07 '15 at 17:02

2 Answers2

6

Using tabs in litterals instead of escaping them ('\t',"\t") is a bad practice because :

  • the reader is not immediately aware that there is a tab. Consequence: wrong assumptions about code, wrong changes (for example when allignemnt of code output needs to be adapted)
  • the tab expansion might be different depending on your editor. Consequence: hindering of teamwork and maintenance, as different people may see different layouts.
  • MOST OF ALL: some editors convert tabs to spaces when saving your source file (using the configured tab expansion settings) getting rid of such embedded tabs. Consequence: unnoticed/undesired change (f.ex: when a teammember uses such an editor and make a minor edit, not even in this string).
Christophe
  • 68,716
  • 7
  • 72
  • 138
  • 1
    @zenith The second point makes it a bad practice, because it hinders teamwork and maintenance. Example: Programmer A embeds tab in the string using tab stops of 8. Programmer B has to maintain the code, opens it with tab stops of 4, makes some changes that shift the place of the tab in the line. Output seems sunddenly to be misaligned, so he'll add another tab, etc... – Christophe Mar 07 '15 at 16:43
  • @Christophe: That's quite an incompetent developer if he's arbitrarily adding tabspaces to a string literal for a bad reason like that! Of course, such incompetent developers do exist. – Lightness Races in Orbit Mar 07 '15 at 17:01
  • 3
    @LightnessRacesinOrbit Taht's it: the world isn't perfect !... And I wouldn't blame such a guy: the tab-expansion feature that many professional editors offer, demonstrate that most people assume that tabs are used in code for indenting, and not as litterals. Nobody should go against such popular thoughts ;-) – Christophe Mar 07 '15 at 17:09
  • 1
    @Christophe: Even _that_ assumption is broken, since auto-converting tab indentation to evil space indentation is the devil's work. – Lightness Races in Orbit Mar 08 '15 at 14:15
3

I've never really thought about it, but I can't imagine finding literal tabspace characters a good idea because you cannot immediately distinguish them from standard whitespace. If you need a tabspace in your string literal for some specific reason, it's more clear and explicit to write \t so that everybody knows precisely what you intended to do.

By the way, the notion that there must be a good use case just because the standard allows it … is somewhat broken. For example, the standard allows us to declare raw pointers, and to write new.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • 1
    Y'know what? [This answer was already given on the original question](http://stackoverflow.com/a/28901052/560648). I'm not really sure I understand why you had to re-ask it. – Lightness Races in Orbit Mar 07 '15 at 16:34
  • 1
    Simple. I'm not an expert and the comments seem to say the compiler will take care of it anyway. When in doubt, ask. That's how Q&A works, right? – Mast Mar 07 '15 at 16:41
  • @Mast: The compiler will "take care of" what? – Lightness Races in Orbit Mar 07 '15 at 17:00
  • 1
    Lightness, the prior research is my experience with both languages. I can understand why it shouldn't be done, but I've seen things in the past which I couldn't agree to be a good practice. So, instead of accepting comments on a related question as an answer, I posted my own question. To prevent mistakenly accepting wrong statements. – Mast Mar 07 '15 at 17:14