Function, variable et al names

Discussion in 'Javascript' started by Andrew Poulos, Dec 29, 2004.

  1. If I'm writing a sizeable chunk of javascript (1000s of lines) and it
    includes lots of custom functions, global variables, prototypes etc is
    there an agreed, customary, conventional, or best practise way to name
    them? What I'm worried about is inadvertently having a naming clash
    where, say, a function and a global variable end up being named the same.

    I have a friend who starts the name of every function with "f" eg.
    fMyFunction, and every global variable with "g" eg. gMyVariable. I'm not
    sure if this is the "smartest" way to do it.

    Should I make a separate index with each name listed with a description
    of it's purpose, usage etc?


    Andrew Poulos
    Andrew Poulos, Dec 29, 2004
    #1
    1. Advertising

  2. Andrew Poulos

    Evertjan. Guest

    Andrew Poulos wrote on 29 dec 2004 in comp.lang.javascript:
    > If I'm writing a sizeable chunk of javascript (1000s of lines) and it
    > includes lots of custom functions, global variables, prototypes etc is
    > there an agreed, customary, conventional, or best practise way to name
    > them? What I'm worried about is inadvertently having a naming clash
    > where, say, a function and a global variable end up being named the same.


    Write modular code that has its own defined local variables, and keep the
    global variables to a minimum.

    Enter the modules [=functions] with all variables as parameters and exit
    the code with all parameters in the return value. The latter is not that
    easy if there are more than one return values, but could be done with an
    array.

    That way the modules can even be reentrant.

    The hopefully very few global variables could be given special names,
    but that is not necessary,
    as there is no strict objection to duplicate variable names:

    <script type='text/javascript'>
    var v = 7; //global

    function f(){
    var v = 8; //local

    alert('local v = ' + v)
    alert('global v = ' + window['v'])
    }
    f()
    </script>

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
    Evertjan., Dec 29, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paddy McCarthy
    Replies:
    3
    Views:
    708
    Anthony J Bybell
    Sep 24, 2004
  2. Ares Lagae
    Replies:
    8
    Views:
    447
    Ares Lagae
    Sep 24, 2004
  3. wanwan
    Replies:
    3
    Views:
    429
    Alex Martelli
    Oct 14, 2005
  4. News123
    Replies:
    2
    Views:
    463
    John Machin
    Nov 26, 2008
  5. Peter Buckley
    Replies:
    5
    Views:
    199
    matt neuburg
    Feb 27, 2009
Loading...

Share This Page