Which one is more tend to throw error?
I am reviewing an application and I am wondering which one is more error-prone?
Public Function 开发者_StackOverflow社区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 If
block, then in both cases it will exit the function. The code that executes if it does not enter the body of the first If
block is also identical.
first one, because you won't be going through everything if vsValue is nothing, otherwise if it is something and the second uCase value's criterias are not there, then it will go to the final else. Which will return false when you may only be missing a specific state.
It just makes it so the program wouldn't need to go through two extra if's in the end.
I believe none is more error-prone. 2nd one might be a little better since it uses else if instead of 2 ifs ...
An observation with your function: If you pass in a boolean True
value that has been converted to an integer it will fail because it evaluates to -1 not 1.
When Visual Basic converts numeric data type values to Boolean, 0 becomes False and all other values become True
I wrote something very similar to this as follows which evaluates the False side first:
Public Shared Function ConvertBool(ByVal Value As String) As Boolean
If Value Is Nothing Then Return False
Select Case Value.ToUpper
Case "", "0", "N", "FALSE", "F" 'etc
Return False
Case Else
Return True
End Select
End Function
The end result is going to be the same in either case. May I suggest the following refactoring technique which makes the code a little more readable and is easier to expand to include other matches?
Public Function ToBool(ByVal vsValue As String) As Boolean
If Not String.IsNullOrEmpty(vsValue) Then
Dim value As Boolean = False
vsValue = vsValue.ToUpper()
value = value OrElse vsValue = "TRUE"
value = value OrElse vsValue = "1"
value = value OrElse vsValue = "Y"
value = value OrElse vsValue = "YES"
value = value OrElse vsValue = "T"
Return value
End If
Return False
End If
Here is another possibility.
Public Function ToBool(ByVal vsValue As String) As Boolean
If Not String.IsNullOrEmpty(vsValue) Then
vsValue = vsValue.ToUpper()
Select Case vsValue
Case "TRUE": Return True
Case "1": Return True
Case "Y": Return True
Case "YES": Return True
Case "T": Return True
End Select
End If
Return False
End If
In the above the example the compiler will emit a sequencial lookup. If the number of case statements hits a certain threshold it may convert the entire Select
construct into a Dictionary
lookup like what C# does. If this is a function that you expect to be called then you could experiment with both ways to see which one is faster.
精彩评论