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

Unexpected use of ++

Expand Messages
  • grahamj_42
    var i; for ( i=0 ; i
    Message 1 of 8 , Dec 12, 2010
    • 0 Attachment
      var i;

      for ( i=0 ; i<10 ; i++ )
      {
      /* some code */
      }

      The i++ here is straight from K&R C, and so should be expected! I haven't found a previous remark about this in the group, so is it a new feature?
    • Harry Whitfield
      ... From http://www.jslint.com/lint.html ++ and -- The ++ (increment) and -- (decrement) operators have been known to contribute to bad code by encouraging
      Message 2 of 8 , Dec 12, 2010
      • 0 Attachment
        grahamj_42 <grahamj42@...> wrote:

        > var i;
        >
        > for ( i=0 ; i<10 ; i++ )
        > {
        > /* some code */
        > }
        >
        > The i++ here is straight from K&R C, and so should be expected! I haven't found a previous remark about this in the group, so is it a new feature?


        From http://www.jslint.com/lint.html

        ++ and --

        The ++ (increment) and -- (decrement) operators have been known to contribute to bad code by encouraging excessive trickiness. They are second only to faulty architecture in enabling to viruses and other security menaces. There is a plusplus option that prohibits the use of these operators.



        for (i = 0; i < 10; i += 1)
        {
        // some code
        }

        works just as well.

        Harry.
      • Brant Gurganus
        ... Hash: SHA512 ... Besides what grahamj_42 indicated, citing a reference for a different language doesn t make sense. If such a thing existed, cite K&R
        Message 3 of 8 , Dec 12, 2010
        • 0 Attachment
          -----BEGIN PGP SIGNED MESSAGE-----
          Hash: SHA512

          On 12/12/2010 04:53 PM, grahamj_42 wrote:
          > The i++ here is straight from K&R C, and so should be expected! I
          > haven't found a previous remark about this in the group, so is it a new
          > feature?
          >

          Besides what grahamj_42 indicated, citing a reference for a different
          language doesn't make sense. If such a thing existed, cite K&R
          JavaScript instead. However, even for C, K&R is a dated source that I've
          seen encourage bad practices. However, that's a discussion for some
          other list.
          -----BEGIN PGP SIGNATURE-----
          Version: GnuPG v2.0.16 (GNU/Linux)
          Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

          iQIcBAEBCgAGBQJNBXJyAAoJEBu6+LqX8zdcxEoP/2xAEFxH/L69i9FVLfzH/Xzl
          nzKJhQmJ9xFMxAxe0TVWN/z+KtBJAPTQpqv7S5v3BKvGMNhfNhNTdKE0eGJHQucK
          w/FucSmsN86TpUALfH2bMUrsP9gd7gZu1uqPMFaiG4j1iUsPFkL3TWFPXD3Gl+nl
          TQnHw1HCYhbmfM95t3FRD96fQXDYzabLw10KyxOZ5tNHsg6XmLcw+n4MTv1tw1EM
          ct6ZfASwYMx4JXJYOOM9ci1VGf7noTz8PPopLZBD10HMAJq4nEO3AR0YtxjqWRPD
          884zCSIUWVHoB+n0lkksmDVyhVkSUhSIG5z5IIHGVYOQObSYdT9GO6Yk391LRFpy
          KbWVYwMi8R/DgLcCvV2T5ui6gsM/IuWn099X/QFKceuMVb647RUIvQJSz2ZAWqiB
          4LcToNpWnZQHPRxcphfGoufdbyJt3tRcgoZX0xjH5zyTPLnYYCH23VEzYJRguTGb
          +SGbM0rJ1jfjRAxSf0CN3aZRP3QQQCSZ7QnOeWWxbP1NRHcyi13MmUHrKi0KBZ7o
          Y398aAsjDDWiGkESe4u24MOBfKbH6sUb1q+D+cOB8ABZiODJrxalxpGKq3a0IhtD
          4Hyr83GHQ6LBZ/wXzCimGSwyBK2X6umBvzdYQZNZNK6DarguYDLCyKWsUmXdRX8O
          uaFoYugZzreLk2Rhl3Pm
          =ua5e
          -----END PGP SIGNATURE-----



          [Non-text portions of this message have been removed]
        • Jean-Charles Meyrignac
          ... Everybody knows that this is bad ! In fact, it should be: for ( i=0 ; i
          Message 4 of 8 , Dec 12, 2010
          • 0 Attachment
            On Sun, Dec 12, 2010 at 10:53 PM, grahamj_42 <grahamj42@...> wrote:

            >
            >
            > var i;
            >
            > for ( i=0 ; i<10 ; i++ )
            > {
            > /* some code */
            > }
            >
            >
            Everybody knows that this is bad !
            In fact, it should be:

            for ( i=0 ; i<10 ; ++i )
            {
            /* some code */
            }

            And yes, I'm an old C coder, and, a long time ago, compilers were more
            efficient with ++i than with i++. In this case, an useless read of the i
            variable was generated.

            Frankly, I don't see how it's bad.
            Of course, ++ in expressions is terrible (like if (i++ > 10), or
            a=(i++>10)?1:0;), but when there is no confusion, I find this very elegant.

            JC


            [Non-text portions of this message have been removed]
          • Dominic Mitchell
            On Mon, Dec 13, 2010 at 1:46 AM, Jean-Charles Meyrignac
            Message 5 of 8 , Dec 12, 2010
            • 0 Attachment
              On Mon, Dec 13, 2010 at 1:46 AM, Jean-Charles Meyrignac <
              jcmeyrignac@...> wrote:

              > On Sun, Dec 12, 2010 at 10:53 PM, grahamj_42 <grahamj42@...> wrote:
              >
              > >
              > >
              > > var i;
              > >
              > > for ( i=0 ; i<10 ; i++ )
              > > {
              > > /* some code */
              > > }
              > >
              > >
              > Everybody knows that this is bad !
              >

              "everybody"?


              > In fact, it should be:
              >
              > for ( i=0 ; i<10 ; ++i )
              > {
              > /* some code */
              > }
              >
              >
              This also fails to pass muster with JSLint.


              > And yes, I'm an old C coder, and, a long time ago, compilers were more
              > efficient with ++i than with i++. In this case, an useless read of the i
              > variable was generated.
              >

              This bears no relevance to JavaScript.


              > Frankly, I don't see how it's bad.
              > Of course, ++ in expressions is terrible (like if (i++ > 10), or
              > a=(i++>10)?1:0;), but when there is no confusion, I find this very elegant.
              >

              -Dom


              [Non-text portions of this message have been removed]
            • grahamj_42
              ... And where did the JavaScript for construct come from, if not from C? However, you missed the irony in my post. As anyone who has learned C finds this way
              Message 6 of 8 , Dec 13, 2010
              • 0 Attachment
                --- In jslint_com@yahoogroups.com, Brant Gurganus <brant@...> wrote:

                > Besides what grahamj_42 indicated, citing a reference for a different
                > language doesn't make sense. If such a thing existed, cite K&R
                > JavaScript instead. However, even for C, K&R is a dated source that I've
                > seen encourage bad practices. However, that's a discussion for some
                > other list.

                >

                And where did the JavaScript 'for' construct come from, if not from C?

                However, you missed the irony in my post. As anyone who has learned C finds this way of writing a loop natural, 'i++' is to be expected, and if JSLint doesn't like this perfectly unambiguous expression, it should criticise it in different terms.
              • Jakob Kruse
                I for one think the message/error is pretty clear. You turned on an option to disallow the use of ++ . Then you used ++ . That seems unexpected to me. It
                Message 7 of 8 , Dec 13, 2010
                • 0 Attachment
                  I for one think the message/error is pretty clear. You turned on an option to disallow the use of '++'. Then you used '++'. That seems 'unexpected' to me.


                  It is not the way in which you use '++' that is unexpected, but that you use it at all.


                  /Jakob



                  _____

                  From: grahamj_42 [mailto:grahamj42@...]
                  To: jslint_com@yahoogroups.com
                  Sent: Mon, 13 Dec 2010 15:55:32 +0100
                  Subject: Re: [jslint] Unexpected use of ++






                  --- In jslint_com@yahoogroups.com, Brant Gurganus <brant@...> wrote:

                  > Besides what grahamj_42 indicated, citing a reference for a different
                  > language doesn't make sense. If such a thing existed, cite K&R
                  > JavaScript instead. However, even for C, K&R is a dated source that I've
                  > seen encourage bad practices. However, that's a discussion for some
                  > other list.

                  >

                  And where did the JavaScript 'for' construct come from, if not from C?

                  However, you missed the irony in my post. As anyone who has learned C finds this way of writing a loop natural, 'i++' is to be expected, and if JSLint doesn't like this perfectly unambiguous expression, it should criticise it in different terms.




                  [Non-text portions of this message have been removed]
                • Douglas Crockford
                  ... K&R is an excellent book, but it is about an entirely different language. If you are going to be working with JavaScript, you need relevant documentation.
                  Message 8 of 8 , Dec 13, 2010
                  • 0 Attachment
                    --- In jslint_com@yahoogroups.com, "grahamj_42" <grahamj42@...> wrote:
                    >
                    > var i;
                    >
                    > for ( i=0 ; i<10 ; i++ )
                    > {
                    > /* some code */
                    > }
                    >
                    > The i++ here is straight from K&R C, and so should be expected! I
                    > haven't found a previous remark about this in the group, so is it a
                    > new feature?

                    K&R is an excellent book, but it is about an entirely different language. If you are going to be working with JavaScript, you need relevant documentation.

                    One of the premises of a code quality tool is that there are features in languages that are problematic. The problem with bad parts isn't that they are useless, but that they are dangerous. They should be avoided if there are safer alternatives.

                    In my opinion, ++ and -- are dangerous. They have been implicated in some of the worst OS security bugs. And they seem to confuse people who sometimes use i++ when the mean ++i (as you just did). Confusion leads to bugs.

                    So JSLint has an option to warn when they are used. If you are wise, you will change your code to i += 1. But if you choose not to, then stop turning that option on.
                  Your message has been successfully submitted and would be delivered to recipients shortly.