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. Soooooo, it's solely based in your opinion.... ok

    ReplyDelete
  3. I appreciate your opinion and apparently is the better way to maintain a clean code.

    ReplyDelete

Note: Only a member of this blog may post a comment.