(White) Space, the final frontier

In this second post about my preferred coding style, I’ll talk about white space.

White space is very important to improve readability, not only when dealing with code. For instance, when you write an email you usually break it up in several paragraphs, instead of writing a single chunk of code (or if you don’t, you should!).

When writing a piece of software, white space should be used to isolate code blocks logically and to enhance the visibility of the most important parts of the code (e.g. arguments, operators, operands…).

I often see people writing code like this:

1
2
3
4
5
6
7
8
9
<span style="color: blue">void</span> <span style="color: teal">foo</span>() {
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue">string</span> bar = "a";
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue">string</span> row = "";
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue">foreach</span> (ObjectType item <span style="color: blue">in</span> collection) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Response.Write("zot!");
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AnotherMethodCall();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;abc = dtMinData;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;row = i%252==0 ? "even" : "odd";
// ...

It’s not very pleasant to the eyes (and I’ve seen worse than that!). Let’s pimp it up!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<span style="color: red">1.</span>&nbsp;&nbsp;void <span style="color: teal">foo</span>()
<span style="color: red">2.</span>&nbsp;&nbsp;{
<span style="color: red">3.</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue">string</span> bar = "a";
<span style="color: red">4.</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue">string</span> row = "";
<span style="color: red">5.</span>&nbsp;&nbsp;
<span style="color: red">6.</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue">foreach</span>( ObjectType item <span style="color: blue">in</span> collection ) {
<span style="color: red">7.</span>&nbsp;&nbsp;
<span style="color: red">8.</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Response.Write( "zot!" );
<span style="color: red">9.</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AnotherMethodCall();
<span style="color: red">10.</span>&nbsp;&nbsp;
<span style="color: red">11.</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;abc = dtMinData;
<span style="color: red">12.</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;row = (( i %25 2 ) == 0 ) ? "even" : "odd";
<span style="color: red">13.</span>&nbsp;&nbsp;
<span style="color: red">14.</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// ...

Some notes:

  • line 1-2: as I’ve said in my previous post, I hate the java-style 🙂 Line 2 helps to separate the method name from the first line of code, so they’re both easier to see.
  • line 5 is empty, to mark the end of the variable declaration block and the beginning of a statements block.
  • line 6: notice the spaces inside the parenthesis, to increase the readability of the content of the foreach(). There is no space between foreach and the opening parenthesis, for two reasons: 1. the parenthesis is part of the instruction, not of its arguments 2. syntax highlighting makes the foreach keyword already enough visible.
  • line 10 is empty, stressing the passage from a series of method invokations to a block of assignments.
  • line 12: what?! no whitespace around operators? Also, even if the language in this case doesn’t require them, I prefer to use parenthesis to explicate the structure of the expression.

Leave a Reply

Your email address will not be published. Required fields are marked *