As a programmer I spent the large majority of my time researching solutions to coding problems. Generally speaking it's not the solution I find, but a methodology or coding pattern that helps to piecemeal a solution together. I say a solution here because there really is no right answer when it comes to programming.
The thing is though, when working with other people's code you tend to overlook it far too quickly. The general thought process is that if it works, you don't touch it ... and you move on. In my opinion, this sort of thinking though leads to a greater long-term debt.
Imagine you're working on a complex piece of software but then you have to switch to something else for a few days or a week, or how about you go on a two-week holiday. When it's time to revisit your complex, copy-and-pasted code, you'll inevitably hit a mental brick wall.
As you start to scan over each line of code you start scratching your head thinking: "... how does this work, and why is this variable named like so, and when did I start writing my functions this way [email protected]!"
Most people might not realise this, but as a programmer a week of absence is a very long time. Some programmers even joke that after a long break that they actually have to re-learn the coding languages that they have been using for years!
The point is that if you're going to use other people's code you really have to over-compensate or massage the code. This can take you off track as you try to work the code into place. I'm not saying you shouldn't copy-and-paste code, what I am saying is this:
- Spend some extra time at the end of your session to clearly comment your new code. Describe it in as much detail as you can so your future self will thank you.
- Update variable and function names so they follow the conventions within your codebase.
- Add references to where your foreign code came from so you and other developers (your colleagues perhaps?) have some context.
Foreign code can actually look very foreign in your own code if it doesn't make sense in your specific context. Take what you need from others but try to find your own solution by building on top of it, in your own words. That way, it'll make sense to you because it was crafted from your hands!
Got any tips to add to the list? Feel free to leave a comment!