Wednesday, August 10, 2011

jQuery Naming Conventions: Don't Prefix Variables With $

When working with jQuery, I hate it when developers prefix their variables with dollar signs. For example, var $buttons = $('.button');. In my opinion the dollar sign really hurts the readability of the code.

Some developers claim that adding the dollar sign is a good naming convention because it shows that the value represents a jQuery object. If that's the case, then everybody should start using Hungarian notation all of the time. Gross.

The one exception I might make is for var $this = $(this);, although even then I would rather prefer something like var me = $(this);.

Long story short, don't prefix your variables with a dollar sign. Besides, it's not like you're working with PHP or anything.

3 comments:

  1. Agree on the naming convention, but disagree on the exception. var $this could lead to some problems if you're not paying attention. var me = $(this) is much better.

    ReplyDelete
  2. I was actually looking for this resource a few weeks back. Thanks for sharing with us your wisdom.This will absolutely going to help me in my projects .


    ----------------------------------------------------------
    Flower Girl Dresses|Empire Wedding Dresses|New Style Wedding Dresses

    ReplyDelete
  3. Thanks for sharing your info. I really appreciate your efforts and I will be waiting for your further write ups thanks once again.
    html5 player

    ReplyDelete