Programming Memoirs: The Benefits of Copy-Pasting Code

Any software developer who has worked on a significant number of projects has at one time or the other either copy-pasted code or design and problem solving patterns. These could either be within one’s projects or from others. A great example that would resonate with many developers is StackOverflow.
I regularly browse the website not only to help me quickly get past errors and wrap my head around new or forgotten information but also to browse answers to questions and see various ways to solve problems. It is edifying to say the least. An example was a question on how to print 1 to 100 without using loops or conditionals. The best answer, to me anyway, was to catch an exception (not great programming practice I know but the solution was concise and elegant).
Copy pasting code
Various opportunities lie in the copy-pasting of code. If copying code within the same project or on projects you have worked on, it saves the developer a lot of time, although it is an inelegant way of achieving code reuse. It also refreshes one’s understanding of the old code base and refreshes your memory of the project’s code.
Better understanding of code leads to one major benefit: reduction of code entropy or bit rot. Bit rot refers to the tendency of code quality reducing over time or as the codebase grows. The only way to reduce entropy in software systems, just like in thermodynamics, is to actively pursue measures to reduce it. One of the ways in which entropy can be maintained or even reduced as the codebase grows is through code refactoring. Copy pasting code requires editing the variable names at the very least and hooking the code to the rest of the system or module. This provides a rich opportunity for refactoring code.
During these changes, a software developer may see a better way of solving the problem, be it in terms of code readability, maintainability, performance etc. By making these changes in the original codebase from which they are copied the code, they have helped prevent bit rot from growing worse in the codebase. They also improve desired features and characteristics of the system and the code such as efficiency, correctness and scalability.
Copy pasting code and making changes to the code also offers an opportunity most developers rarely pass up, experimenting. Since one doesn’t have to develop the code from scratch (recommended for beginners though), it leaves time to play around with the code. The way most developers play with code is by trying to break it or make it better. This offers one novel ways of solving problems, a deeper understanding of the way the code exactly works and more resilient and better code.
Another great reason to copy-paste code is that it helps in preventing with re-inventing the wheel. In keeping with the DRY (Don’t Repeat Yourself) principle, the only time you should repeat yourself is when you are mastering coding. After that, the lesser lines of code you write to accomplish something, the better you should feel about it. Code copy pasting helps in reducing repetition of effort, largely increasing efficiency and the time required to write and test code.
Copying pasting design patterns
Design patterns are what truly distinguish coders from developers. Coders usually stumble on design patterns either by accident or by design (though unintentional). Developers are usually already turning the problem over in the heads and evaluating the most appropriate design patterns to apply.
The beauty of learning design patterns is that it free you (mostly) from the nuances of programming languages. Once you have learnt design patterns, it becomes a breeze to learn the fundamentals of any language and get productive in it in lesser time. If knowing several programming languages and paradigms is like having a tool belt, knowing design patterns is like having manuals on how to carry out different tasks like making a table building a house with the tools you have. It does not matter whether the material (technology stack) is wood, stone or metal. If you know the design, it is then simply a matter of using the materials to realize the desired product’s design.
Conclusion
Everyone, from the expert level developers to newbies and (especially) noobs, everyone copy pastes code. This varies from using Git to check out code (expert) to copy pasting it from examples (newbies) and answer-question forums (noobs) other than StackOverflow and peers of course :). Design patterns are essentially copy pasted problem solving models, from the Singleton patterns to the MVC pattern. By leveraging copy pasting code and design patterns properly, it is possible to enjoy the many benefits of copy pasting while avoiding it pitfalls, discussed in the next article.
Stay tuned. Tuning off…