来源:www.cncfan.com | 2006-1-25 | (有2151人读过)
(Page 3 of 5 )
Customer Spec part 2: "Ok John, that’s great, now we need to do it for all of these numbers too…."
You suddenly need to work out the result of hundreds of sums. But because we have a function that does all of the dirty work (okay, perhaps it’s not that dirty) you only need to add one line of code per calculation:
<% function simplemaths(var1, var2) result=var1+var2 ' Add a new line to do the multiplication result=result*10 response.write result & "<br />" end function
call simplemaths(1, 2) call simplemaths(1, 9) call simplemaths(3, 4) call simplemaths(1, 5) %>
So you’re happily coding away, putting the final sum together when the customer calls you again:
"Erm, I’ve just realized – I need you to multiply all of the numbers by ten before you write them to the screen! Sorry!"
If you had hundreds of individual calculations then that could be a big task. If you have one function that does it all, then it’s easy!
<% function simplemaths(var1, var2) result=var1+var2 ' Add a new line to do the multiplication result=result*10 response.write result & "<br />" end function
call simplemaths(2, 2) call simplemaths(1, 2) call simplemaths(1, 9) call simplemaths(3, 4) call simplemaths(1, 5)
input1=10 input2=20 call simplemaths(input1, input2)
%>
Pretty compelling, eh? So functions are the bee’s knees and re-usable, maintainable code will save us all. The functions that developers have as a resource act as the foundations for the applications that they produce and the ability to control these functions is a valuable one. The concept relies upon the fact that you and your code shouldn’t really have to worry about how to achieve the specific task – because after you’ve coded it once you never need to code it again. It’s a little black-box of code and all you need to know is how to use it (not how it works inside). This description is edging us gently towards the concepts of Object-Orientated (OO) Design and if you’d like to stick with me through the series of articles we’ll attempt to cover this in more detail – by developing more real-life examples and ideally code that we all can use and re-use!
|