dcsimg
Results 1 to 1 of 1
  1. #1

    Thread Starter
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    39,938

    What is indenting, and why should I do it?

    Indenting is adding spaces/tabs in front of 'blocks' of code (such as If-End If), so that it is easier for you and other people to see how the code flows (ie: which parts of the code will run under certain situations).

    Here is a simple VB6 code example without indenting:
    Code:
    Private Sub Command1_Click()
    Dim intAnswer As Integer
    
    If IsNumeric(Text1.Text) = False Then
    MsgBox "please enter a number"
    End If
    intAnswer = CInt(Text1.Text) * 10
    MsgBox "10 times " & Text1.Text & " is " & intAnswer
    
    End Sub
    ...and the same code with indenting:
    Code:
    Private Sub Command1_Click()
    Dim intAnswer As Integer
    
      If IsNumeric(Text1.Text) = False Then
        MsgBox "please enter a number"
      End If
      intAnswer = CInt(Text1.Text) * 10
      MsgBox "10 times " & Text1.Text & " is " & intAnswer
    
    End Sub
    This should be done with all kinds of 'blocks' of code that are control structures (things which run only in certain situations, or multiple times), including: If-End If, For-Next, Do-Loop, and more.

    If there are multiple control structures inside each other, the inner one(s) should be indented an extra level, eg:
    Code:
    Private Sub Command1_Click()
    Dim intLoop As Integer
    
      For intLoop = 1 To 10
        If intLoop = Val(Text1.Text) Then
          MsgBox "equal"
        Else
          If intLoop > Val(Text1.Text) Then
            MsgBox "not found"
            Exit For
          End If
        End If
      Next intLoop
    
    End Sub

    Why should you indent your code?
    There are several benefits to indenting your code, most of which save you time, including:
    • You can see at a glance where the end of a code block is (eg: where is the End If for this If?), rather than having to read each line until you find it.
    • When you have used the same kind of code structure multiple times (such as one If-End If inside another), you don't need to work out which "end of block" goes with which "start of block".
    • You can immediately ignore chunks of the code that aren't relevant to what you are currently doing.
    • It is much easier to notice if a line of code is in the wrong place (eg: after an End If when it should be before it).
    Indenting has no effect on how the code runs, and does not alter the size of your finished program.

    There are two things about indenting which are 'bad', but they are both easily ignorable. One is that it takes time to do, but you gain much more time from the benefits. The other is that the size of the code file(s) increases by a small percentage, but as the code for an entire project is usually well under 1MB, the 'waste' will usually only be a few KB.


    What ways are there to do it more easily? [VB6 and earlier, VBA]
    When the cursor is at the start of a line, you can press the Tab key to increase the indent for that line, and Shift-Tab to reduce the indent.

    Instead of the Tab key, you can also use these buttons on the toolbar: Name:  indent.jpg
Views: 8236
Size:  1.6 KB (if they aren't shown, go to "View"->"Toolbars"->"Edit").

    If you highlight multiple lines of code (with the mouse, or Shift and the arrow keys) you can use either of the above methods to indent multiple lines at once.

    Note that the amount of spaces that VB will use for both of these methods depends on your options, which you can set via "Tools"->"Options"->"Tab width".

    How many spaces you should use is up to you, the important thing is to consistently use the same amount. I personally prefer 2, as it is big enough to be obvious, but not so big that lines of code are regularly wider than the window.


    If you use an addin (such as CVMichael's VBTools AddIn) you can indent large sections of code automatically by pressing a single button.


    .
    Last edited by si_the_geek; Mar 22nd, 2010 at 04:28 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width