Difference between revisions of "Code indentation"
From HashVB
(test) |
m (Minor typo) |
||
(One intermediate revision by one user not shown) | |||
Line 2: | Line 2: | ||
Code indentation is a technique used to make multiple levels of nested code far easier to read and understand. | Code indentation is a technique used to make multiple levels of nested code far easier to read and understand. | ||
− | Take the | + | Take the following example: |
Private Sub mnuFile_Click() | Private Sub mnuFile_Click() | ||
Dim Index As Long | Dim Index As Long | ||
Line 19: | Line 19: | ||
End If | End If | ||
mnuFileItem(4 + Index).Enabled = True | mnuFileItem(4 + Index).Enabled = True | ||
− | mnuFileItem(4 + Index).Caption = "& | + | 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 = "& | + | 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. | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− |
Latest revision as of 13:09, 20 December 2007
Code indentation is a technique used to make multiple levels of nested code far easier to read and understand.
Take the following 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.