ThomasJSmart
Programmer
- Sep 16, 2002
- 634
i have a function that can take a path or string data as an attribute. Just wondering if there is a "right" way to do this (and why).
Similar functions in php seem to take the 2 function route
but this seems a little cumbersome for a developer implementing it, they need to make sure they use the right function.
and it seems to open the door to duplicate code or needing a bunch of followup functions that both initial functions use with further complicates the code.
A single function solution could be to have 2 optional params, and a check to make sure one of them is filled in
but for some reason this feels a bit messy
from a "using the function" perspective it would be ideal to just have 1 param
but this seems a bit unstable and error prone, even more so if the params would be 2 different types of string instead of 1 path - that could be easier recognized.
another option, but probably less intuitive, would be an array
a last one that i can think of is the data and an identifier param
Any other ideas or thoughts on this?
Thanks
site | / blog |
Similar functions in php seem to take the 2 function route
Code:
set_data_with_path($path)
set_data_with_string($string)
but this seems a little cumbersome for a developer implementing it, they need to make sure they use the right function.
and it seems to open the door to duplicate code or needing a bunch of followup functions that both initial functions use with further complicates the code.
A single function solution could be to have 2 optional params, and a check to make sure one of them is filled in
Code:
function set_data($path=false,$string=false){
if(!$path && !$string){
// fail
}
}
from a "using the function" perspective it would be ideal to just have 1 param
Code:
function set_data($path_or_string){
// and then some magic to check if its a path or a string
}
another option, but probably less intuitive, would be an array
Code:
function set_data($path_or_string){ //array('path' => $path) or array('string' => $string)
// we can tell by the key which one it is
}
a last one that i can think of is the data and an identifier param
Code:
function set_data($path_or_string, $type){
// we can tell by the $type which one it is
}
Any other ideas or thoughts on this?
Thanks
site | / blog |