I am reviewing an application and I am wondering which one is more error-prone?
Public Function ToBool(ByVal vsValue As String) As Boolean
If IsNothing(vsValue) OrElse vsValue = "" Then
Return False
End If
If UCase(vsValue) = "TRUE" Or vsValue = "1" Or UCase(vsValue) = "Y" Or UCase(vsValue) = "YES" Or UCase(vsValue) = "T" Then
Return True
Else
Return False
End If
End Function
or
Public Function ToBool(ByVal vsValue As String) As Boolean
If IsNothing(vsValue) OrElse vsValue = "" Then
Return False
ElseIf UCase(vsValue) = "TRUE" Or vsValue = "1" Or UCase(vsValue) = "Y" Or UCase(vsValue) = "YES" Or UCase(vsValue) = "T" Then
Return True
Else
Return False
End If
End Function
There is no difference as to which one is more error prone, as the two code examples are semantically identical. If the code enters the body of the first
Ifblock, then in both cases it will exit the function. The code that executes if it does not enter the body of the firstIfblock is also identical.