Pages

Friday, November 08, 2013

Advantages of Patterns

Why do we use Patterns? Here are some advantages:
  1. Patterns help in identifying minor issues that can cause serious issues during the software development process
  2. Patterns provide generalized solution, not specific to one problem
  3. Patterns help to make the code more DRY
  4. Patterns add to a developers vocabulary, which makes communication faster
  5. Patterns that are commonly used can be improved over time, making it more efficient
We use patterns every day. To understand how helpful patterns can be, lets review a simple element selection problem that jQuery solves for us.

Suppose we need to get the count of all the elements in the DOM with class 'foo', we can use a number of methods:
  1. Select all of the elements in the page and then store references to them. Next, filter this collection and use regular expressions (or another means) to only store those with the class "foo".
  2. Use a modern native browser feature such as querySelectorAll()
  3. Use native features such as getElementsByClassName()
But in jQuery, we can simply use $("selector"). This is significantly more easy to use for selecting HTML elements on a page versus having to manually handle opt for. This provides the most optimized way for accessing all the elements having a particular class or characteristics.

No comments:

Post a Comment