Glue code

Glue is what holds your application together. Code needs to talk to databases, Javascript is calling your PHP functions using Ajax. When two systems, or different parts of the same system need to talk to each other, you end up with glue. It (hopefully) doesn't have any business logic. It's used or copied by at least a few different parts of your system, perhaps even in different projects in the same organisation.

Glue code holds things together. The only problem is that glue is not your application. Unless you're developing a framework, operating system or perhaps something that needs plugins, glue is not your strength. There are always exceptions but if you are writing glue, especially the same glue for the 3rd or 4th time, then you're not directly working on the application you are building.

Having said that, your project won't survive without glue. So how do you keep everything together?

  • Recognise glue. The first step is to understand that you are now writing glue code and not working on business requirements. For example, say you are writing a script to process files that are uploaded to your server. Discovering the files is glue, the processing of then is specific to your application.
  • Standardise across your organisation. Glue may start out as a one off but as time moves on it will become apparent which pieces are worth time improving and which aren't.
  • Make it as simple to use as possible. It will need to be understood by a larger group of developers than your other code. Put that extra time into documentation and code beautification.
  • Make it as robust as possible. You don't want this getting in the way of your 'actual' work.
  • Use 3rd party glue. Someone has nearly always been before you. Wiring your PHP and JS together with AJAX? It's been done. Creating a DB access class? Been there, done that. If you think you can do better your probably wrong. You may write better code, but you probably won't document or test it as thoroughly as an open source project has already been.

In conclusion, you want to keep glue simple and straight forward. Recognise what is and isn't glue in your application. Share the glue you have with every one in your team, and if your brave, the outside world.

The idea of glue is described in Eric Raymond's excellent book The art of unix programming.

Working with select boxes using JQuery

Using standard javascript, working with select boxes is uncomfortable, at best. Typically you want to see which value has been selected. How many times have you written the following code and wished there was a quicker way.

var selObj = forms.surveyform.selectBox;
value = selObj.options[selObj.selectedIndex].value;

jQuery makes this much simpler.

There is the standard val() function which you can use on any form field. The basic premise of jQuery is that you first select an html element and then perform an action on it.

<select name='selectBox'>
...
var value = $('select[name=selectBox]').val();

If you want to make this simpler, you can give the select box an id.

<select name='selectBox' id='selectBox'>
...
var value = $('#selectBox').val();

Another common need is to get the label of the selected item i.e. what the user can see. In this case you need to select the selected option, and get the text value of it.

var label = $('#selectBox :selected').text();

This is just one example of how to use a javascript framework to be more productive.

 
Rss Feed Tweeter button Facebook button Technorati button Reddit button Myspace button Linkedin button Delicious button Digg button Stumbleupon button