Originally posted by Verrigan
Difficulty: Easy 1/5
I've seen a lot of code from various people on these forums, and most of the code has been difficult to read due to improper nesting. If you want me to look at your code, you will need to use some form of nesting. Don't worry.. Nesting is standard in most programming languages, and has been a standard since I can remember.
This tutorial is designed to show you how to use proper nesting when writing your code.
What is nesting? Nesting is known as the proper indentation of your code. When you make a new sub or function, you place that sub/function declaration to the extreme left, to denote that it is a main part of the module.. Anything that is done inside that sub/function should be nested.
How do I nest? You nest by hitting the TAB key to "indent" your code.
When do I nest? Any time you start something that you need to close, you should nest. (i.e. If..Then..Else..End If)
How far should I nest? This is a user preference. I have seen 2, 4, 6, and even 8. You can set your Tab Width in the Visual Basic Editor options. Personally, I use 2. The default for VB is 4. I will be using 4 for this tutorial.
Can you give me some examples? Sure. The rest of this tutorial will give you some examples on how to nest your code.
User Defined Types
Type UserRec Number As Long Name As String * 20 Password As String * 20 Email As String * 255 End Type
Starting a Sub/Function
Private Sub cmdSelectChar_Click() 'Your code goes here. (Yes, you should even nest your comments. :)) End Sub
The rest of this tutorial will build off of the above code, to show you a finally nested full subroutine.
Declaring Variables In a Procedure
Private Sub cmdSelectChar_Click() Dim YesNo As Byte
'Your code goes here. (Yes, you should even nest your comments. :)) End Sub
If..Then..Else..End If
Private Sub cmdSelectChar_Click() Dim YesNo As Byte
'Your code goes here. (Yes, you should even nest your comments. :)) YesNo = MsgBox("Are you using proper nesting?", vbYesNo, "Proper Nesting")
If YesNo = vbYes Then MsgBox "Great! Glad to see you're making your code readable. :)", vbOKOnly, "Readable Code" Else MsgBox "Well, to each their own, but your code will be difficult to read. :(", vbOKOnly, "Unreadable Code" End If End Sub
Select Case..End Select - This one is a bit more difficult, and can be done in two ways. Since you don't close each case, you can choose not to nest the case statements, which is what I do, or go ahead and nest the case statements, which leaves the 'End Select' nested back further. My example will not nest the case statements.
Private Sub cmdSelectChar_Click() Dim YesNo As Byte
'Your code goes here. (Yes, you should even nest your comments. :)) YesNo = MsgBox("Are you using proper nesting?", vbYesNo, "Proper Nesting")
Select Case YesNo Case vbYes MsgBox "Great! Glad to see you're making your code readable. :)", vbOKOnly, "Readable Code" Case vbNo MsgBox "Well, to each their own, but your code will be difficult to read. :(", vbOKOnly, "Unreadable Code" Case Else MsgBox "You have (somehow) entered an invalid response.", vbOKOnly, "Invalid Response" End Select End Sub
Here is an example with the nested Case statements.
Private Sub cmdSelectChar_Click() Dim YesNo As Byte
'Your code goes here. (Yes, you should even nest your comments. :)) YesNo = MsgBox("Are you using proper nesting?", vbYesNo, "Proper Nesting")
Select Case YesNo Case vbYes MsgBox "Great! Glad to see you're making your code readable. :)", vbOKOnly, "Readable Code" Case vbNo MsgBox "Well, to each their own, but your code will be difficult to read. :(", vbOKOnly, "Unreadable Code" Case Else MsgBox "You have (somehow) entered an invalid response.", vbOKOnly, "Invalid Response" End Select End Sub
Either way is acceptable, and is up to the programmer's preference.
With..End With
Private Sub cmdSelectChar_Click() Dim uData As UserRec 'User-Defined Type
'Your code goes here. (Yes, you should even nest your comments. :)) With uData .Number = GetAvailableDBNumber .Name = "Uber D00d" .Password = "$0m3U632D00d" .Email = "uberd00d@uberd00d.com" End With End Sub
Error Handling - Error handling can be nested with your code, but it can also be placed all the way to the left, which is how I do it.
Private Sub cmdSelectChar_Click() On Error GoTo cmdSelectChar_ClickErr 'Your code goes here. (Yes, you should even nest your comments. :)) cmdSelectChar_ClickExit: Exit Sub cmdSelectChar_ClickErr: MsgBox Err.Description, vbExclamation, "Run-Time Error (" & Err.Number & ")" Resume cmdSelectChar_ClickExit End Sub
Open..Close - This one doesn't have to be nested, but it will help you remember to close your files when you're done with them. :)
Private Sub cmdSelectChar_Click() 'Your code goes here. (Yes, you should even nest your comments. :)) Open "C:\bob.txt" For Input As #1 'Code to read your file goes here. Close #1 End Sub
I'm sure there are other things that can be nested, but you get the idea. Nesting helps you, and the people you send your code to, keep track of where sections begin and end. It is optional, however, and is not a requirement of programming in most languages. (VB Included) But it is good practice to use proper nesting techniques to help yourself and your troubleshooters. :)
|