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?
cheers
simon
Saturday, 26th November, 2011 at 9:39 pm |
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).
Saturday, 26th November, 2011 at 10:39 pm |
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
Sunday, 27th November, 2011 at 2:18 pm |
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.
Sunday, 27th November, 2011 at 4:19 pm |
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.
Monday, 28th November, 2011 at 4:48 pm |
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
ErrHandle:
Hide everything under the rug
End Sub
Monday, 28th November, 2011 at 8:02 pm |
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.
Monday, 28th November, 2011 at 8:18 pm |
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.
Tuesday, 29th November, 2011 at 3:58 am |
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).
Wednesday, 30th November, 2011 at 9:37 am |
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…
Wednesday, 30th November, 2011 at 10:13 am |
What about morons that add line-spacing (me!) :)
Friday, 3rd February, 2012 at 5:22 am |
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..