• What We Do
  • Our Work
  • Information
  • Smart Solutions for Smarter Business

    Writing or Reusing Code? Back to blog

    recycleReusing code is something most developers do. Like anything in programming, it can be done well or done poorly, but it’s done nonetheless. My philosophy toward reuse of code, whether it be your own, a coworker’s, or something copy-pasted from the internet, is that you should never do it blindly, and you should always strive to learn something new from it, even if it’s code you wrote yourself. Always consider if it’s a good idea to reuse that code you wrote last month or found yesterday – does it work in that situation? Is there a better way to do what you need to do?

    You should ask yourself those questions anyway during any refactoring stage of your project – but if you’re reusing or copying code, then you’re already at the refactoring stage. If the code you’re planning to plop into your project passes your first question and will work for the project, then it’s time to see if you can make it any better than it was since the first time you wrote it. Even if you already refactored that code the first time, think about what you may have learned in the weeks or months since you first wrote it that might help you further improve it.

    Of course, you’ll find cases where you can’t do any refactoring because the code is already “perfect” (read: good enough for now), you have a time constraint, or (and hopefully this isn’t the case) you’re copying code that you found and you actually don’t understand it but you know that it does what you want it to do. That last thing is very bad practice, and won’t do you any favors as a programmer. If a deadline set by an employer or client is forcing you to use borrowed code that you don’t understand, it’s time to find a new job. There is nothing wrong with using code that’s above your level though, as long as you do take the time to figure out how it works.

    This is important because not only do you have to write the code (or assemble it) in the first place, you’re most likely also going to be responsible for maintaining it. That means that if something’s going wrong and you can’t determine what because you lack knowledge of exactly how the code is working, you’re going to get frustrated rather quickly. Instead of a hectic rush and hours of frustration later, you could have a really great learning experience. Programming doesn’t mean you’re going to write all the code in your projects yourself, but it does mean you’re always going to be learning new code, and, hopefully, learning to improve it.

    By
    Tina Ramsey
    Developer

    By
    Tina Ramsey
    Developer