Function, variable et al names

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andrew Poulos

    Function, variable et al names

    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
  • Evertjan.

    #2
    Re: Function, variable et al names

    Andrew Poulos wrote on 29 dec 2004 in comp.lang.javas cript:[color=blue]
    > 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.[/color]

    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)

    Comment

    Working...