How Technology Choice Can Impact Your Design
Whenever we consider using a new framework, library, or technology, we always take a look to see what features it has, how fast it is, how it impacts our memory footprint, etc. We tend to focus on how the technology meets our current needs but sometimes forget to consider how the usage of this technology will impact the design of our system going forward. Whenever we incorporate something new, we also adjust how we do things to match what the technology is expecting. If you don’t, most often you will seriously regret it later when you have to jump through fiery hoops to get simple things to work.
When dealing with the client layer, this becomes a highly visible change to your system...not by the user but by anyone looking under the hood. For example, when you start using Prototype, you will most likely also start using a lot of ids in your DOM. We do this because Prototype provides us with the handy $(“id”) function, which encourages us to reference individual elements. This also allows us to continue writing the rest of our JavaScript in a procedural manner. The entire library is designed around working on individual elements in the DOM and not groups. Sure they provide $$ for CSS selectors and the invoke function for dealing with arrays, but whenever I use Prototype I feel like this was an afterthought.
Although being a more verbose way to write JavaScript, it allows us to write in much the same manner that we do on the back end. Whether your system is Java, .NET, or Ruby, the vast majority of your developers are probably not very functionally oriented. This style is also largely due to Prototype’s origins with Ruby on Rails. Prototype works in a very Ruby-esque manner in many ways, such as how it enhances existing DOM elements instead of just encapsulating them and providing new objects.
When we work with jQuery on the other hand, we see a complete 180 on the approach. There is no distinction between a single element or many. Everything is accessed through a CSS selector, so you no longer feel the need to reference DOM elements individually. Also, DOM elements are not enhanced, they are encapsulated by the jQuery object. When we use the $ function, we always get a jQuery object back, so there is little need to check for results or loop through the elements. We simply invoke methods directly on the results and it works the same whether there were 0, 1, or a thousand objects returned. The entire framework is designed around the however-many concept, which encourages us to think differently about the DOM and our JavaScript.
Because of the CSS-selector-only approach, I find myself using very few ids at all and the way my functions end up is completely different than it would be if I had used Prototype. I end up taking more time to analyze the DOM to keep it cleaner and more lightweight. I make very heavy use of CSS selectors and when I use the “colon” attributes, I end up eliminating the need to perform checks on the returned objects. All in all, my DOM and JavaScript end up being much more condensed when I write with that mindset.
I am not saying that one approach is better than the other, just that it drives many other decisions and the way other aspects of the system will be implemented. The next time you find yourself evaluating a new technology, remember not just to look at how it meets your needs, but what impact it will have on the way you do things going forward.