/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…
A good rule of thumb is: never copy & paste.
If 4/5ths of functions A and B are identical, split them up. Have both of them call a new function C that implements the common part.
If you show the code where you’re having this problem, we can suggest how to best split it up.