LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 65
Default A coding style question

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

 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
vba coding question thomas donino Excel Programming 13 August 11th 09 03:49 AM
Comma Style Question Steveal New Users to Excel 3 July 6th 09 05:56 PM
Coding Question Jcraig713 Excel Programming 1 April 2nd 09 09:58 PM
VBA Coding Question Matt Excel Programming 3 January 30th 08 12:03 PM
coding question No Name Excel Programming 1 September 28th 04 05:26 PM


All times are GMT +1. The time now is 09:42 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"