I am writing a function to calculate the correlation between x and y (I use R, but the programming language and the purpose of the function is not important), and it has a lot of arguments with different choices. If I use the function by myself, everything is OK.
Now I want to share it with others, and write it in a package. I saw many functions in some standard package has the error handling part at the beginning of the function(and usually the main code is only one or several lines, but the error handling part is very long and contains a lot of if
).
If I am writing a function, say cor(x,y,type1,type2,type3)
, where type1
could be a,b,c
, type2
could be p,q
, type3
could be r,s,t
. And I need to check if x,y is missing or not, right? So what is the best practice to deal with the potential errors? Any book or subject is dealing with this kind of problem (software testing)? I am not a computer science student, so not familiar with the software development process. But I think functions in a software will be much complex than this.
So, what should I do?
-
should I use a lot of
if else
in one functioncor()
? -
should I write two functions say
cor_type2_p()
, andcor_type2_q()
? and do error checking in the main functioncor()
, and only focus on the correct case incor_type2_p()
, andcor_type2_q()
? -
or what else?
Thanks!
Aucun commentaire:
Enregistrer un commentaire