Loose Coupling

I’m enjoying reading Nicholas Zakas’s Professional JavaScript for Web Developers – in particular chapter 24 – “Best Practices.”  One of the places where I would like to do a better job is in bringing some of the loose coupling techniques I’ve used at previous shops to my new work.  In the realm of Web Development, Zakas’s assertions include:

Decouple HTML/JavaScript

“One of the most common types of coupling is HTML/JavaScript coupling… each represent a different layer of the solution: HTML is the data, and JavaScript is the behavior.  Because they are intended to interact, there are a number of different ways to tie these two technologies together…. JavaScript that appears inline in HTML…is too tightly coupled….. HTML and JavaScript can also be too tightly coupled when the reverse is true.”

“This…has to do with keeping the layers separate and being able to easily identify the source of errors.”

“Markup can be included and hidden when the entire page is rendered….  Another approach is to make an Ajax request to retrieve additional HTML to be displayed.”

Decouple Application Logic/Event Handlers

“An event handler should interrogate the event object for relevant information and pass that information to some method that handles the application logic.” (This makes it easier to debug if the problem is in the event handler or in the application method – and you can invoke the application method independently.)

One thought on “Loose Coupling”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s