Code Formatting - Its important people!
Posted: 2002-06-01
By: ArchiveBot
Viewed: 67
Filed Under:
No attachments for this post
I aim to try and get more people to make their code more readable. More readable code means that the code is percieved better and better perception of the code might even to a better vote. Also, it makes everybody else alot happier if thay can actually read your code.
Original Author: Coding Genius
Code
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:w="urn:schemas-microsoft-com:office:word"
xmlns="http://www.w3.org/TR/REC-html40">
Contents:
Introductionstyle='font-size:14.0pt;mso-bidi-font-size:12.0pt'>
First of all, what do I actually want to do here?
Well, this is not an easy question to answer. All too often I see badly written
code. It might be the best code ever written but if it is unreadable then it is
considered (by me anyway) as being bad code. Code can be formatted in different
ways. It is up to you. I will be showing you all of my conventions but these
are not the only ways to do it. Your way might be right as well.
style='font-size:10.0pt;mso-bidi-font-size:12.0pt'>The first thing I always
notice in a piece of code is how well it is indented. For example:
Private
Function Example (ByVal
ExampleVar as long)
If
ExampleVar > 0 then
Select
Case ExampleVar
Case
1:
Call
DoThis (1)
Case
2:
Call
DoThis (2)
End style='color:blue'>select
Elseif
ExampleVar < 0 then
Case
-1:
Call
DoThis (-1)
Case
–2
Call
DoThis (-2)
End style='color:blue'>Select
End style='color:blue'>if
End style='color:blue'>Function
This
is not indented at all. If the code is long and complex with long lines then it
looks like an English essay or something similar. This I believe is much more
readable:
Private
Function Example (ByVal
ExampleVar as long)
If
ExampleVar > 0 then
Select Case ExampleVar
Case 1:
style='font-size:8.0pt;mso-bidi-font-size:12.0pt;color:blue'>Callstyle='font-size:8.0pt;mso-bidi-font-size:12.0pt'> DoThis (1)
Case 2:
style='font-size:8.0pt;mso-bidi-font-size:12.0pt;color:blue'>Callstyle='font-size:8.0pt;mso-bidi-font-size:12.0pt'> DoThis (2)
End select
Elseif
ExampleVar < 0 then
Select case ExampleVar
Case -1:
style='font-size:8.0pt;mso-bidi-font-size:12.0pt;color:blue'>Callstyle='font-size:8.0pt;mso-bidi-font-size:12.0pt'> DoThisstyle="mso-spacerun: yes"> (-1)
Case –2
style='font-size:8.0pt;mso-bidi-font-size:12.0pt;color:blue'>Callstyle='font-size:8.0pt;mso-bidi-font-size:12.0pt'> DoThisstyle="mso-spacerun: yes"> (-2)
End Select
End style='color:blue'>if
End style='color:blue'>Function
The
rules I use for indentation are:
For
every new block you write, on the next line press tab.
I.E.
A block is defined as a “With statement”, “If statement”, ”Loop”, “Select case
statement”, “Type structure”, “Enumeration structure”, E.T.C
Basically
anything with a beginning, then some code, then an end.
E.g.
With
This
style='mso-tab-count:1'> If
This then
style='mso-tab-count:2'> style='color:blue'>Select case This
style='mso-tab-count:2'> style='color:blue'>Case 1:
style='mso-tab-count:3'> DoThis
style='mso-tab-count:2'> style='color:blue'>Case else:
style='mso-tab-count:3'> DoThis
style='mso-tab-count:2'> style='color:blue'>End Select
style='mso-tab-count:1'> Else
style='mso-tab-count:2'> style='color:blue'>While This
style='mso-tab-count:3'> DoThisstyle='color:blue'>
style='mso-tab-count:2'> style='color:blue'>Wend
style='mso-tab-count:1'> End
If
End style='color:blue'>With
This
is good indentation and makes your code very readable.
Just
one more thing, do not squash all your code up. Leave spaces between blocks and
lines, or blocks and other blocks, between functions, subs E.T.C.
Lines.
Break long ones up, and don’t use the “:” function
If
you have a long line such as an API function:
Declare Function BitBlt style='color:#3366FF'>Lib "gdi32" Alias
"BitBlt" (ByVal hDestDC style='color:#3366FF'>As Long, ByVal
x As Long,
ByVal y As
Long, ByVal
nWidth As Long,
ByVal nHeight As
Long, ByVal
hSrcDC As Long,
ByVal xSrc As
Long, ByVal
ySrc As Long,
ByVal dwRop As
Long) As style='color:#3366FF'>Long
Fortunately
for us this has already been put into separate lines for us, but imagine it
hadn’t.
To
break something up into separate lines use this “ _” at the end of the line.
E.G.
Original
Line:
FirstPart
SecondPart ThirdPart
Broken
Up:
FirstPart
_
Second
Part _
Third
Part
Simple
enough right?
Another
thing I see is this:
FirstLine:
SecondLine: ThirdLine
Don’t
put separate lines onto one line.
There
we go. That was simple right?
Commentingstyle='font-size:14.0pt;mso-bidi-font-size:12.0pt'>
Just
a quick note on this.
Do
not overdo the comment lines. Only explain things, which are difficult, in
detail. For the rest, a quick description of what the particular piece of code
does.
Most
of the time people do not bother to comment their code at all. This is
something to avoid doing. Always put some commenting in.
A
good idea is: If you have written any custom subs/ functions/ properties E.T.C.
then you should put a short comment line at the beginning with a description of
the function, what you should pass to the parameter if there are any, and what
the function returns if it returns something.
There
are two ways to comment:
1 style='color:#339966'>‘Comment here
2style='color:#339966'> Rem Comment here
I
think Rem stands for remark
Ok.
I’m done now
Remember
I am not saying here that my way is right. Your way is wrong. Change your ways
now.
All
I am saying is that you should adopt some kind of conventions to make your code
more readable. I use the ones I do because those are the ones I see most often
and find most readable.
Thanks
for reading this and please leave your feedback. Maybe it’s sad but I
love feedback. A vote wouldn’t do any harm either. I’m not after code of
the month or anything but if somebody comes across this and it has a good vote
then they are more likely to read it. More people read this and everybody’s
code becomes more readable (Then world peace and happiness for everybody style='font-size:10.0pt;mso-bidi-font-size:12.0pt;font-family:Wingdings;
mso-ascii-font-family:"Times New Roman";mso-hansi-font-family:"Times New Roman";
mso-char-type:symbol;mso-symbol-font-family:Wingdings'>J)
<Coding Genius>
Comments on this post
No comments have been added for this post.
You must be logged in to make a comment.