About Me

My photo
Experienced Web Developer using C#, ASP Classic (VBScript) and ASP.NET, MySQL, T-SQL, and other SQL variants, JavaScript (W3Schools Certified and very well versed in jQuery and learning Dojo), and XML. Heavy interest in JavaScript, framework creation on various language platforms, and keeping up with the best industry-accepted practices.

Monday, September 14, 2009

Let's keep it DRY, folks.

If there's one thing I've come to love more than anything else in programming, it's the concept of DRY (Don't Repeat Yourself).

Seems like common sense, doesn't it?  I mean...really -- DUH!

As common sense as it may seem, there are a lot of people out there who do repeat themselves.  In fact, I used to be one of them!  I have been spending a little extra time lately sitting down and seeing what I can functionalize because what may initially seem like added work from the tried and true copy-paste method, can actually save you a ton of time not only in later stages of development, but also in instances where you need to debug and make additions to your applications.

Let's look at the run down on DRY coding in JavaScript and why it's useful.

As any programmer with even a little bit of experience can tell you, requirements for projects are normally constantly changing.  It's because of this constant change that the DRY style of programming really shines.  As the criteria change, if you weren't using DRY methods of coding, you would have to make changes in multiple locations.  Keeping it DRY allows you to make a single change and be done with the modification.

Let's face it-- everybody hates debugging.

Imagine you're debugging an application that's a few thousand lines in code. You spend an hour or so on it and finally find the issue that's been causing it all.  You tried to call the getElementById method of documnet, which is undefined.

Too bad you copied/pasted this functionality into about ten different places...

Now imagine that you utilized a functionalized method in those ten places.  You find the error sooner because you're digging through less lines of code, and when you find it, you only correct it in one place and then you're done!  The same goes for making changes to that functionality.  You make the addition in one place and you're done!


A really good way to implement DRY programming is by creating or using pre-built JavaScript libraries.
Using a JavaScript library, you utilize reusable, functionalized code that can really decrease dev time, increase performance and make your code look a lot cleaner.
There are some really good libraries out there.
Some ones you may want to look into are (in no particular order):
I use jQuery.  It was the first library I stumbled upon and it's suited my needs perfectly.  Each library has its strengths and its weaknesses, so be sure to check them all out before deciding which one to use.
John Resig also wrote a very good blog about Building A JavaScript Library

The concept of DRY can easily be adapted to any programming language.  The reason I chose to talk about it in JavaScript is because that is the language I am most familiar with, but the logic is sound in any language that can utilize functionalized code.

Do you have any good examples of DRY programming?  All languages welcome!

No comments:

Post a Comment