HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Calling functions with different variable types

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
withdifferenttypesfunctionscallingvariable

Problem

I need to call the function that will get the value of a datatable cell with the 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 Function


Can you tell me if it uses bad coding practices or is potentially problematic?

Solution

If you use this method quit often by passing an 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 If


can be simplified by removing the redundant else like so

If condition Then
    Return Value
End If 
Return otherValue


because 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 Function

Code Snippets

If condition Then
    Return Value
Else
    Return otherValue
End If
If condition Then
    Return Value
End If 
Return otherValue
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 Function

Context

StackExchange Code Review Q#102043, answer score: 7

Revisions (0)

No revisions yet.