Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> On the other hand, if the code is in a function, then you only need to change it once. And if you do find that one of the ten invocations should not be changed, then you can still copy-paste - or make the function more general.

Ah yes, but what happens if you have to change 3 of the function invocations in one way, 5 in another, and the other two need to be completely rewritten because those aren't even using the same abstraction any more?

If it's all in one function, most developers will try to change that function to make all 10 cases work, when it should never have been one function in the first place.

It is much much easier to fix ten copy-paste places than to untangle a knot that should never have been tied, once it's holding pieces of your system together.



There is no one size fits all.

In a many cases I'd still rather have three or more versions of a function, many which may just be very thin shims to accommodate that scenario than 10 copy/pastes of variations. Or shim at the call site and keep one function if that suits.


If a function does different things in different circumstances it should usually be split into different functions.

Languages like Erlang which can have different versions of a function, selected by pattern matching (with optional guards) make this convenient:

    Name(Pattern11,...,Pattern1N) [when GuardSeq1] ->
        Body1;
    ...;
    Name(PatternK1,...,PatternKN) [when GuardSeqK] ->
        BodyK.


This is such a strange argument. You want to copy and paste code 10 times rather than making a function, because if the requirements change and if the person assigned to fix it is a moron, then it might prevent the moron from choosing one specific way of making a mess?

You can't prevent future morons from doing moronic stuff in the future. They'll just find another moronic thing to do.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: