by freework · 11y ago · 0 comments
This snippet should only live in one place:
if (something) {
for (blah blah) {
something_else();
}
do_something();
}
on the other hand, this code can be copy+pasted as much as you like: do_something();
do_another_thing();
another_call();
some dogmatic programmers will want to place those last three lines of code into a separate function and instead copy+paste that. But I've found that doing that sometimes makes the code more harder to read.if you copy:
do_something();
do_another_thing();
another_call();
a few times in your codebase, but decide that you want to alter that a bit: do_something();
perform_new_feature();
do_another_thing();
another_call();
you can hurt yourself by forgetting that that alteration needed to be done to each instancejust easy to maintain if you do this:
function dodoan() {
do_something();
do_another_thing();
another_call();
}
then copy paste dodoan() as many times as you wantthough i do agree all rules have their exceptions and should be handled in such a way that allows for future alterations stead some obsessive mindless adherence
def a_and_b_and_c() {...}
def a_and_b_and_c_error_checked_internally() {...}
def a_and_new_and_b_and_c {...}
def a_and_new_and_b_and_c_error_checked_internally() {...}
def a2_and_b_and_c
And so on. Basically you are just putting a memorization task on calling the names rather than using the underlying bits well. Learning the balances around this is one of those "craft" bits of programming.Your parent's comment addresses by far the best argument for the principle, which is that any time something is likely to change in a snippet, copy-pasting should be undertaken rarely and thoughtfully. With the recognition that nearly any given snippet is likely to change in some way, we can conclude that nearly any copy-paste is "bad".
Your comment is then a great argument against the thoughtless application of the principle, recognizing that yes, granted, nearly any snippet will change, but the required changes may well not be uniform.
Personally, I think it still makes sense to pull out shared behavior for the period of time in which it is shared. During that period of time, all you have is a guess that the code might eventually diverge. If there comes a point in time when you do want the code to diverge, it is straightforward to copy-paste it back out, if branching or creating a similar method with the differences is a worse option. On the flip side, if you haven't pulled out the shared behavior, and you discover that you want to change it everywhere in the same way, it is far less straightforward to go find all those places, or even be aware that you need to do so.
Another point is naming by purpose rather than behavior. To continue your example, it makes sense to ask why one method checks internally and the other externally? What different purposes do the different checking styles have? If there are good answers to questions like that, the methods can be named better, and it becomes less about memorizing name than understanding when to use which.
Of course none of this is at all black and white, and I think you're spot on that this is one of those craft bits, and among the most important!
I think it comes down to this - when I first learned to code, it was hard enough to keep track of a couple different functions. As experience came, what I could track and reason about and keep in my mental model grew, so now that I've got some experience I can see more of how the whole system will grow and interact. I'm not really smarter per se, but rather I just have more practice as putting it all together.
Analyzing this further would require having a more fleshed out example.