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

Re: YUI Compresser could produce shorter output

Expand Messages
  • D. Wayne Fincher
    That would be great if it weren t bad programming technique. I presume you re not suggesting that the compressor take out curly braces if the programmer did
    Message 1 of 15 , Nov 2, 2008
    View Source
    • 0 Attachment
      That would be great if it weren't bad programming technique.

      I presume you're not suggesting that the compressor take out curly
      braces if the programmer did use them.

      In which case, we're left with cases where the programmer doesn't use
      them. But I don't speak for myself here. Every "code conventions" or
      "programming standards" I've ever read suggests that curly braces
      always be used.

      --- In ydn-javascript@yahoogroups.com, "crlender" <crlender@...> wrote:
      >
      > Hello.
      >
      > Consider this (admittedly contrived) case; the first line is the
      > original input, the second line is YUI Compressor's version:
      >
      > function test(a){if(a==2)return;else func()}
      > function test(A){if(A==2){return}else{func()}};
      >
      > It would appear that YUI-Comp actually *adds* curly braces to create
      > blocks around single statements in if/else/for/while/etc where they
      > aren't strictly necessary. Since single statements are very common in
      > these structures, quite a few more bytes could be saved here. For
      > example, this loop
      >
      > for (var k in map) {
      > if (map.hasOwnProperty(k)) {
      > if (!(k in target)) {
      > target[k] = map[k];
      > }
      > }
      > }
      >
      > could be normalized to
      >
      > for (var k in map)
      > if (map.hasOwnProperty(k))
      > if (!(k in target))
      > target[k] = map[k];
      >
      > which would save 6 more bytes. Not a biggie, but maybe worth
      > mentioning, as it can add up in longer scripts.
      >
      > (The last semicolon in the first example isn't necessary either,
      > but it only occurs at the end of a file and is probably intentional,
      > to allow for safe concatenation of compressed scripts.)
      >
      >
      > - Conrad
      >
    • crlender
      ... That s exactly what I m suggesting. ... Remember, we re talking about compressed code, intended to be read by browsers, not humans. Style conventions don t
      Message 2 of 15 , Nov 2, 2008
      View Source
      • 0 Attachment
        --- In ydn-javascript@yahoogroups.com, "D. Wayne Fincher"
        <slowlychillin@...> wrote:
        > That would be great if it weren't bad programming technique.
        >
        > I presume you're not suggesting that the compressor take out curly
        > braces if the programmer did use them. [...]

        That's exactly what I'm suggesting.

        > Every "code conventions" or "programming standards" I've ever
        > read suggests that curly braces always be used.

        Remember, we're talking about compressed code, intended to be read
        by browsers, not humans. Style conventions don't apply here.
        Putting the whole script in one long line and calling our variables
        A, B, C... isn't part of our "best practices" either ;-)


        - Conrad
      • dav.glass@yahoo.com
        Conrad -- You also need to remember that the Compressor is removing the line breaks, so your example: for (var k in map) if (map.hasOwnProperty(k)) if (!(k in
        Message 3 of 15 , Nov 2, 2008
        View Source
        • 0 Attachment
          Conrad --

          You also need to remember that the Compressor is removing the line breaks, so your example:


          for (var k in map)
          if (map.hasOwnProperty(k))
          if (!(k in target))
          target[k] = map[k];

          Would result in a syntax error when line breaks are removed. Adding the brackets allows for that line to be compressed into one line without throwing errors.

          Dav

          Dav Glass
          dav.glass@...
          blog.davglass.com




          + Windows: n. - The most successful computer virus, ever. +
          + A computer without a Microsoft operating system is like a dog
          without bricks tied to its head +
          + A Microsoft Certified Systems Engineer is to computing what a
          McDonalds Certified Food Specialist is to fine cuisine +



          ----- Original Message ----
          > From: crlender <crlender@...>
          > To: ydn-javascript@yahoogroups.com
          > Sent: Sunday, November 2, 2008 3:39:43 PM
          > Subject: [ydn-javascript] Re: YUI Compresser could produce shorter output
          >
          > --- In ydn-javascript@yahoogroups.com, "D. Wayne Fincher"
          > wrote:
          > > That would be great if it weren't bad programming technique.
          > >
          > > I presume you're not suggesting that the compressor take out curly
          > > braces if the programmer did use them. [...]
          >
          > That's exactly what I'm suggesting.
          >
          > > Every "code conventions" or "programming standards" I've ever
          > > read suggests that curly braces always be used.
          >
          > Remember, we're talking about compressed code, intended to be read
          > by browsers, not humans. Style conventions don't apply here.
          > Putting the whole script in one long line and calling our variables
          > A, B, C... isn't part of our "best practices" either ;-)
          >
          >
          > - Conrad
          >
          >
          > ------------------------------------
          >
          > Yahoo! Groups Links
          >
          >
          >
        • crlender
          ... No, it wouldn t; it s perfectly legal ECMAScript. The curlies really are optional (just like in C, Java, and other languages with C-like syntax). - Conrad
          Message 4 of 15 , Nov 2, 2008
          View Source
          • 0 Attachment
            --- In ydn-javascript@yahoogroups.com, dav.glass@... wrote:
            > You also need to remember that the Compressor is removing the line
            breaks, so your example:
            >
            > for (var k in map)
            > if (map.hasOwnProperty(k))
            > if (!(k in target))
            > target[k] = map[k];
            >
            > Would result in a syntax error when line breaks are removed.

            No, it wouldn't; it's perfectly legal ECMAScript. The curlies really
            are optional (just like in C, Java, and other languages with C-like
            syntax).


            - Conrad
          • Philip Tellis
            ... Adding braces isn t purely a style convention. They re also used to ensure that the machine does what the programmer intended. Taking them out in
            Message 5 of 15 , Nov 2, 2008
            View Source
            • 0 Attachment
              c dropped bits saying:

              > Remember, we're talking about compressed code, intended to be read
              > by browsers, not humans. Style conventions don't apply here.

              Adding braces isn't purely a style convention. They're also used to
              ensure that the machine does what the programmer intended. Taking them
              out in compressed code makes for problems which are very hard to debug
              because the code works in development, but not in production.
            • dav.glass@yahoo.com
              Conrad -- I used the wrong term (syntax error), to me code that doesn t run as expected is an error ;) So not putting the braces in and compressing the line
              Message 6 of 15 , Nov 2, 2008
              View Source
              • 0 Attachment
                Conrad --

                I used the wrong term (syntax error), to me code that doesn't run as expected is an error ;)

                So not putting the braces in and compressing the line could result in code that doesn't run as you want it to.

                Dav

                Dav Glass
                dav.glass@...
                blog.davglass.com




                + Windows: n. - The most successful computer virus, ever. +
                + A computer without a Microsoft operating system is like a dog
                without bricks tied to its head +
                + A Microsoft Certified Systems Engineer is to computing what a
                McDonalds Certified Food Specialist is to fine cuisine +



                ----- Original Message ----
                > From: crlender <crlender@...>
                > To: ydn-javascript@yahoogroups.com
                > Sent: Sunday, November 2, 2008 3:51:36 PM
                > Subject: [ydn-javascript] Re: YUI Compresser could produce shorter output
                >
                > --- In ydn-javascript@yahoogroups.com, dav.glass@... wrote:
                > > You also need to remember that the Compressor is removing the line
                > breaks, so your example:
                > >
                > > for (var k in map)
                > > if (map.hasOwnProperty(k))
                > > if (!(k in target))
                > > target[k] = map[k];
                > >
                > > Would result in a syntax error when line breaks are removed.
                >
                > No, it wouldn't; it's perfectly legal ECMAScript. The curlies really
                > are optional (just like in C, Java, and other languages with C-like
                > syntax).
                >
                >
                > - Conrad
                >
                >
                > ------------------------------------
                >
                > Yahoo! Groups Links
                >
                >
                >
              • Philip Tellis
                ... Try this code: if (map.hasOwnProperty(k)) { if (!(k in target)) target[k] = map[k]; else target[k] = null; } It s still one statement inside the outer if,
                Message 7 of 15 , Nov 2, 2008
                View Source
                • 0 Attachment
                  c dropped bits saying:

                  > --- In ydn-javascript@yahoogroups.com, dav.glass@... wrote:
                  >> You also need to remember that the Compressor is removing the line
                  > breaks, so your example:
                  >>
                  >> for (var k in map)
                  >> if (map.hasOwnProperty(k))
                  >> if (!(k in target))
                  >> target[k] = map[k];
                  >>
                  >> Would result in a syntax error when line breaks are removed.
                  >
                  > No, it wouldn't; it's perfectly legal ECMAScript. The curlies really
                  > are optional (just like in C, Java, and other languages with C-like
                  > syntax).


                  Try this code:

                  if (map.hasOwnProperty(k)) {
                  if (!(k in target))
                  target[k] = map[k];
                  else
                  target[k] = null;
                  }

                  It's still one statement inside the outer if, but if you remove the
                  braces, you confuse the engine. Take out the semi colons for more fun.


                  In any event, what you're stating is a micro-optimisation that buys you
                  nothing if you also gzip your javascript before serving. All those
                  braces will compress into a single bit or something of that order.
                • Randall R Schulz
                  ... Grotesque! Redundant punctuation does not accentuate readability, and any programmer who would make mistakes that result from reading code without braces
                  Message 8 of 15 , Nov 2, 2008
                  View Source
                  • 0 Attachment
                    On Sunday 02 November 2008 15:30, D. Wayne Fincher wrote:
                    > That would be great if it weren't bad programming technique.

                    Grotesque! Redundant punctuation does not accentuate readability, and
                    any programmer who would make mistakes that result from reading code
                    without braces as if it did should either:

                    1) Find a new career.
                    2) Use Python.


                    > I presume you're not suggesting that the compressor take out curly
                    > braces if the programmer did use them.

                    Redundant is redundant. Why not remove them? Besides, the compressed
                    code is not meant for human consumption, it's mean to load ASAP.


                    > In which case, we're left with cases where the programmer doesn't use
                    > them. But I don't speak for myself here. Every "code conventions" or
                    > "programming standards" I've ever read suggests that curly braces
                    > always be used.

                    Gross. Grotesque! Hideous!!

                    Programming is about thinking. Auto-pilot "coding conventions" that try
                    to protect the programmer from his own inattention are not ultimately
                    productive.


                    Randall Schulz
                  • Randall R Schulz
                    ... Clearly a source change that alters the code s semantics is not acceptable for the compressor. Randall Schulz
                    Message 9 of 15 , Nov 2, 2008
                    View Source
                    • 0 Attachment
                      On Sunday 02 November 2008 15:52, Philip Tellis wrote:
                      > c dropped bits saying:
                      > > Remember, we're talking about compressed code, intended to be read
                      > > by browsers, not humans. Style conventions don't apply here.
                      >
                      > Adding braces isn't purely a style convention. They're also used to
                      > ensure that the machine does what the programmer intended. Taking
                      > them out in compressed code makes for problems which are very hard to
                      > debug because the code works in development, but not in production.

                      Clearly a source change that alters the code's semantics is not
                      acceptable for the compressor.


                      Randall Schulz
                    • Philip Tellis
                      ... When 900 years old you reach, look as good, you will not. Hmm?
                      Message 10 of 15 , Nov 2, 2008
                      View Source
                      • 0 Attachment
                        RRS dropped bits saying:

                        > On Sunday 02 November 2008 15:30, D. Wayne Fincher wrote:
                        >> That would be great if it weren't bad programming technique.
                        >
                        > Grotesque! Redundant punctuation does not accentuate readability, and


                        When 900 years old you reach, look as good, you will not. Hmm?

                        :)
                      • crlender
                        ... Just tested it, and it works just as expected. ... No, it s *two* statements inside the outer if . And those semicolons are not optional, so taking them
                        Message 11 of 15 , Nov 2, 2008
                        View Source
                        • 0 Attachment
                          --- In ydn-javascript@yahoogroups.com, Philip Tellis <philip@...> wrote:
                          > Try this code:
                          >
                          > if (map.hasOwnProperty(k)) {
                          > if (!(k in target))
                          > target[k] = map[k];
                          > else
                          > target[k] = null;
                          > }

                          Just tested it, and it works just as expected.

                          > It's still one statement inside the outer if, but if you remove the
                          > braces, you confuse the engine. Take out the semi colons for more fun.

                          No, it's *two* statements inside the outer 'if'. And those semicolons
                          are not optional, so taking them out *would* be a syntax error without
                          line breaks.

                          I'm not sure what you mean by the engine getting confused... I've
                          never seen a JavaScript implementation that couldn't correctly parse
                          control structures with or without redundant curlies. This is basic
                          stuff. Or maybe did you mean the compressor engine could get confused?

                          > In any event, what you're stating is a micro-optimisation that buys you
                          > nothing if you also gzip your javascript before serving. All those
                          > braces will compress into a single bit or something of that order.

                          Sure, it's not that big a deal, but the compressor's output *could*
                          be improved (even if only by a few hundred bytes per script) without
                          sacrificing code integrity. Whether the developers choose to implement
                          it that way is of course their choice.

                          Regarding gzip: if we could just assume that gzip were universally
                          available and supported, the compressor wouldn't have to go to all
                          the trouble... just strip out comments and let gzip do the rest.


                          - Conrad
                        • Randall R Schulz
                          ... Umm... Yes? Unless, perhaps, you re an acolyte of Ray Kurzweil. However, I fail to apprehend the significance of the assertion... RRS
                          Message 12 of 15 , Nov 2, 2008
                          View Source
                          • 0 Attachment
                            On Sunday 02 November 2008 16:11, Philip Tellis wrote:
                            > RRS dropped bits saying:
                            > > On Sunday 02 November 2008 15:30, D. Wayne Fincher wrote:
                            > >> That would be great if it weren't bad programming technique.
                            > >
                            > > Grotesque! Redundant punctuation does not accentuate readability,
                            > > and
                            >
                            > When 900 years old you reach, look as good, you will not. Hmm?
                            >
                            > :)


                            Umm... Yes?

                            Unless, perhaps, you're an acolyte of Ray Kurzweil.


                            However, I fail to apprehend the significance of the assertion...


                            RRS
                          • crlender
                            ... expected is an error ;) ... in code that doesn t run as you want it to. Dav, I may be misreading you, but I don t see how the example I posted could be
                            Message 13 of 15 , Nov 2, 2008
                            View Source
                            • 0 Attachment
                              --- In ydn-javascript@yahoogroups.com, dav.glass@... wrote:
                              > I used the wrong term (syntax error), to me code that doesn't run as
                              expected is an error ;)
                              >
                              > So not putting the braces in and compressing the line could result
                              in code that doesn't run as you want it to.

                              Dav, I may be misreading you, but I don't see how the example I posted
                              could be interpreted in any other way than the example with braces. The
                              ECMAScript standard specifies how these structures are to be parsed and
                              evaluated; any difference would indicate a (severe) bug in the script
                              engine.

                              Just in case, let me mention that I personally do use braces even if
                              they're not necessary, but a compressor doesn't get any bonus points
                              for readable output.


                              - Conrad
                            • D. Wayne Fincher
                              Code conventions aren t just about making code more readable . Anyone that attacks my post on that one single issue is taking part in a straw man. That is,
                              Message 14 of 15 , Nov 2, 2008
                              View Source
                              • 0 Attachment
                                Code conventions aren't just about "making code more readable".

                                Anyone that attacks my post on that one single issue is taking part in
                                a straw man. That is, instead of presenting a rebuttal based on the
                                argument at hand, you take another argument which is easier to rebut
                                and refute that one.

                                In this case, I never mentioned anything about the readability of the
                                code. And yet, a few posts only rebutted me on that point.

                                Classic straw man approach. ;)


                                --- In ydn-javascript@yahoogroups.com, "crlender" <crlender@...> wrote:
                                >
                                > --- In ydn-javascript@yahoogroups.com, dav.glass@ wrote:
                                > > I used the wrong term (syntax error), to me code that doesn't run as
                                > expected is an error ;)
                                > >
                                > > So not putting the braces in and compressing the line could result
                                > in code that doesn't run as you want it to.
                                >
                                > Dav, I may be misreading you, but I don't see how the example I posted
                                > could be interpreted in any other way than the example with braces. The
                                > ECMAScript standard specifies how these structures are to be parsed and
                                > evaluated; any difference would indicate a (severe) bug in the script
                                > engine.
                                >
                                > Just in case, let me mention that I personally do use braces even if
                                > they're not necessary, but a compressor doesn't get any bonus points
                                > for readable output.
                                >
                                >
                                > - Conrad
                                >
                              Your message has been successfully submitted and would be delivered to recipients shortly.