Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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:

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

 

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

 

   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.

  • Administrators
Posted

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.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

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:

 

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

 

vs.

 

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.

~Nate�

___________________________________________

Please use the [vb]/[cs] tags on posted code.

Please post solutions you find somewhere else.

Follow me on Twitter here.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...