When to alter a function vs when to just write a new one...?
- by Andrew Heath
/is n00b
Through the gift of knowledge and expertise encoded here, I am doing my best to avoid n00b mistakes as I learn the basics of programming.
I use functions when I (think I) can in PHP, and keep them somewhat sorted in different includes.
The n00b problem I'm running into now is situations where perhaps 4/5th of an existing function is relevant to a new need. Maybe there are a slightly different set of inputs, or an additional calculation or two in the series, or output needs a different format/structure... but the core of the function is still applicable.
Is there a good rule of thumb regarding when one should bolt-on crap to an original function and when one should (literally) copy & paste most of it into a new function and tweak to fit the situation?
On the one hand I feel bad duping code, on the other I feel bad cluttering up an existing function with stuff not always needed...