Thursday, January 25, 2007

Cut and Paste Code

This is just a little reminder to not be so lazy.  I know it's quick and easy to cut and paste code, especially for reports.  It saves a lot of time and you can really burn through projects that way.  But you just have to consider what you're doing.  Just because a section of code worked well before, that doesn't mean that it'll work well here.  Look before you leap.  If you want to cut and paste code, go ahead, but at least look at it to make sure it's actually what you need. 

Recently, I ran across a group of SPs that were clearly from the same template.  They all pulled 150+ cols into #tables, and used only a couple of them.  The theory behind it was the original author needed a few cols, and the rest of the authors only needed a couple, but since they were included in the select * the original guy pulled in, they decided to just leave it at that.  It's nothing but laziness people.  Look at what you need and get it.  A DB is like a buffet.  You can eat all you want, but eat all you take.  If you need logic from a query, that's fine, but you should only use that query to help you cut some of your work, not to replace it. 

So, do the work, think about what you need, and do what's right.  And another thing... cut and paste code leads to some other really bad things.  Like in the case of this code I just fixed.  Not only did the SP get cloned, it wasn't good code to begin with.  So, instead of the other report writers figuring out how to write the queries themselves, and possibly stumbling on a better way, they just pushed the same bad code out there.  Don't just assume that because code is published in the DB, that it's good.  Take the code if you need it, but only take what you can use, and definitely check it somehow to make sure it's written well to begin with.  If you don't know how to check it, there are plenty of resources.  Start with your own DBAs, and if you can't get any love there, try the internet... books... training CDs... user groups... seminars... newsgroups, etc...

1 comments:

Amma said...

Good for people to know.

About Me

My Photo
Sean McCown
I am a Contributing Editor for InfoWorld Magazine, and a frequent contributor to SQLServerCentral.com as well as SSWUG.org. I live with my wife and 3 kids, and have practiced and taught Kenpo for 22yrs now.
View my complete profile

Labels

Blogumulus by Roy Tanck and Amanda Fazani

Page Views