patterncsharpMinor
Calling functions with different variable types
Viewed 0 times
withdifferenttypesfunctionscallingvariable
Problem
I need to call the function that will get the value of a datatable cell with the
To avoid writing two functions that, I wrote this code:
Can you tell me if it uses bad coding practices or is potentially problematic?
ColIndex, either a text (column name) or an integer (columnindex).To avoid writing two functions that, I wrote this code:
Public Function DTableCellValue(ByRef RowData As DataRow, _
ByVal ColIndex As Object, _
Optional ByVal DefaultVal As String = "") As Object
Try
Dim ret As Object = RowData.Item(ColIndex)
If IsDBNull(ret) Then
Return DefaultVal
Else
Return ret
End If
Catch ex As Exception
Throw
End Try
End FunctionCan you tell me if it uses bad coding practices or is potentially problematic?
Solution
If you use this method quit often by passing an
My advice would be to write two methods one using an
Based on the naming guidelines you should name methods by using a verb or a verb phrase.
Based on the same guidelines method arguments should be named using
The
A construct like
can be simplified by removing the redundant
because if the condition is
Implementing the mentioned points (leaving aside the method name) leads to two methods like so
Integer you will get performance problems because of the boxing and unboxing of the struct. My advice would be to write two methods one using an
Integer and the other using a String as argument.Based on the naming guidelines you should name methods by using a verb or a verb phrase.
DTableCellValue looks by its name more as a property than a method. Based on the same guidelines method arguments should be named using
camelCase casing. Although VB.NET isnt case sensitive I would like to suggest to follow these guidelines. The
try..catch is in its current form senseless because if an exception is thrown you are just rethrowing it. A construct like
If condition Then
Return Value
Else
Return otherValue
End Ifcan be simplified by removing the redundant
else like so If condition Then
Return Value
End If
Return otherValuebecause if the condition is
true the else won't be reached. Implementing the mentioned points (leaving aside the method name) leads to two methods like so
Public Function DTableCellValue(ByRef rowData As DataRow, _
ByVal colIndex As Integer, _
Optional ByVal defaultVal As String = "") As Object
Dim ret As Object = rowData.Item(colIndex)
If IsDBNull(ret) Then
Return defaultVal
End If
Return ret
End Function
Public Function DTableCellValue(ByRef rowData As DataRow, _
ByVal colIndex As String, _
Optional ByVal defaultVal As String = "") As Object
Dim ret As Object = rowData.Item(colIndex)
If IsDBNull(ret) Then
Return defaultVal
End If
Return ret
End FunctionCode Snippets
If condition Then
Return Value
Else
Return otherValue
End IfIf condition Then
Return Value
End If
Return otherValuePublic Function DTableCellValue(ByRef rowData As DataRow, _
ByVal colIndex As Integer, _
Optional ByVal defaultVal As String = "") As Object
Dim ret As Object = rowData.Item(colIndex)
If IsDBNull(ret) Then
Return defaultVal
End If
Return ret
End Function
Public Function DTableCellValue(ByRef rowData As DataRow, _
ByVal colIndex As String, _
Optional ByVal defaultVal As String = "") As Object
Dim ret As Object = rowData.Item(colIndex)
If IsDBNull(ret) Then
Return defaultVal
End If
Return ret
End FunctionContext
StackExchange Code Review Q#102043, answer score: 7
Revisions (0)
No revisions yet.