Difference between revisions of "Code indentation"
From HashVB
(Code indentation) |
m (Minor typo) |
||
(2 intermediate revisions by 2 users 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 |
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.