VB.Net Syntax best practice

Superfly1611

Regular
Joined
Feb 17, 2004
Messages
66
Location
UK
Hello all,

I've just been given the task of taking an existing vb.net web application - which works, I should point out - and refactor the codebase into something more maintainable.

Looking at the existing codebase is more like.... stairing into a bowl full of alphabet spaghetti. All in code-behind files, no object-orientation applied and full of inconsistencies.

My question: I'm a C# developer by trade - I have 1 small project of VB.Net to my name which I wrote 5 years ago...

Is there a best practice guideline for syntactically laying out your code?
For example:
Code:
    Protected Overrides Sub CreateChildControls()
        Dim repeaterControl As Repeater = FindControl("repeaterControl")
    End Sub

Code:
    Protected Overrides Sub CreateChildControls()
        Dim repeaterControl As Repeater
        repeaterControl = FindControl("repeaterControl")
    End Sub

Code:
    Protected Overrides Sub CreateChildControls()
        Dim repeaterControl = FindControl("repeaterControl")
    End Sub

All 3 code blocks compile and run - but is one of them better at performing the task? Is one of them widely considered best practice?
I know which one I prefer - but thats more because it reminds me of C# than anything else.

Any feedback would be welcome.
 
I would tend to use the first one, I find the code simpler and easier to read - it states it's intent more clearly than the second.

The 3rd is only equivalent if you are using VS 2008 and you have implicit variable typing turned on - otherwise you could be declaring repeeaterControl as an object by accident.
 
First or Second option is best.

Be consistant and have a reason for doing it that way -- besides "i like it that way".

As long as you are consistant (and avoid the third option) you'll be fine.

Personally, I'd go with the second because it keeps variable declerations seperate from variable initialization.

I think two lines of code is more readable than one. In this simple case it really makes no difference, but imagine this:

Visual Basic:
Dim myFirstObject as MyObjectTypeWithLongName = someObject.GetObject(123, "val 1", "val 2", SomeThing.EnumValue, ...)

vs.

Visual Basic:
Dim myFirstObject as MyObjectTypeWithLongName 
myFirstObject = someObject.GetObject(123, "val 1", "val 2", SomeThing.EnumValue, ...)

As PD stated the third one could result in objects being of different type than you expect.
 
Back
Top