code | 1-21-2011
An abstraction layer is basically code that extends the functions that do the heavy lifting. It is used so that you don't call the code that is doing the work directly. For example, if you are using jQuery, and you want to make an Ajax call, instead of firing the $.ajax() function, you would call a different function, like
myApplication.ajax() which turns around and calls
There are obviously some drawbacks to this approach, but there are also benefits. Here are the benefits as I see them:
myApplication.ajax()and they expect to do an Ajax call.
- Since the modules don't know what library is being used, you can easily switch out the library that you are using. Is Prototype not cutting it for you? Just switch out the code in the abstraction layer and you are suddenly using jQuery... and your modules are never the wiser!
- You get more control over individual library functions. Let's say that the jQuery DOM manipulation is exactly what you need, but you need something a little different for Ajax calls. You could even use the abstraction layer to point to your own homespun solutions.
- This approach makes your code more loosely coupled, which is typically a good thing.
However, with the benefits come the drawbacks. Here are some of the drawbacks I've though of:
- There is always the chance that things will get messy. When you write your code, you may know that myApplication.save() does an Ajax call to save the information inside a form to the server, but what about the developers who come after you? Also, without proper training, other developers will just write code the "normal" way on top of yours, making you have an ugly mixture of abstracted code and direct calls to the library functions.