D
deltaquattro
Hello,
taking the cue from the previous thread I opened ("Error handling in
VBA"), I would like to ask you if in your opinion it's usually better
to check input data to a subroutine, inside the subroutine, or to move
each check to a distinct subroutine. As an example, I'll post again my
Interp subroutine, this time with some modifications added by Peter T:
Public Function Interp2(xArr() As Double, yArr() As Double, _
x As Double) As Double
Dim i As Long
' this is the part ...
If ((x < xArr(LBound(xArr))) Or (x > xArr(UBound(xArr)))) Then
'MsgBox "Interp2: x is out of bound" & "X = " & x
'.... I am considering moving outside the subroutine
Else
For i = LBound(xArr) To UBound(xArr)
If xArr(i) = x Then
Interp2 = yArr(i)
Exit For
ElseIf xArr(i) > x Then
Interp2 = yArr(i - 1) + (x - xArr(i - 1)) / _
(xArr(i) - xArr(i - 1)) * (yArr(i) - yArr(i
- 1))
Exit For
End If
Next i
End If
End Function
From one point of view, I would say yes, because a single subroutine
should just do a single thing, as someone on this ng recently
reminded me (don't remember who). On the other hand, if one follows
this principle too strictly, wouldn't he end writing "unsafe" code? If
I take out the check from Interp2 and move it to another subroutine
which the caller has to call before Interp2, I risk forgetting to add
the call to the checker subroutine when I reuse Interp2 in another
code. Sure, I could place the call to the checking sub inside Interp2:
however, this way I end up having a code with the same functionalities
as before, but with the added slowdown due to the call to the checking
subroutine.
Best Regards
Sergio
taking the cue from the previous thread I opened ("Error handling in
VBA"), I would like to ask you if in your opinion it's usually better
to check input data to a subroutine, inside the subroutine, or to move
each check to a distinct subroutine. As an example, I'll post again my
Interp subroutine, this time with some modifications added by Peter T:
Public Function Interp2(xArr() As Double, yArr() As Double, _
x As Double) As Double
Dim i As Long
' this is the part ...
If ((x < xArr(LBound(xArr))) Or (x > xArr(UBound(xArr)))) Then
'MsgBox "Interp2: x is out of bound" & "X = " & x
'.... I am considering moving outside the subroutine
Else
For i = LBound(xArr) To UBound(xArr)
If xArr(i) = x Then
Interp2 = yArr(i)
Exit For
ElseIf xArr(i) > x Then
Interp2 = yArr(i - 1) + (x - xArr(i - 1)) / _
(xArr(i) - xArr(i - 1)) * (yArr(i) - yArr(i
- 1))
Exit For
End If
Next i
End If
End Function
From one point of view, I would say yes, because a single subroutine
should just do a single thing, as someone on this ng recently
reminded me (don't remember who). On the other hand, if one follows
this principle too strictly, wouldn't he end writing "unsafe" code? If
I take out the check from Interp2 and move it to another subroutine
which the caller has to call before Interp2, I risk forgetting to add
the call to the checker subroutine when I reuse Interp2 in another
code. Sure, I could place the call to the checking sub inside Interp2:
however, this way I end up having a code with the same functionalities
as before, but with the added slowdown due to the call to the checking
subroutine.
Best Regards
Sergio