Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it "bad practice" to use tab characters in string literals?

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"?

like image 308
Mast Avatar asked Mar 07 '15 16:03

Mast


People also ask

Are string literals bad?

While there is nothing inherently wrong with the use of string literals, there is room for improvement. String literals very often lead to stringly typed code, a play on strongly typed.

Is it possible to modify a string literal?

The behavior is undefined if a program attempts to modify any portion of a string literal. Modifying a string literal frequently results in an access violation because string literals are typically stored in read-only memory.

What is the difference between character literals and string literals?

Character literals represents alphabets (both cases), numbers (0 to 9), special characters (@, ?, & etc.) and escape sequences like \n, \b etc. Whereas, the String literal represents objects of String class.

What characters surround a string literal?

You can include predefined String values within your code as string literals. A string literal is a sequence of characters surrounded by double quotation marks ( " ).


2 Answers

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).
like image 136
Christophe Avatar answered Sep 22 '22 11:09

Christophe


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.

like image 31
Lightness Races in Orbit Avatar answered Sep 20 '22 11:09

Lightness Races in Orbit