I have a function that is similar to this:
public sub TestFunction() As Boolean
On Error GoTo NewError:
Dim testObject As New TestObject
For TestVaiable 0 to 1000
TestObject.TestMethod(TestVariable)
Next
TestFunction = True
Exit function
NewError:
TestFunction = False
End Function
I have two questions:
1) Is it bad practice to keep reusing an object in a loop? I don’t think it is
2)Is it bad practice to return a boolean (false) if there is an error?
It’s good practice to reuse a variable in a loop.
It’s good practice to reuse an object that will either be in the same state for the whole loop, or have a very simple change that relates clearly to the nature of the loop.
It’s bad practice to reuse an object in such a way that it makes it harder to see what’s going on with it.
It’s good practice to return quickly from a Sub or Function .There’s a superstition in VB about returning early that comes from other languages that are irrelevant to it, sort of a computer equivalent to people thinking you shouldn’t split infinitives in English because you can’t in Latin. It’s nonsense.
It’s bad practice to just return from a Sub or Function when you encounter an error, without any further handling unless that is the most sensible thing to do for some reason you can explain in a short comment of less than about 200 characters.
It’s bad practice not to put in that comment of less than 200 characters explaining why it’s okay to just return when that error happened.