本文介绍了是“坏做法”在字符串文字中使用制表符字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
作为(请注意,我不是所述问题的作者)。
我学到了这样的代码被认为是不良做法。这些评论似乎暗示着同样的事情。但是,由于某种原因,标准允许这个骗子,所以有人必须发现没有伤害或有一个用例。
不逃避的制表符字符被广泛接受\t使用tab选项卡而不是转义它们('\t '
,\t)是一种不好的做法,因为:
- 有一个标签。结果:错误的代码假设,错误的更改(例如,当代码输出需要修改时)
- 根据编辑器的不同,选项卡扩展可能会有所不同。结果:阻碍了团队合作和维护,因为不同的人可能看到不同的布局。
- 最多:一些编辑者在保存源文件(使用配置的选项卡扩展设置)时删除这些嵌入的选项卡将标签转换为空格。结果:不注意/不需要的更改(f.ex:当一个团队成员使用这样的编辑器,并进行一个小编辑,甚至在这个字符串)。
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"?
解决方案
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).
这篇关于是“坏做法”在字符串文字中使用制表符字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!