Mirage Source http://web.miragesource.net/forums/ |
|
Nesting http://web.miragesource.net/forums/viewtopic.php?f=210&t=1504 |
Page 1 of 1 |
Author: | Verrigan [ Tue Mar 13, 2007 4:10 pm ] |
Post subject: | Nesting |
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 Code: Type UserRec Number As Long Name As String * 20 Password As String * 20 Email As String * 255 End Type Starting a Sub/Function Code: 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 Code: 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 Code: 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. Code: 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. Code: 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 Code: 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. Code: 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_ClickExitEnd 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. :) Code: 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. [Edit] Cleaned up code for Dark Echo. (See below) |
Author: | Dark Echo [ Wed Mar 14, 2007 1:16 am ] |
Post subject: | |
Great tutorial dude.. I would just like to add to this very good tutorial is that adding spaces between certain lines of code helps distinguish between certain sections.. Adding spaces is just something i do, i dont know anyone else who does it.. But it does help dramatically.. For example.. Sorry Verrigan im using one of your examples here.. Declaring Variables In a Procedure Code: Private Sub cmdSelectChar_Click()
Dim YesNo As Byte 'Your code goes here. (Yes, you should even nest your comments. :)) End Sub I think nesting is considered good programming practice.. I cant remember the exact words my teacher used.. But yeah.. Something like that.. Again, great tutorial mate.. |
Author: | ShadowLife [ Wed Mar 14, 2007 1:41 am ] |
Post subject: | |
I can honestly say I hate trying to fix someone elses code when they don't nest. Or if their nesting is just a single space... Very nice, I hope most people will carryout nesting in any type of programming or scripting. [Also, I leave blank lines between Dims, SelectCase blocks, IfElseThen Blocks, after Sub and before End Sub] |
Author: | Lea [ Wed Mar 14, 2007 1:46 am ] |
Post subject: | |
For robotics we were making a program to assist scouting other teams. A buddy sent me his code, in old familiar VB6. I take one look, notice all the code is unnested (entirely). Deleted, and started my own program |
Author: | Verrigan [ Wed Mar 14, 2007 3:32 am ] |
Post subject: | |
Dark Echo, I agree. I generally split sections up based on what operation I am performing.. Like, for instance.. If I'm opening a database, and then I'm grabbing a recordset out of one of the tables, I'll separate those two sections by an empty line. This, however, is not as big of an issue for me as nesting. I do it when I'm really programming, but I don't look for it, specifically when looking at another person's code. One thing I hate, though.. Which is really making me want to get rid of GTalk, is that the damn thing gets rid of beginning (and probably ending) spaces... |
Author: | Matt [ Wed Mar 14, 2007 4:26 am ] |
Post subject: | |
Maybe if you email Google about that, they'll fix it. They're pretty user orientated on things like that, so they may listen. GTalk owns all. |
Author: | Verrigan [ Wed Mar 14, 2007 2:17 pm ] |
Post subject: | |
Yeah.. You're probably right. |
Author: | Lea [ Wed Mar 14, 2007 2:37 pm ] |
Post subject: | |
They should add the option, or a tag, or something. Code: /c Now my spacers aren't truncated when I talk!!
could show up as " Now my spacers aren't truncated when I talk!! " edit: Hah, even phpBB truncates this stuff.. |
Author: | Verrigan [ Wed Mar 14, 2007 3:42 pm ] |
Post subject: | |
Well... PHPBB uses HTML, which strips all spaces more than 1... Remember ? |
Page 1 of 1 | All times are UTC |
Powered by phpBB® Forum Software © phpBB Group https://www.phpbb.com/ |