White Space in Code

Does white space matter?

You bet it does.

Is it easy to define the rules for something as simple as when you should and should not insert a blank line?

No. I’ve never written or read a concise and unambiguous declaration of what should be a simple style tip.

Let’s go back to my assertion that white space matters. Does it? Why does it?

It matters because when you write code you aren’t writing your shopping list. You’re not writing for the compiler. You’re not writing instructions for a machine. You are writing for an audience. You are writing code that will be read by another coder and it is your job to express yourself clearly.

Express your intent. Express the nature of the problem that you are trying to solve. Express the forces compelling you to choose one solution above any other. Explain the details of your solution in a way that the reader will be able to follow without having to unpick your work. Don’t make the reader have to go back to the very beginning and rebuild the same understanding that you have achieved, following all the same reasoning you had to work through.

You are writing to explain yourself to a reader.

Writers of fiction understand how to present their words in ways that make it easy for the reader to tune into their ideas. Along with all the rules of grammar and the conventions of expression and idiomatic speech that are quite different in English compared to Java, there are also widely followed rules of style that apply equally to computer languages as they do to creative prose.

Here’s one rule that relates very closely to the use of white space in code: The paragraph.

Stephen King writes about the use of paragraphs in his book “On Writing”. Think about the way you layout code as you read this.

Before leaving the basic elements of form and style, we ought to think for a moment about the paragraph, the form of organization which comes after the sentence. To that end grab a novel… Open the book in the middle and look at any two pages. Observe the pattern – the lines of type, the margins, and most particularly the blocks of white space where paragraphs begin or leave off.

 

You can tell without even reading if the book you’ve chosen is apt to be easy or hard, right? Easy books contain lots of short paragraphs and lots of white space. Hard books, ones full of ideas, narration, or description, have a stouter look. A packed look. Paragraphs are almost as important for how they look as for what they say; they are maps of intent.

 

In expository prose, paragraphs can (and should) be neat and utilitarian. The ideal expository graf contains a topic sentence followed by others which explain or amplify the first.

 

Even in the informal essay it’s possible to see how strong the basic paragraph form can be. Topic-sentence-followed-by-support-and-description insists that the writer organize his thoughts.

 

Writing is refined thinking. If your thesis is no more organized than a high-school essay, you’re in big trouble.

Here’s what the author’s bible, Strunk and White’s ‘The Elements of Style’, has to say:

Ordinarily a subject requires division into topics, each of which should be dealt with in a paragraph. The object of treating each topic in a paragraph by itself is, of course, to aid the reader. The beginning of each paragraph is a signal that a new step in the development of the subject has been reached.

 

In general, remember that paragraphing calls for a good eye as well as a logical mind. Enormous blocks of print look formidable to readers, who are often reluctant to tackle them. Therefore, breaking long paragraphs in two, even if it is not necessary to do so for sense, meaning, or logical development, is often a visual help. But remember, too, that firing off many short paragraphs in quick succession can be distracting.

When you look at densely packed code, do you instinctively know you’re in for a rough time?

Can you tell just by the shape of the code in a method or a class that you’re going to have to re-write it, before you even read any of the individual words?

Do you take care to break up related blocks of code like loops and return statements by inserting blank lines at carefully chose points?

Do you notice deviations from symmetry in neighbouring lines of code and re-organise your method parameters and return types to achieve symmetry and a consistent level of abstraction?

If you don’t, then I urge you to read Clean Code and to watch the Clean Coders videos.

Finally, I urge any programmer who wants to write clean code to read Strunk and White. People have been writing, and writing about writing, for a very long time, and some important lessons about writing well can quickly be learned and applied when writing code.

Advertisements
Posted in Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: