Print Page | Close Window

Nesting

Printed From: Mirage Source
Category: Tutorials
Forum Name: Approved Tutorials
Forum Discription: All tutorials shown to actually work with MSE are moved here.
URL: http://ms.shannaracorp.com/backup-forums/forum_posts.asp?TID=35
Printed Date: 20 December 2006 at 6:01pm
Software Version: Web Wiz Forums 8.01 - http://www.webwizforums.com


Topic: Nesting
Posted By: Sync
Subject: Nesting
Date Posted: 07 February 2006 at 5:43pm
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. :)



Replies:
Posted By: Sync
Date Posted: 07 February 2006 at 5:43pm
Approved



Posted By: Da Undead
Date Posted: 05 March 2006 at 12:36pm

lol you approved yourself. Thats funny :p.

I still don't under stand wat Nesting is though >_> :(.



Posted By: funkynut
Date Posted: 05 March 2006 at 1:07pm
He didnt approve himself, he approved verrigans tut, he brought the tut from the old forums.  And nesting is pretty much organizing code into blocks by indenting it 


Posted By: Da Undead
Date Posted: 05 March 2006 at 2:21pm
Does is speed up process or something? Explain plz..


Posted By: Misunderstood
Date Posted: 05 March 2006 at 4:10pm
read the f**kin topic. It says what nesting is. Quit being such an idiot and try finding answers yourself.

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.


guess where I found that? THIS topic.


Posted By: Renegade
Date Posted: 05 March 2006 at 4:21pm
Miss calm down but i do agree with you STRONGLY. Dude look though the tutorials before you post

-------------
Eternia online, Coming soon to a movie i mean computer near you!


Posted By: rochal
Date Posted: 11 March 2006 at 11:55am
hey folks: http://en.wikipedia.org/wiki/KISS_principle 



Posted By: funkynut
Date Posted: 11 March 2006 at 12:00pm
err, whats that meant to refer to?

You talking about verrigans post or something else


Posted By: Sonire
Date Posted: 11 March 2006 at 1:06pm
Originally posted by Da Undead

lol you approved yourself. Thats funny :p.


No, didn't. He approved Verrigan's tutorial.
Originally posted by Sync

Originally posted by Verrigan


Originally posted by Da Undead

I still don't under stand wat Nesting is though >_> :(.


Really? Here, this will help:
Originally posted by Verrigan

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.



Additionally, I have found a few links you should check out, Da Undead:

http://secure.hop.com/index.cfm?AFID=158&redirCampID=154 - http://secure.hop.com/index.cfm?AFID=158&redirCampID=154
http://www.dictionary.com - Nesting

And finally, just stfu, and read a tutorial, and maybe even ATTEMPT to understand it, before you make a post, and waste bandwidth.



-------------
I grant permission for anyone to alter a tutorial previously posted by me for use in a tutorial submission for MSE, so long as "Originally Coded By Sonire" is credited at the top of the tutorial.


Posted By: Renegade
Date Posted: 11 March 2006 at 2:28pm

lmao sonire well said

http://secure.hop.com/index.cfm?AFID=158&redirCampID=154 - http://secure.hop.com/index.cfm?AFID=158&redirCampID=154

Lol!



-------------
Eternia online, Coming soon to a movie i mean computer near you!


Posted By: hintswen
Date Posted: 12 March 2006 at 1:48pm
wow, that is so true. if only everyone would nest their code all the time. 


Posted By: Leighland
Date Posted: 12 March 2006 at 5:31pm
Nesting is a must

@Sonire... gonna save that one for sure


Posted By: William
Date Posted: 13 March 2006 at 12:25am
Nesting most slow down the code a lot.

-------------
Phoenix Engine: www.key2heaven.net/phoenix


Posted By: Johnman
Date Posted: 13 March 2006 at 6:07am
Yes William, it slows down the code, we're just being bastards and telling everyone to do it.

Any one remember the 'double' incident?

No... well think about it...


-------------
</windows>


Posted By: Memphis
Date Posted: 13 March 2006 at 7:43am
OMG. Stop being dumb Un Dead* and friggin get with the program.


Posted By: funkynut
Date Posted: 13 March 2006 at 8:42am
William, dunno where you've been, but err, nesting does not make your program slow, its counted as white space, pretty much just there to make everything look organized, and like comments, when compiled arnt included in the actually program



Print Page | Close Window

Bulletin Board Software by Web Wiz Forums version 8.01 - http://www.webwizforums.com
Copyright ©2001-2006 Web Wiz Guide - http://www.webwizguide.info