G
Guest
Hi. I have a class with an collection property. The collection property is
sometimes equal to nothing. The class has a function named 'Exists' which
returns TRUE if the specified string exists in the collection, and FALSE if
it does not. This is illustrated below.
My co-worker and I are having a friendly disagreement as to whether or not
we should rely on an exception to return a result. My coworker's version of
the function and mine are shown below. As you can see, they are both having
the same logic, but one is using a try-catch (relying on the catch to return
FALSE), and the other uses an IF statement.
Is one of these functions necessarily better than the other? Is my
coworker's function better than mine or vica vera? Are there any performance
benefits of using try-catch versus if-endif?
Thanks.
-----------------------------------------------------------
Public Class MyClass
Protected _Col As Collection
'************************************************************
' MY COWORKER'S FUNCTION
Public Function Exists(ByRef sKey As String) As Boolean
Try
If Not _Col.Item(sKey) Is Nothing Then Return True
Catch 'sometimes _Col=Nothing, hence the try-catch
Return False
End Try
End Function
'************************************************************
'************************************************************
' MY FUNCTION
Public Function Exists(ByRef sKey As String) As Boolean
Dim bReturn as Boolean = False
If Not _Col Is Nothing AndAlso _
Not _Col.Item(skey) is Nothing Then
bReturn = True
End If
Return bReturn
End Function
'************************************************************
End Class
sometimes equal to nothing. The class has a function named 'Exists' which
returns TRUE if the specified string exists in the collection, and FALSE if
it does not. This is illustrated below.
My co-worker and I are having a friendly disagreement as to whether or not
we should rely on an exception to return a result. My coworker's version of
the function and mine are shown below. As you can see, they are both having
the same logic, but one is using a try-catch (relying on the catch to return
FALSE), and the other uses an IF statement.
Is one of these functions necessarily better than the other? Is my
coworker's function better than mine or vica vera? Are there any performance
benefits of using try-catch versus if-endif?
Thanks.
-----------------------------------------------------------
Public Class MyClass
Protected _Col As Collection
'************************************************************
' MY COWORKER'S FUNCTION
Public Function Exists(ByRef sKey As String) As Boolean
Try
If Not _Col.Item(sKey) Is Nothing Then Return True
Catch 'sometimes _Col=Nothing, hence the try-catch
Return False
End Try
End Function
'************************************************************
'************************************************************
' MY FUNCTION
Public Function Exists(ByRef sKey As String) As Boolean
Dim bReturn as Boolean = False
If Not _Col Is Nothing AndAlso _
Not _Col.Item(skey) is Nothing Then
bReturn = True
End If
Return bReturn
End Function
'************************************************************
End Class