Loading ...
Sorry, an error occurred while loading the content.

Re: Enhancement Request: Identifier length restriction

Expand Messages
  • Marc Draco
    Austin may be vitriolic, but he has a point. Code Complete by Steve McConnell is my bible and it has some interesting things to say on single-character
    Message 1 of 11 , Jul 6, 2010
    • 0 Attachment
      Austin may be vitriolic, but he has a point.

      Code Complete by Steve McConnell is my bible and it has some interesting
      things to say on single-character identifiers.

      Personally, I only ever use single-character vars in short loops. For a
      single iterator, I tend to use "n" (which betrays my beginnings in an
      early BASIC); and I would generally eschew i, j, k for nested loops
      unless there's a very good reason - i and j are so easy to confuse due
      to "confirmation bias".

      x & y (and z) are a special case also - these are handy for short loop
      iterations when co-ordinate systems are in use.

      Where speed isn't paramount, I've found a short object can be very
      helpful. Using X and Y as an example, let's say I need two versions of
      each, I might create two literal, local objects like this:

      var old = {};
      var cur = {};

      and initialise some variables

      old.x = 0;
      old.y = 0;
      cur.x = old.x +10;
      cur.y = old.x +10;

      Any operations could conceivably use "with" on these but that has
      problems all of its own. Personally I never use it.

      This is far clearer than using very long names, is very easy to type and
      less error prone because we're grouping related variables. Of course,
      your mileage may vary.
    • Michael Mikowski
      Sure James. Cut and paste from our standards document: All arguments to a function have a master prefix, arg_ . Prefixes for compound variable types prefix
      Message 2 of 11 , Jul 6, 2010
      • 0 Attachment
        Sure James. Cut and paste from our standards document:

        All arguments to a function have a master prefix, 'arg_'.

        Prefixes for compound variable types

        prefix
        variable type (vtype)
        obj_ full object with methods
        hash_ object as associative array
        ary_ array
        elem[type]_ dom element object; type is optional

        $
        jquery object


        Prefixes for scalar variable type
        prefix
        scalar type (stype)
        count_ Integer counter
        idx_ Integer loop or array index
        idint_ Integer interval id
        idto_ Integer timout id
        int_
        Integer, general

        ms_ Integer millisecond
        num_
        floating point number, general

        px_ Integer pixel units
        str_ string, general

        sw_ boolean switch (true or false)
        Examples:

        A single scalar might be px_screen_x.

        If the variable is compound then the prefixes should be strung together, e.g. ary_px_screen or hash_px_screen.

        var fnRenderScreen = function ( arg_hash_px_screen ){...};

        If a function takes multiple named arguments, we use a single input hash called arg_hash_spec, e.g:

        var fnRenderScreen = function ( arg_hash_spec ){...};

        called like so:

        ret = fnRenderScreen({ sw_overlay : true, px_left : 10, px_top: 250, px_width: 150, px_height: 150 });

        Hope that helps :)

        Cheers, Mike




        ________________________________
        From: James Friedman <james.william.friedman@...>
        To: z_mikowski@...
        Sent: Mon, July 5, 2010 3:40:50 AM
        Subject: Re: [jslint] Re: Enhancement Request: Identifier length restriction

        Hello Mike,


        I am interested in the prefixes that you use. I find the use of
        prefixes to be invaluable in keeping track of what-is-what - But then
        I do often suffer from "Teflon of the brain - nothing sticks..."


        Thank you,

        - James

        On Mon, Jul 5, 2010 at 12:53 AM, Michael Mikowski <z_mikowski@...> wrote:
        >
        >
        >
        > Hi All:
        >
        > On a bit of a tangent, there are no sigils in javascript, so therefore we use RPN to indicate data types. So my implementation would look something like this:
        >
        > var
        > ary_foo = [ 'bing','bang','boom'],
        > idx_foo
        > ;
        >
        > for ( idx_foo = 0; idx_foo < ary_foo.length; idx_foo++ ){ }
        >
        > We have a comprehensive list of prefixes we currently use, if anyone is interested.
        >
        > Feedback and suggestions are welcome :)
        >
        > Cheers, Mike
        >
        > ________________________________
        > From: "abyssoft@..." <abyssoft@...>
        > To: jslint_com@yahoogroups.com
        > Sent: Sun, July 4, 2010 3:11:36 PM
        > Subject: [jslint] Re: Enhancement Request: Identifier length restriction
        >
        > That is why I would like it as a switch/option not a requirement.
        >
        > I would write
        >
        > for (var i = 0; i < foo.length; i += 1) {}
        >
        > as
        >
        > var fooIndex;
        > for (fooIndex = foo.length -1 ; fooIndex + 1 ; fooIndex -= 1) {
        > //Loop Content even if only a one liner.
        > }
        >
        > I never declare vars in loops, or in the setup of a loop.
        >
        > --- In jslint_com@yahoogroups.com, "pauanyu" <pcxunlimited@...> wrote:
        > >
        > >
        > >
        > > What about this (very) common construct:
        > >
        > > for (var i = 0; i < foo.length; i += 1) {}
        > >
        > > I agree that very short identifiers are generally a bad idea, but I also think there are a couple exceptions (like the above).
        > >
        > > --- In jslint_com@yahoogroups.com, "abyssoft@" <abyssoft@> wrote:
        > > >
        > > > Reasoning
        > > >
        > > > Identifiers of only 1 or 2 Characters can lead to errors in thought and poor readability of code. I have a preference for identifiers that consist of 3 or more characters and they are rarely that short.
        > > >
        > > > An alternate enhancement is for support of single and double letter identifiers when listed in /*global*/.
        > > >
        > >
        >
        > [Non-text portions of this message have been removed]
        >
        >


        --
        --------------------------
        Television and the media culture have replaced thoughtfulness with
        entertainment. We are being converted from a critically minded
        inquisitive population enjoying informed discourse to a nation of
        passive retards.


        [Non-text portions of this message have been removed]
      • Cheney, Edward A SSG RES USAR USARC
        ... Beautify the code using a JSLint white space compatible algorithm. Then perform the following replacements: For functions: - Manually change the name of
        Message 3 of 11 , Jul 6, 2010
        • 0 Attachment
          > For me personally it is exceedingly annoying to encounter code that
          > has nothing but 1 to 2 char identifiers and no or poor documentation.

          Beautify the code using a JSLint white space compatible algorithm. Then
          perform the following replacements:

          For functions:
          - Manually change the name of the function where it is declared
          - Find and replace on the following after ensure that each of the
          following is not in quotes:
          + space + variable name + left paren
          ex: " a("
          + space + variable name + comma
          ex: " a,"
          + space + variable name + right paren
          ex: " a)"
          + space + variable name + right square brace
          ex: " a]"
          + space + variable name + right curly brace
          ex: " a}"

          For arrays:
          - Same as above except find and replace on a left square brace
          instead of opening paren

          For object literals
          - Same as above except find and replace on a left curly brace instead
          of opening paren

          For value literals (strings and numbers)
          - Same as above except find and replace on a space, instead of an
          opening paren

          You could write a JS application to do all of this for you. This would
          be the best bet because you are not going to influence how other people
          write code their own code. The only code that you can effectively
          influence is the code under your own responsibility.

          Austin Cheney
          http://prettydiff.com/
        Your message has been successfully submitted and would be delivered to recipients shortly.