Unoriginal Code

The perils of copying code to follow patterns in large code bases
Published on 2024/02/01

Not a mystery that we do plenty of copying and pasting. It used to be mostly from Stackoverflow and your own codebase, now add ChatGPT to the mix. I'll cover how I use ChatGPT another day but I'd be lying if I said I never ported over snippets I found online or from a co-worker. I want to recognize that there are different layers to this and one of them is...not great. There are several reasons why you grab code from somewhere else including:

  • Using the same patterns
  • Having the same test setup
  • Trivial snippets
  • Unfamiliarity with what you're trying to do

And the list goes on (not without overlaps). I am confident there's plenty of videos, or blog posts or entire books around the topic of blindly copying but I was thinking about it today. I sometimes realize that the different way to leverage existing code makes the difference between a Junior and Senior engineer.

Copying to use the same patterns can be ideal to maintain consistency across the codebase but, do you know why that pattern is there? Do you know if it really suits your use case? Is it even a good pattern? Some of the best engineers I've stumbled upon don't give anything for granted. This can lead them to break patterns and change history (for the better), other times I see Juniors accepting the current "idioms" and going along with them. The moment you don't push yourself to have a deeper understanding of the tool you use you're regressing. The moment you write code ported over from somewhere else that you do not understand AND that breaks, you are responsible for it. On top of that you haven't thought of alternatives ahead of time because you took it for granted, you don't have a deep understanding so it will take longer to fix. You introduced the risk of making things worse and prevented yourself from learning and growing.

Copying over a test setup. I see that a lot and when a test doesn't pass I see blank eyes and people force to revisit every line of the setup to make sure they did it right. You could have done that in the beginning and while the end result might be the same, you already understand what your code does and can iterate faster. Sometimes you have unnecessary setup and your test suite is somewhat slow but you either don't notice or don't realize how a slow test affects productivity (especially if it could have been avoided).

You see the pattern here.

Thoughts

Copying code is not inherently bad. You should use the opportunity to learn and possibly never paste code you don't understand. Make it yours and rewrite it in a way that resonates with you and the context in which you're using it. This recommendation is often shared when trying to learn something. Write it down, rephrase it like you were to explain it to a child. Only then you can claim understanding (ever heard of The Feynman Technique?). The same goes for the code you write :)

0
← Go Back