I have heard a lot of people suggest when designing software that it is a good practice to build an abstraction layer around calls to a third-party API libraries.
So, if I understand this correctly, say for example I am building a web-application with jQuery. I need to build an abstract layer around jQuery and have the rest of my application code use my abstraction API vs. direct calls to jQuery.
My questions:
Thanks in advance!
jQuery is an highly usable API, designed to work very effectively at the "application code" level. It should not need abstracting & you will not benefit from doing so whatsoever.
You may find use in building some "library components" or a small amount of library code on top of it -- factoring out application-level usage of repeating UI components & patterns, specific to your app.
In general, the advice to "abstract everything" sounds mostly erroneous. Abstraction is used for things you will, or might, have to make interchangeable. Abstraction where not needed is misguided, not useful & nothing but a cost otherwise.
jQuery is already an abstraction layer, so whoever told you this doesn't know the first thing about what they're talking about.
Rather than abstraction, a more common situation -- with other libraries than jQuery -- is that they are designed at a "library implementation" or SPI level, below the level at which your application code operates.
In this situation it is normally useful to build your own library code/ classes -- such as readers, writers, builders, and other "task-oriented" classes -- to be used by your application code with an application-level API, and drive down to the library. But again, this is not abstraction.
The three most useful places to actually use abstraction are:
If you're building a reasonably complex web-application (UI, interaction, and data heavy) jQuery alone, which is essentially just a very good DOM manipulation and AJAX library, will not provide you the tools you need to structure your client-side code in a maintainable way.
I think the abstractions you've heard people talking about are the various client side frameworks which have started appearing in the last few years such as Backbone, Knockout, Angular and Ember, most of which use (or can use) jQuery for the stuff it's good at, principally DOM manipulation.
Any one of these frameworks will provide you the tools you need to structure your code in a modular and maintainable way, without you having to reinvent the wheel. If your web application is at all complex (or likely to become complex over time) I would strongly advise you to consider one of the above as a starting point.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With