为什么空行缩进不好? [英] Why is indentation in empty lines bad?
问题描述
我知道的每个FOSS项目都有在代码中的尾部空格的规则。但我认为继续当前缩进在下一行是非常自然的:
int main()
{
... int a = 42;
....
.... return a;
}
但是git实例会抛出警告。所以我的问题是:为什么这些标签 在当前缩进不好?
我不是在寻找答案办法。
这可能是因为合并具有无用空格的补丁比它应该更难。
diff(1)
和 patch(1)
将空格和制表符视为重要内容。 (询问任何 Makefile
或 .py
源文件 - 它们是重要的!如果空白行上有四个空格,并且我的空白行有八个空格,任何尝试在我们之间共享补丁将失败,原因。
授予,如果你批量改变一段代码的缩进,你必须去一些工作,使补丁应用 em>。但试图在看起来空白处追踪合并失败是痛苦。 (我已经浪费了太多的我的生活做到这一点。是的, vim
listchars
可以帮助,
Every FOSS project I know has rules against trailing whitespace in code. But I think it's very natural to continue the current indentation on the next line:
int main()
{
....int a = 42;
....
....return a;
}
But git for instance throws warnings anyway. So my question is: Why are those tabs inside the current indentation bad?
I'm not looking for answers like "It's always done this way". Let's assume indentation is done consistently in the whole project in question.
It is probably because merging patches with useless whitespace is harder than it should be.
diff(1)
and patch(1)
treat spaces and tabs as important content. (Ask any Makefile
or .py
source file -- they are important!) And if your "blank line" has four spaces on it, and my "blank line" has eight spaces on it, any attempt to share patches between us will fail for very trivial reasons.
Granted, if you wholesale change the indentation of a block of code, you'll have to go to some work to make patches apply anyway. But trying to track down merge failures on lines that look blank is painful. (I've wasted too much of my life doing just that. Yes, vim
listchars
can help, but reading code with listchars
on all the time is also annoying.)
So people standardize on no trailing whitespace. It might not really make sense to worry about a dozen lost bytes here or there from a storage standpoint, but it really makes merging patches easier. We could probably just as well standardize on adding trailing whitespace, exactly as you have suggested, and be just as happy, but we might as well standardize on the approach that is as parsimonious as possible.
这篇关于为什么空行缩进不好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!