Search code examples
language-agnosticwhitespaceliterate-programming

Use of Vertical Whitespace


My intention in this question is not to be pedantic, but rather to explore an overlooked axis of an important topic (the use of whitespace). Much debate and care has been put into the use of horizontal whitespace, indenting after a conditional, a space between an if and parenthesis, etc. In fact the issue is considered to be so important and contentious that, not only do some companies have rules and standards for it, but some companies even have rules forbidding it's discussion.

Why is it, considering the state of horizontal whitespace, that the discussion of vertical whitespace is such a dead issue? Why is the x more important than the y? A few days ago I noticed that when I reading code, I, without thinking, often adjust vertical groupings of statements. Having read other peoples code now, with an eye toward vertical whitespace, I noticed several patterns, so I ask stackoverflow:

  • What hard and soft rules do you apply to vertical whitespace?
  • Are there any vertical whitespace uses that are generally considered very bad, or very good practice?
  • Have you found reading code with "correct" vertical whitespace helped in understanding it?
  • Does anyone other than typographers and me care?

Solution

  • I look at vertical space in code the way I look at paragraphs in written prose. Just as a paragraph is meant to group sentences together that have a common point or idea, lines that are related should be grouped together.

    The overall objective is to improve the readability of the code. Just as an article without any paragraphs would be difficult to read, so to is code without any vertical space. And just as with prose, there is a balance between composing paragraphs that are too short or too long. But in the end, it mostly comes down to personal style and preference.