Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
313 views
in Technique[技术] by (71.8m points)

c++ - Why is indentation in empty lines bad?

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.

question from:https://stackoverflow.com/questions/5917956/why-is-indentation-in-empty-lines-bad

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...