Superfly1611 Posted July 2, 2008 Posted July 2, 2008 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. Quote
Administrators PlausiblyDamp Posted July 3, 2008 Administrators Posted July 3, 2008 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. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Nate Bross Posted July 3, 2008 Posted July 3, 2008 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. Quote ~Nate� ___________________________________________ Please use the [vb]/[cs] tags on posted code. Please post solutions you find somewhere else. Follow me on Twitter here.
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.