I have a function that returns an integer, however I would like to expand it to add a new param to it. With this param, however, the function would have to return an array.
- Is it bad practice to have a function that returns either an array or an integer based on a param?
- If so, how can I solve this?
I think it would be bad practice also to just copy-paste the entire function for 4-5 extra lines.
If at all possible, I would call one function from within another.
For the function you have in mind, it might not be possible to do this, but it may be a good option.
As an extra note, as the functions are related to one another, I would write them as class methods to “group” them together.
Take
Usersfor example; I might need a function to get a single user and another to get multiple users. It makes sense to collect these methods in a class: