I was reviewing someone elses code recently and I noticed they had used what I consider excessive line spacing.

Public Sub RunAllThree()

Dim objWorksheet  As Worksheet

Dim intResponse   As Integer

I will henceforth call this scardeycat spacing and use it as a derogatory term. Leaving aside for a minute the silly name prefixes, what about all that white space?

In brace languages I tend to to open and close on separate lines which breaks things up anyway without need for additional spacing.

I use the odd blank line in my VBA to give eyes a break before or after some intense code, or to give extra emphasis to something.

But like all formatting, if you overdo it and emphasise everything then you emphasise nothing. The reality is that programming code is relatively information dense, adding white space can make it less scary up to a point, and after that you are just forcing the reader to scroll more than they should need to.

What is your approach to spacing?



11 Responses to “Scare-D-cat”

  1. Harlan Grove Says:

    I always have a blank line between Const, Static and Dim declarations and between Dim and code. I begin and end any block (loop, If or Select) with 10 or more lines with a blank line. For dense code, like much numerical programming, I even add the opening block line as a comment to the ending block line, e.g.,

    If foo = bar
    ‘much code
    End If ‘ foo = bar

    My pet coding peeve is gratuitous brackets, e.g., ((((a * b) * c) * d) * e).

  2. Bob Phillips Says:

    I often feel people underdo the white space lines.I don’t think a line after every code line is required, but we do need some breaks.

    I like to break all seprate functional groups with a whie space line. The other thing I tend to do, which I admit is propbably to the taste of few others, is to add a white space line after all pair constructs. For instance

    Some code

    With Worksheets(“Sheet1”)

    For i = 1 To 10

    If something = something Then

    Do Stuff
    Do More Stuff
    End If
    Next i
    End With

    More code

  3. Steve Says:

    Always err on the side of generosity. It looks neater on screen, and it’s easier to squeeze later if you want to print it out than it is go through code and insert extra line spacing to make something readable.
    I agree with Bob’s approach, and use it to break procedural code into functional sections (though I’d quibble that his example ought to have nested indents for the With, If and For sections)

    In a professional environment code commenting and layout is not just for the benefit of the developer or other programmers.

    • Bob Phillips Says:

      Steve, these blogs have a tendency to strip indenting, so I didn’t bother trying as that wasn’t the point I was making. But, I absolutely agree with you on the principle of indenting.

  4. Oli Says:

    I am sporadic when coding but generally group similar function code eg:

    Sub Example()

    Const BLAH_BLAH as String = “Something”
    Const BLAH_BLAH_BLAH as String = “Something Else”

    Dim I as Integer

    On Error GoTo ErrHandle:

    With object
    .property = ???
    .property2 = ???
    End With

    For I = 1 to 10000000
    […] more code
    Next i

    […] more code

    Exit Sub

    Hide everything under the rug

    End Sub

  5. JP Says:

    If you use the Smart Indenter addin for the Office VBE, it can/will do something similar to this for you. See the “Align Dim’s in column” option.

  6. Pob Says:

    I’m with JP – since I use the same add-in – so maybe that just makes me lazy, but being lazy is what makes me a programmer…so that’s okay, then.

  7. dougaj4 Says:

    I’m fairly free with my blank lines, but a line between every Dim statement is definitely over-doing it, in fact I don’t see the benefit of putting every dim on a new line (or of putting every function argument on a new line).

    It seems to me more readable and more efficient to keep as much information visible within a given window as possible, which means using the full screen width for both function arguments and variable dim statements (which also means the smart-indent “Align Dim’s in column” option does nothing for me, but that’s OK).

  8. fysicusje Says:

    I use line spacing as an integral part of my code layout, for example between 2 seperate steps within an algorythm.

    If there is 1 thing I hate is seeing other morrons remove line spaces where I added them…

  9. Stephen Allen Says:

    Just read this. I was going to add real code to my presentation at DevCon. – 25 Jan 2012

    I’ve always known it was badly laid out. I’m so glad I didn”t..

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: