A Developer’s Debate: Copy/Paste vs. Typing a New Pattern

When working in an existing repository on a team, you tend to follow established patterns to keep the codebase consistent. As a junior developer, you replicate these patterns and apply them to the story you are working on. Either way, when applying these patterns, I find myself debating whether to copy/paste or re-type the pattern.

To some, this might be an insignificant dilemma, but I’ve found it important for efficiency, accuracy, and my learning development. I’ll warn you that this is completely my opinion. So, take it as you please, but I have found that when I am applying an established pattern to a new context, that is the moment I need to instantly decide if I want to copy/paste or type something out. But,  it all depends on the situation.

Tests with Edge Cases

Many times I had to write tests for edge cases that only had slight variations from each other. For example, I may need to write tests for an endpoint or testing client error status codes. Or, I may need to write error tests for a service function. Perhaps I need to repeat these tests for each endpoint or service function. If there is a pattern in the way the tests are arranged and asserted, then I prefer to copy/paste a test with that pattern. Then, I just alter it with the necessary mocks, actions, and assertions so I don’t forget the pattern.

Simple Tweaks

This will be a judgment call, but sometimes a pattern can be easily applied to another function, class, or component, and all you need to do is update the context. It can be as simple as updating the dependency injection, the error message, or where the data is fetched from. Because you only have to make a few updates, you may ask, why aren’t we abstracting what you copied into a reusable function? Is this due to the principle of separation of concerns? That’s something you and your team will need to discuss further. But, if your team decides this is a pattern they want for that class instead of abstracting, then copying and pasting will be far more efficient and accurate as long as you understand the pattern.

Major Changes

You may start adjusting a pattern you copied and pasted, then realize that the new context requires far more changes than you expected. Maybe there was a good pattern for a simple React component that had a form for editing account details and you wanted to copy that component for editing organization details. Then you discover that the EditOrgDetails component has far more complex validation rules and multiple error handlings because the organization details are fetched from various clients or endpoints.

At that point, the copied component becomes a distraction. It is only when you start typing out the EditOrgDetails component from scratch that you figure out whether you stick to the pattern or customize this component to its needs. Still, even if you decide to type this component from scratch mid-way, it is far better to start over than to get lost in your thought process.

Learning Development

Just applying a pattern is not good enough. I try to avoid copying and pasting when I need to have complete grasp of why, how, and when to use a pattern. For example, in React components, there might be a certain way parent and children components are structured. Perhaps the parent component handles rendering an error modal or a spinner and fetches data for the children components. Then, the children components render the actual content like a form or the data.

Though it might be easier to simply copy and paste, if you just apply the pattern, then you might not realize that you have a gap in understanding. I only discovered my gaps when I started typing the component out and asking questions. If the child component is the one rendering the data, why fetch it in the parent component? This question makes me want to break away from the pattern and try fetching the data from the child component. Only when I do this do I understand that the error handling and the conditions to render a spinner all depend on whether the app successfully fetched the data.

But how can I know this if I just follow the pattern? I discovered that as I typed out the pattern directly in the context of the story, I started asking questions. Is this necessary? Why are we doing this? What if I changed it? Only by seeking out those questions do I begin to understand the intent behind that pattern and build a deeper understanding.

Extra Miscellaneous Situations

A few non-pattern related situations I also found to copy and paste, is when I need to copy:

  • variable names
  • error messages
  • translation texts

I highly recommend copying and pasting rather than re-typing them out. The spelling or text remain exactly the same, and you will also reduce the possibility of tendonitis or carpal tunnel syndrome damage.

Final Say

In the end, it’s your call. I just find that this small decision has helped me grow as a developer to control when I want to learn and when I want to be accurate and efficient.

Conversation

Join the conversation

Your email address will not be published. Required fields are marked *