Whitespace Is Good

Whitespace is good!

Rationale:

Humans use contrast to distinguish objects from each other. Whitespace is very useful in providing contrast between statement blocks, thus making readers assume there are logical breaks between and logical connections within these blocks. Thus, use whitespace to effectively break your code into "paragraphs".

Why do you think paragraphs are formed the way they are?

Example:

A fairly simple example:

 CString &CString::TrimLeft( void )
 {
	assert( !m_iLockCount );
	assert( m_szBuffer );

char *sz = m_szBuffer; while( *sz && isspace(*sz) ) sz++;

return operator =(sz); }

Exceptions:

After you've white-spaced your code, consider whether any of the groups of lines are good candidates to be in their own function. A group of lines in its own function tell the user that the lines belong together, just like white space does. But the name of the function also tells the user why the lines belong together.

Many people squish inline functions in C++ onto one line if they are very short. This rationale is that this increases the readability of the class declaration because it keeps the focus on the declaration and not on the definition (implementation). However, consider whether you should pull the bodies of your inline definitions out of the class declaration altogether.

Another exception are languages like JavaScript that need to be loaded over a network, where every byte makes the user wait a little longer. However, consider using a JavaScript compression tool, so that you can have your whitespace without making the user wait longer.

Arguments:

"The program runs faster without whitespaces! Wheeee!"

Only in interpreted languages. However, this performance hit is negligible.

"The program compiles faster without whitespaces!"

True enough, but who cares? Modern computers parse out whitespaces in microsecond granularity. A compiler probably spends less than 0.1% of its time on whitespaces. In C++, templates; large, complicated header file trees; optimizations; disk I/O etc. consume the CPU. Unless you're stuck on the (very cool) PDP series of computers still, move into the present.

Another point is that if whitespace costs, so do comments - and nobody makes a serious claim that comments are bad. So if you use comments, you can use whitespace -- Pete Hardie

"Whitespaces push the code past the one printable page limit for a method."

Your one-page limit is an artificial constraint on complexity. Complexity is not a function of method length, but a subjective function of comprehension. Whitespaces increase comprehension by reducing the amount of information you have to digest at once. Coupled with block comments, you're set. "Whitespace is sometimes inferior to breaking code out into separate functions, where the name of the function says why the code is in its own function. With white space, the user has to guess why the code is grouped together."

True enough. But sometimes separate functions are just too heavyweight.

I'd amend the above to say "Whitespace is often inferior to breaking code out into separate functions..." And I've seen many examples when people use whitespace as a crutch to avoid doing the work of breaking out things into functions. So while I certainly wouldn't say WhitespaceIsBad?, I would say WhitespaceIsNotEnough?.

I'll extend the above and say refactoring greatly reduces the importance of whitespace. I still prefer whitespace in certain areas, but once the methods are shortened up and operating at a single level of abstraction, the additional "readability" provided by whitespace is negligible.

Hmm... I recall needing to make a minor edit to a program which contained numerous groups of completely blank lines. I replaced each such group by just one blank line, making the program several pages shorter. I call that normal tidying. -- VickiKerr


I HaveThisPattern so strongly that if I refactor a large method into, say, 5 smaller methods plus the original method containing 5 method calls, I'll end up retaining the original "paragraph" structure in the original method - in other words, a 9-line method consisting of 4 blank and 5 non-blank lines. DoubleSpacedMethod?, maybe? -- ZygoBlaxell


I find that the need for whitespace is reduced by SyntaxColouring?.
nobody makes a serious claim that comments are bad

Sure they do. See TreatCommentsWithSuspicion.

See: SelfDocumentingCode / CodeFormatting

EditText of this page (last edited July 2, 2010) or FindPage with title or text search