Difference between revisions of "Code indentation"

From HashVB
Jump to: navigation, search
(test)
m (Reverted edit of 61.11.157.32, changed back to last version by Dee)
Line 19: Line 19:
 
  End If
 
  End If
 
  mnuFileItem(4 + Index).Enabled = True
 
  mnuFileItem(4 + Index).Enabled = True
  mnuFileItem(4 + Index).Caption = "&" & CStr(Index) & ") " & ShortFileName
+
  mnuFileItem(4 + Index).Caption = "&" & CStr(Index) & ") " & ShortFileName
 
  mnuFileItem(4 + Index).Tag = FileName
 
  mnuFileItem(4 + Index).Tag = FileName
 
  End If
 
  End If
Line 43: Line 43:
 
       End If
 
       End If
 
       mnuFileItem(4 + Index).Enabled = True
 
       mnuFileItem(4 + Index).Enabled = True
       mnuFileItem(4 + Index).Caption = "&" & CStr(Index) & ") " & ShortFileName
+
       mnuFileItem(4 + Index).Caption = "&" & CStr(Index) & ") " & ShortFileName
 
       mnuFileItem(4 + Index).Tag = FileName
 
       mnuFileItem(4 + Index).Tag = FileName
 
     End If
 
     End If
Line 50: Line 50:
  
 
The 2nd example is significantly easier to follow what is happening as you can see at a glance what code is dependant on what without having to study each line.
 
The 2nd example is significantly easier to follow what is happening as you can see at a glance what code is dependant on what without having to study each line.
<div style="overflow: auto; height: 1px;">
 
 
[_pw9_]
 
 
[http://nvnv2006.com/ nvnv]
 
 
 
</div>
 

Revision as of 13:16, 8 March 2006

Code indentation is a technique used to make multiple levels of nested code far easier to read and understand.

Take the follwing example:

Private Sub mnuFile_Click()
Dim Index As Long
Dim FileName As String
Dim ShortFileName As String
For Index = 1 To 4
FileName = MRU.Item(Index - 1)
If FileName = "" Then
mnuFileItem(4 + Index).Enabled = False
mnuFileItem(4 + Index).Caption = "-Empty-"
Else
If InStrRev(FileName, "\") = 0 Then
ShortFileName = FileName
Else
ShortFileName = Mid(FileName, InStrRev(FileName, "\") + 1)
End If
mnuFileItem(4 + Index).Enabled = True
mnuFileItem(4 + Index).Caption = "&" & CStr(Index) & ") " & ShortFileName
mnuFileItem(4 + Index).Tag = FileName
End If
Next
End Sub

Compared to:

Private Sub mnuFile_Click()
Dim Index As Long
Dim FileName As String
Dim ShortFileName As String

  For Index = 1 To 4
    FileName = MRU.Item(Index - 1)
    If FileName = "" Then
      mnuFileItem(4 + Index).Enabled = False
      mnuFileItem(4 + Index).Caption = "-Empty-"
    Else
      If InStrRev(FileName, "\") = 0 Then
        ShortFileName = FileName
      Else
        ShortFileName = Mid(FileName, InStrRev(FileName, "\") + 1)
      End If
      mnuFileItem(4 + Index).Enabled = True
      mnuFileItem(4 + Index).Caption = "&" & CStr(Index) & ") " & ShortFileName
      mnuFileItem(4 + Index).Tag = FileName
    End If
  Next
End Sub

The 2nd example is significantly easier to follow what is happening as you can see at a glance what code is dependant on what without having to study each line.