At eimagine, we like to architect full solutions from the ground up. However, that’s not always the case. Sometimes, clients have solutions that are already in place or under development, and we take them over. Developing on someone else’s codebase can be a challenge, especially if they didn’t know what they were doing. Often times, lack of experience (or lack of willingness to learn modern flavors of Microsoft .NET!) is a contributing factor to why a project has failed or a client has come to eimagine to get them across the goal line!

Microsoft .NET is a flexible platform, and as a part of that there a many different ways to get the same thing done in code. However, your solutions can be functional on the outside while being a major mess on the inside. End users might not notice immediately, but as the system grows there are often maintenance issues and performance issues that stem from improper coding standards and practices. Just because it “works” doesn’t mean it’s the best or only way to code your solution.

Here are some common myths and mistakes I see in VB.NET code… If you’re a beginner to Microsoft .NET, ASP.NET or VB.NET, please read and get these facts straight! It’s important to learn programming concepts and language use correctly, or else bad habits pop up. I see it all the time in code. It’s bad when I can tell who originally developed the code based solely on their bad habit left-overs. This is something you’ll want to get right, and be on the lookout to fix when you take over maintenance for code left over from someone else! It’s good for business, and good for your career! Future developers working on your code will thank you :)

“With” versus “Using”

The “With…End With” statements allow you to make repeated references to an object. It is just an easy way to avoid typing the object name over and over. It is not any faster, better, or more optimized that the same code with repeated references to the same object. People seem to confuse the two, and think that “With” will produce better performing results. Here is an example of use:

Dim myObject As FakeObject = New FakeObject
With myObject
.PropertyA = "Foo"
.PropertyB = "Bar"
.SaveChanges()
End With

This produces the exact same result and performance as the code without the “With…End With” statements:

Dim myObject As FakeObject = New FakeObject
myObject.PropertyA = "Foo"
myObject.PropertyB = "Bar"
myObject.SaveChanges()

The “Using…End Using” statements, on the other hand, actually optimizes memory usage. It can only be used on objects that implement the IDisposible interface. When “Using” an object, it will be disposed after the block of code completes, optimizing the memory usage for free space. Here is an example of use:

Using New myObject
myObject.PropertyA = "Foo"
myObject.PropertyB = "Bar"
myObject.SaveChanges()
End Using

Here is the equivalent code without the “Using…End Using” statements:

Dim myObject As FakeObject = New FakeObject
Try
myObject.PropertyA = "Foo"
myObject.PropertyB = "Bar"
myObject.SaveChanges()
Finally
If myObject IsNot Nothing Then
myObject.Dispose()
End If
End Try

In Summary: “With” is just coding shorthand. “Using” is for optimizing garbage collection.

“ByRef” versus “ByVal”

Passing an argument “By Reference” allows the routine to modify the object directly. That means, any actions performed in the routine effect the actual variable in the calling code block. Passing an argument “By Value” means that any modifications made in the routine are to a local variable and will not effect the actual variable in the calling code block.

Often, I see code where a number of variables are passed “ByRef” so they can be modified in a subroutine. This is not a great situation, and is not really the correct way to make your code more modular!

Generally speaking, it is best to avoid using “ByRef” arguments. If you need to return a single value, use a Function that returns the value, and set it to a variable in the calling code block. If you need to modify multiple objects or properties at once in a routine, should you perhaps be representing your collection in a class instead of individual variables? Create a custom class object, and use a Function to return the entire object. Don’t pass a bunch of variables ByRef and modify them in a random subroutine just to break up your code blocks into smaller portions.

In Summary: Use “ByVal” in almost all situations. Avoid passing arguments using “ByRef.” Use Functions to return results, and create class objects to represent complex types.

Looping

I often see the wrong types of iterative loops used for situations in code. Using the wrong type of loop can be counter-productive, and can have unintended side effects! Here are some common ways of doing looping iterations, along with where and how they are best utilized.

For…Next

Dim mySum As Integer = 0
Dim myNumbers() As Integer = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }
For i As Integer = 0 To 9
mySum += myNumbers(i)
Next

Good for: When you know exactly the number of iterations you’ll need (In this example: You always want to process exactly 10 elements in the myNumbers array)

Signs of Misuse:

  • If you are doing a bunch of on-the-spot calculations to figure out the number of items, you might want to use “For Each”
  • If you have an “Exit For” statement in there, you might want to use “Do While/Until”

For Each…Next

Dim mySum As Integer = 0
Dim myNumbers() As Integer = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }
For Each i As Integer In myNumbers
mySum += i
Next

Good for: When you don’t know the number of iterations you’ll need (In this example: The myNumbers array might have a different numbers of elements at any time, and you want to process them all)

Signs of Misuse:

  • If you have a special counter variable in there, you might want to use “For”
  • If you have an “Exit For” statement in there, you might want to use “Do While/Until”

Do While/Until…Loop

Dim isAtTen As Boolean = False
Dim mySum As Integer = 0
Dim myNumbers() As Integer = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }
Dim i As Integer = 1
Do Until isAtTen 
mySum += myNumbers(i)
 i += 1
 If myNumbers(i) = 10 Then
isAtTen = True
End If

Loop

Dim notYetTen As Boolean = True
Dim mySum As Integer = 0
Dim myNumbers() As Integer = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }
Dim i As Integer = 1
Do While notYetTen
mySum += myNumbers(i)
i += 1
If myNumbers(i) = 10 Then
notYetTen = False
End If

Loop

Good for: When you are waiting for a specific condition to exit out of the loop (In this example: You are waiting for the number 10 to appear, then want to stop looping)

Signs of Misuse:

  • If you have a special counter variable in there, or a set number of iterations to complete, you might want to use “For”

Have some other common VB.NET mistakes to add? Comment below!

Like this post? Share it!