Writing Reusable Code

Recently for our YumDrop.com site, I was asked to put together a new page that we could use to sell products that we still had in stock which were discontinued. We wanted to be able to display only those items on the page to promote their sale. The obvious goal was to get rid of the inventory.

So as usual, I fire up Visual Studio 2005 (our web application is written in C#) and get to work. As soon as I get started putting the page together I had the following thought:

“Man, I’m glad I wrote that thumb item control to display any product item.”

What I was thinking of is a .NET control I built to display any given product on our site. This consists of the product thumb image, a link, its name, and price. All you have to do is tell the control what size image you want to display. The cool thing is this can be used ANYWHERE on our site. It can accept data from any source (database call, xml, etc.) and display it. Having such a gem saves me a lot of development time basically piecing the same code that appears on 5-6 different sections of our site.

I know what your saying now though. Everyone knows about writing reusable code. What I’m saying is that I don’t think enough developers actually do it. I think many times, as developers up against deadlines, are just trying to get our projects completed (which is an admirable goal, one I strive for every day), thus we sometimes write non-reusable code. Don’t forget to ask yourself the following each time you write some new snippet of code:

“Am I going to need this functionality somewhere else in my application or even another application tomorrow, the next day, or 5 months from now?”

I think you’d be surprised that a lot of times, the answer is yes. If you write your code so you can reuse it later, you’ll thank yourself, like I did, months later when you find yourself needing that functionality again.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.