> From: Pops
> 
> Has anyone, some web resource, some blog, something, 
> discussed recommendations,  standard practice for JavaScript 
> coding styles?
> 
> Like in C world, we use Hungarian notation.
> 
>       szAnything    -   a null terminated string
>       fSomething    -   a flag or true/false value
>       iSomething     -   an integer (negative/positive) number
>       ulSomething  -    an unsigned long integer

You'll see a lot of this in Acrobat PDF JavaScript, with an o prefix for
Objects, s for Strings. In most cases I find that annoying.

Are you familiar with the difference between Apps Hungarian and Systems
Hungarian?

Apps Hungarian uses the prefix to tell you something interesting and useful
about the *purpose* of the variable. Systems Hungarian follows strictly the
low level data type. Many Apps Hungarian enthusiasts (like me) consider
Systems Hungarian to be a Bad Thing.

The Acrobat convention is like Systems Hungarian, following the data type
instead of telling something more useful about the object.

> In the OOPs world, its been mixed, hungarian and/or  
> "spelling things out".
> 
>      btnSignOn        -  a Signon Button
> 
> but some would argue, "Signon" is good enough.

Depends on the context. In a short function that only deals with with that
object, I may not bother with the prefix. (The shorter the function, the
less the need for long names.) But suppose you have a function that deals
with closely related elements:

   textSearch - a search field
   btnSearch - the "Search" button for that field

Then it can start to get useful.

> At times, due my old FORTRAN days,  I even use the i - n 
> concept to denote a number:
> 
>          iTotal   -  number
>          nTotal   -  number
> 
> and again, some would argue "Total" is good enough.

I'm a big fan of "i" and "n", but I wouldn't use them like that. I use "i"
and "n" like Apps Hungarian, where "i" is an index and "n" is the total. In
the simple case:

   for( var i = 0, n = someArray.length;  i < n;  ++i ) {
   }

Or when there is more than one thing:

   for( var iRow = 0, nRows = rows.length;  iRow < nRows;  ++iRow ) {
      var row = rows[iRow];
      for( var iCol = 0, nCols = row.cols.length;  iCol < nCols;  ++iCol ) {
         var col = cols[iCol];
      }
   }

In that code, when I see iRow and nRows vs. iCol and nCols, I know right
away which are the related values and what they contain.

> In JS, we already have a few pseudo-standard nomemclature, 
> for example a popular patten of starting functions with a 
> lowercase letter:
> 
>    function initProcess()
> 
> I personally prefer using capitalized words for 
> processes/functions and lower case words for data.

Don't! In JavaScript, it's a standard convention to use initial uppercase
for constructors and initial lowercase for all other functions. Initial
uppercase will make everyone think you plan to use the function as a
constructor.

And finally, to bring this on topic :-) here is a jQuery-specific naming
convention that I highly recommend. Use an initial $ on every variable that
contains a jQuery object:

   var $foo = $('#foo'), foo = foo[0];

   $foo.show();  // jQuery

   var id = foo.id;  // DOM

It's always a good idea to cache a jQuery object instead of repeating the
query or $() call over and over again, and the $ prefix is a good visible
reminder that it's a jQuery object.

-Mike

Reply via email to