When to alter a function vs when to just write a new one...?

Posted by Andrew Heath on Stack Overflow See other posts from Stack Overflow or by Andrew Heath
Published on 2010-05-07T06:14:35Z Indexed on 2010/05/07 6:18 UTC
Read the original article Hit count: 282

Filed under:
|
|

/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...

© Stack Overflow or respective owner

Related posts about php

Related posts about function