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

Re: [PBML] code comments...

Expand Messages
  • Jenda Krynicky
    From: Nathan.Jeffrey@dhs.vic.gov.au ... I disagree. I think it s much cleaner, safer and easier to maintain if you only declare your variables before you use
    Message 1 of 9 , Nov 3, 2002
      From: Nathan.Jeffrey@...
      > That looks like pretty clean code to me. Only two suggestions.
      >
      > 1. Have you thought about declaring all variables at the top of your
      > code?
      > It's a habit I got into while studying CS, and I've found it aids
      > readability.

      I disagree.

      I think it's much cleaner, safer and easier to maintain if you only
      declare your variables before you use them and in the smallest
      possible scope. Therefore you not only make sure you do not
      accidentaly use them somewhere where you should not, but it's also
      much easier to take a snipet of code, copy it elsewhere and modify.

      It's hard to give examples since this starts to look different only
      if the code gets longer, but basicaly I prefer the way Mr. Prakash
      uses.


      IMHO the only variables that should be declared on top of your
      scripts are thos that you really meant to be global. (And you should
      only have as many as strictly necessary.)

      The code looks good. I'd have only one comment.

      Most often he uses

      if (condition) {
      commands
      }

      but sometimes

      if (condition)
      {commands}

      I think it would be better to stick with the first formating always.

      Jenda
      ===== Jenda@... === http://Jenda.Krynicky.cz =====
      When it comes to wine, women and song, wizards are allowed
      to get drunk and croon as much as they like.
      -- Terry Pratchett in Sourcery
    • Nathan.Jeffrey@dhs.vic.gov.au
      That s interesting. It really wouldn t be an issue in perl, where declaring your variables first doesn t matter so much, as using my() will always make sure
      Message 2 of 9 , Nov 3, 2002
        That's interesting. It really wouldn't be an issue in perl, where
        declaring your variables first doesn't matter so much, as using my() will
        always make sure you're in the correct scope anyway. So your point has a
        lot of merit. There's no reason not to do it your way.

        Excepting that old habits die hard, I guess :-)

        N





        "Jenda Krynicky" <Jenda@...>
        04/11/2002 08:54 AM
        Please respond to perl-beginner


        To: perl-beginner@yahoogroups.com
        cc:
        Subject: Re: [PBML] code comments...


        From: Nathan.Jeffrey@...
        > That looks like pretty clean code to me. Only two suggestions.
        >
        > 1. Have you thought about declaring all variables at the top of your
        > code?
        > It's a habit I got into while studying CS, and I've found it aids
        > readability.

        I disagree.

        I think it's much cleaner, safer and easier to maintain if you only
        declare your variables before you use them and in the smallest
        possible scope. Therefore you not only make sure you do not
        accidentaly use them somewhere where you should not, but it's also
        much easier to take a snipet of code, copy it elsewhere and modify.

        It's hard to give examples since this starts to look different only
        if the code gets longer, but basicaly I prefer the way Mr. Prakash
        uses.


        IMHO the only variables that should be declared on top of your
        scripts are thos that you really meant to be global. (And you should
        only have as many as strictly necessary.)

        The code looks good. I'd have only one comment.

        Most often he uses

        if (condition) {
        commands
        }

        but sometimes

        if (condition)
        {commands}

        I think it would be better to stick with the first formating always.

        Jenda
        ===== Jenda@... === http://Jenda.Krynicky.cz =====
        When it comes to wine, women and song, wizards are allowed
        to get drunk and croon as much as they like.
        -- Terry Pratchett in Sourcery



        Unsubscribing info is here:
        http://help.yahoo.com/help/us/groups/groups-32.html

        Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/







        _________________________________________________________________________________

        This email contains confidential information intended only for the person named above and may be subject to legal privilege. If you are not the intended recipient, any disclosure, copying or use of this information is prohibited. The Department provides no guarantee that this communication is free of virus or that it has not been intercepted or interfered with. If you have received this email in error or have any other concerns regarding its transmission, please notify Postmaster@...
        _________________________________________________________________________________
      • Franki
        I was under the impression that any variables declaried in the body of your document, (even my variables) acted essentially as globals. since they are
        Message 3 of 9 , Nov 4, 2002
          I was under the impression that any variables declaried in the body of your
          document, (even "my" variables)
          acted essentially as globals. since they are associated in the main
          namespace and not in enclosing blocks.

          ie if you declair a "my" variable near the top of your script, outside of
          any enclosing blocks, it will be available
          everywhere in the script, even subs and such, much as a real global would
          be...

          So I thought the reason you should only declair vars where they are used, is
          so they go out of scope as soon as the code using it in its enclosing blocks
          is finished. thereby freeing up the memory used by that var once its not
          needed anymore.

          is that an incorrect assumption???


          rgds

          Frank

          -----Original Message-----
          From: Nathan.Jeffrey@...
          [mailto:Nathan.Jeffrey@...]
          Sent: Monday, 4 November 2002 6:22 AM
          To: perl-beginner@yahoogroups.com
          Subject: Re: [PBML] code comments...


          That's interesting. It really wouldn't be an issue in perl, where
          declaring your variables first doesn't matter so much, as using my() will
          always make sure you're in the correct scope anyway. So your point has a
          lot of merit. There's no reason not to do it your way.

          Excepting that old habits die hard, I guess :-)

          N





          "Jenda Krynicky" <Jenda@...>
          04/11/2002 08:54 AM
          Please respond to perl-beginner


          To: perl-beginner@yahoogroups.com
          cc:
          Subject: Re: [PBML] code comments...


          From: Nathan.Jeffrey@...
          > That looks like pretty clean code to me. Only two suggestions.
          >
          > 1. Have you thought about declaring all variables at the top of your
          > code?
          > It's a habit I got into while studying CS, and I've found it aids
          > readability.

          I disagree.

          I think it's much cleaner, safer and easier to maintain if you only
          declare your variables before you use them and in the smallest
          possible scope. Therefore you not only make sure you do not
          accidentaly use them somewhere where you should not, but it's also
          much easier to take a snipet of code, copy it elsewhere and modify.

          It's hard to give examples since this starts to look different only
          if the code gets longer, but basicaly I prefer the way Mr. Prakash
          uses.


          IMHO the only variables that should be declared on top of your
          scripts are thos that you really meant to be global. (And you should
          only have as many as strictly necessary.)

          The code looks good. I'd have only one comment.

          Most often he uses

          if (condition) {
          commands
          }

          but sometimes

          if (condition)
          {commands}

          I think it would be better to stick with the first formating always.

          Jenda
          ===== Jenda@... === http://Jenda.Krynicky.cz =====
          When it comes to wine, women and song, wizards are allowed
          to get drunk and croon as much as they like.
          -- Terry Pratchett in Sourcery



          Unsubscribing info is here:
          http://help.yahoo.com/help/us/groups/groups-32.html

          Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/







          ____________________________________________________________________________
          _____

          This email contains confidential information intended only for the person
          named above and may be subject to legal privilege. If you are not the
          intended recipient, any disclosure, copying or use of this information is
          prohibited. The Department provides no guarantee that this communication is
          free of virus or that it has not been intercepted or interfered with. If you
          have received this email in error or have any other concerns regarding its
          transmission, please notify Postmaster@...
          ____________________________________________________________________________
          _____



          Unsubscribing info is here:
          http://help.yahoo.com/help/us/groups/groups-32.html

          Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
        • Jenda Krynicky
          From: Nathan.Jeffrey@dhs.vic.gov.au ... Using my() IS declaring. If you do my ($x, $y); .... while (something) { for (...) { my $x = ...; ... } }
          Message 4 of 9 , Nov 4, 2002
            From: Nathan.Jeffrey@...
            > That's interesting. It really wouldn't be an issue in perl, where
            > declaring your variables first doesn't matter so much, as using my()
            > will always make sure you're in the correct scope anyway.

            Using my() IS declaring. If you do


            my ($x, $y);
            ....
            while (something) {
            for (...) {
            my $x = ...;
            ...
            }
            }

            you've declared two separate variables that just happen to have the
            same name. And while it's accepted by the "compiler" it can lead to
            uncaught errors.
            (Sorry if I misunderstood what you said!)

            > Excepting that old habits die hard, I guess :-)

            That's true :-)

            Jenda
            ===== Jenda@... === http://Jenda.Krynicky.cz =====
            When it comes to wine, women and song, wizards are allowed
            to get drunk and croon as much as they like.
            -- Terry Pratchett in Sourcery
          • Jenda Krynicky
            From: Franki ... outside ... Not really. The scope of my()d variables specified in the body of the script, outside all blocks, is the
            Message 5 of 9 , Nov 4, 2002
              From: "Franki" <frankieh@...>
              > I was under the impression that any variables declaried in the body of
              > your document, (even "my" variables) acted essentially as globals.
              > since they are associated in the main namespace and not in enclosing
              > blocks.
              >
              > ie if you declair a "my" variable near the top of your script,
              outside
              > of any enclosing blocks, it will be available everywhere in the
              > script, even subs and such, much as a real global would be...
              >

              Not really. The scope of my()d variables specified in the body of the
              script, outside all blocks, is the rest of the file. And they are NOT
              accessible via the main:: namespace.

              #!perl
              my $x = 5;
              print "x=$main::x\n";
              __END__

              And the variable will be available only in the subs defined below
              the declaration, in the same file.

              > So I thought the reason you should only declair vars where they are
              > used, is so they go out of scope as soon as the code using it in its
              > enclosing blocks is finished. thereby freeing up the memory used by
              > that var once its not needed anymore.

              Yes that's one of the reasons. I tend not to care that much about
              memory most of the time, for me the reason that it makes the
              development easier and the programs safer (typos are more likely to
              get caught) is much more important. Perl is rather memory hungry
              anyway so if we did care more about the memory than the developers
              time we would be using something else.

              There is one thing you should know about ... sometimes it's better to
              widen the scope a little:


              foreach my $file (glob '*.txt') {
              $file =~ m/\.([^.]+)$/;
              my $ext = $1;
              ...
              }

              is slower than

              my $ext;
              foreach my $file (glob '*.txt') {
              $file =~ m/\.([^.]+)$/;
              $ext = $1;
              ...
              }

              The reason is that in the first case you force perl to create and
              destroy a variable in each iteration.
              If you are worried that the scope of $ext would be too big you may
              create a block just to restrict it:

              ...
              {
              my $ext;
              foreach my $file (glob '*.txt') {
              $file =~ m/\.([^.]+)$/;
              $ext = $1;
              ...
              }
              }

              [REM]
              I think this would be a good candidate for optimization ... if a
              variable is declared lexical in the main block of a loop it would not
              be destroyed and recreated for every iteration.
              [/REM]

              Jenda
              ===== Jenda@... === http://Jenda.Krynicky.cz =====
              When it comes to wine, women and song, wizards are allowed
              to get drunk and croon as much as they like.
              -- Terry Pratchett in Sourcery
            • Franki
              cool, thanks for the tip... learned my thing for the day :-) I was doing it right, but for the wrong reason.. handy to know. rgds Frank ... From: Jenda
              Message 6 of 9 , Nov 4, 2002
                cool, thanks for the tip...

                learned my "thing" for the day :-)

                I was doing it right, but for the wrong reason..

                handy to know.


                rgds

                Frank

                -----Original Message-----
                From: Jenda Krynicky [mailto:Jenda@...]
                Sent: Monday, 4 November 2002 9:31 PM
                To: perl-beginner@yahoogroups.com
                Subject: RE: [PBML] code comments...


                From: "Franki" <frankieh@...>
                > I was under the impression that any variables declaried in the body of
                > your document, (even "my" variables) acted essentially as globals.
                > since they are associated in the main namespace and not in enclosing
                > blocks.
                >
                > ie if you declair a "my" variable near the top of your script,
                outside
                > of any enclosing blocks, it will be available everywhere in the
                > script, even subs and such, much as a real global would be...
                >

                Not really. The scope of my()d variables specified in the body of the
                script, outside all blocks, is the rest of the file. And they are NOT
                accessible via the main:: namespace.

                #!perl
                my $x = 5;
                print "x=$main::x\n";
                __END__

                And the variable will be available only in the subs defined below
                the declaration, in the same file.

                > So I thought the reason you should only declair vars where they are
                > used, is so they go out of scope as soon as the code using it in its
                > enclosing blocks is finished. thereby freeing up the memory used by
                > that var once its not needed anymore.

                Yes that's one of the reasons. I tend not to care that much about
                memory most of the time, for me the reason that it makes the
                development easier and the programs safer (typos are more likely to
                get caught) is much more important. Perl is rather memory hungry
                anyway so if we did care more about the memory than the developers
                time we would be using something else.

                There is one thing you should know about ... sometimes it's better to
                widen the scope a little:


                foreach my $file (glob '*.txt') {
                $file =~ m/\.([^.]+)$/;
                my $ext = $1;
                ...
                }

                is slower than

                my $ext;
                foreach my $file (glob '*.txt') {
                $file =~ m/\.([^.]+)$/;
                $ext = $1;
                ...
                }

                The reason is that in the first case you force perl to create and
                destroy a variable in each iteration.
                If you are worried that the scope of $ext would be too big you may
                create a block just to restrict it:

                ...
                {
                my $ext;
                foreach my $file (glob '*.txt') {
                $file =~ m/\.([^.]+)$/;
                $ext = $1;
                ...
                }
                }

                [REM]
                I think this would be a good candidate for optimization ... if a
                variable is declared lexical in the main block of a loop it would not
                be destroyed and recreated for every iteration.
                [/REM]

                Jenda
                ===== Jenda@... === http://Jenda.Krynicky.cz =====
                When it comes to wine, women and song, wizards are allowed
                to get drunk and croon as much as they like.
                -- Terry Pratchett in Sourcery



                Unsubscribing info is here:
                http://help.yahoo.com/help/us/groups/groups-32.html

                Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
              • prakash
                Thank you Jenda and Nathan for providing useful suggestions.. thanks.. -prak ... http://help.yahoo.com/help/us/groups/groups-32.html Your use of Yahoo! Groups
                Message 7 of 9 , Nov 4, 2002
                  Thank you Jenda and Nathan for providing useful suggestions.. thanks..

                  -prak


                  > -----Original Message-----
                  > From: Franki [mailto:frankieh@...]
                  > Sent: Monday, November 04, 2002 5:44 AM
                  > To: perl-beginner@yahoogroups.com
                  > Subject: RE: [PBML] code comments...
                  >
                  >
                  > cool, thanks for the tip...
                  >
                  > learned my "thing" for the day :-)
                  >
                  > I was doing it right, but for the wrong reason..
                  >
                  > handy to know.
                  >
                  >
                  > rgds
                  >
                  > Frank
                  >
                  > -----Original Message-----
                  > From: Jenda Krynicky [mailto:Jenda@...]
                  > Sent: Monday, 4 November 2002 9:31 PM
                  > To: perl-beginner@yahoogroups.com
                  > Subject: RE: [PBML] code comments...
                  >
                  >
                  > From: "Franki" <frankieh@...>
                  > > I was under the impression that any variables declaried in the body of
                  > > your document, (even "my" variables) acted essentially as globals.
                  > > since they are associated in the main namespace and not in enclosing
                  > > blocks.
                  > >
                  > > ie if you declair a "my" variable near the top of your script,
                  > outside
                  > > of any enclosing blocks, it will be available everywhere in the
                  > > script, even subs and such, much as a real global would be...
                  > >
                  >
                  > Not really. The scope of my()d variables specified in the body of the
                  > script, outside all blocks, is the rest of the file. And they are NOT
                  > accessible via the main:: namespace.
                  >
                  > #!perl
                  > my $x = 5;
                  > print "x=$main::x\n";
                  > __END__
                  >
                  > And the variable will be available only in the subs defined below
                  > the declaration, in the same file.
                  >
                  > > So I thought the reason you should only declair vars where they are
                  > > used, is so they go out of scope as soon as the code using it in its
                  > > enclosing blocks is finished. thereby freeing up the memory used by
                  > > that var once its not needed anymore.
                  >
                  > Yes that's one of the reasons. I tend not to care that much about
                  > memory most of the time, for me the reason that it makes the
                  > development easier and the programs safer (typos are more likely to
                  > get caught) is much more important. Perl is rather memory hungry
                  > anyway so if we did care more about the memory than the developers
                  > time we would be using something else.
                  >
                  > There is one thing you should know about ... sometimes it's better to
                  > widen the scope a little:
                  >
                  >
                  > foreach my $file (glob '*.txt') {
                  > $file =~ m/\.([^.]+)$/;
                  > my $ext = $1;
                  > ...
                  > }
                  >
                  > is slower than
                  >
                  > my $ext;
                  > foreach my $file (glob '*.txt') {
                  > $file =~ m/\.([^.]+)$/;
                  > $ext = $1;
                  > ...
                  > }
                  >
                  > The reason is that in the first case you force perl to create and
                  > destroy a variable in each iteration.
                  > If you are worried that the scope of $ext would be too big you may
                  > create a block just to restrict it:
                  >
                  > ...
                  > {
                  > my $ext;
                  > foreach my $file (glob '*.txt') {
                  > $file =~ m/\.([^.]+)$/;
                  > $ext = $1;
                  > ...
                  > }
                  > }
                  >
                  > [REM]
                  > I think this would be a good candidate for optimization ... if a
                  > variable is declared lexical in the main block of a loop it would not
                  > be destroyed and recreated for every iteration.
                  > [/REM]
                  >
                  > Jenda
                  > ===== Jenda@... === http://Jenda.Krynicky.cz =====
                  > When it comes to wine, women and song, wizards are allowed
                  > to get drunk and croon as much as they like.
                  > -- Terry Pratchett in Sourcery
                  >
                  >
                  >
                  > Unsubscribing info is here:
                  > http://help.yahoo.com/help/us/groups/groups-32.html
                  >
                  > Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
                  >
                  >
                  >
                  >
                  >
                  > Unsubscribing info is here:
                  http://help.yahoo.com/help/us/groups/groups-32.html

                  Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/


                  ______________________________________________________
                  This email was pre-scanned by Spam Guardian
                  A Service Provided by www.foothill.net and FNFmail.com
                • Nathan.Jeffrey@dhs.vic.gov.au
                  No, you re absolutely right. Except for your use of the word document up near the top. But I m also a pedant, so ignore me :-) I usually go about declaring
                  Message 8 of 9 , Nov 5, 2002
                    No, you're absolutely right. Except for your use of the word "document"
                    up near the top. But I'm also a pedant, so ignore me :-)

                    I usually go about declaring variables like...

                    #!/path/to/perl

                    =item RCS info
                    $Header$
                    $Locker$
                    =cut

                    =item pod doco
                    blah
                    =cut

                    use whatever;

                    my $foo;
                    my $bar;

                    sub ($$); # prototype declaration

                    code;
                    code;

                    =item sub doco
                    Blah
                    =cut
                    sub ($$) {
                    my $foo_other;
                    my $bar_other;

                    code;
                    code;
                    }

                    =item RCS Log
                    $Log$
                    =cut

                    I just don't like declaring variables immediately before the line that
                    uses it, even though there's no good reason not to. I'm all for declaring
                    them in the same block, though.

                    N






                    "Franki" <frankieh@...>
                    04/11/2002 07:04 PM
                    Please respond to perl-beginner


                    To: <perl-beginner@yahoogroups.com>
                    cc:
                    Subject: RE: [PBML] code comments...


                    I was under the impression that any variables declaried in the body of
                    your
                    document, (even "my" variables)
                    acted essentially as globals. since they are associated in the main
                    namespace and not in enclosing blocks.

                    ie if you declair a "my" variable near the top of your script, outside of
                    any enclosing blocks, it will be available
                    everywhere in the script, even subs and such, much as a real global would
                    be...

                    So I thought the reason you should only declair vars where they are used,
                    is
                    so they go out of scope as soon as the code using it in its enclosing
                    blocks
                    is finished. thereby freeing up the memory used by that var once its not
                    needed anymore.

                    is that an incorrect assumption???


                    rgds

                    Frank

                    -----Original Message-----
                    From: Nathan.Jeffrey@...
                    [mailto:Nathan.Jeffrey@...]
                    Sent: Monday, 4 November 2002 6:22 AM
                    To: perl-beginner@yahoogroups.com
                    Subject: Re: [PBML] code comments...


                    That's interesting. It really wouldn't be an issue in perl, where
                    declaring your variables first doesn't matter so much, as using my() will
                    always make sure you're in the correct scope anyway. So your point has a
                    lot of merit. There's no reason not to do it your way.

                    Excepting that old habits die hard, I guess :-)

                    N





                    "Jenda Krynicky" <Jenda@...>
                    04/11/2002 08:54 AM
                    Please respond to perl-beginner


                    To: perl-beginner@yahoogroups.com
                    cc:
                    Subject: Re: [PBML] code comments...


                    From: Nathan.Jeffrey@...
                    > That looks like pretty clean code to me. Only two suggestions.
                    >
                    > 1. Have you thought about declaring all variables at the top of your
                    > code?
                    > It's a habit I got into while studying CS, and I've found it aids
                    > readability.

                    I disagree.

                    I think it's much cleaner, safer and easier to maintain if you only
                    declare your variables before you use them and in the smallest
                    possible scope. Therefore you not only make sure you do not
                    accidentaly use them somewhere where you should not, but it's also
                    much easier to take a snipet of code, copy it elsewhere and modify.

                    It's hard to give examples since this starts to look different only
                    if the code gets longer, but basicaly I prefer the way Mr. Prakash
                    uses.


                    IMHO the only variables that should be declared on top of your
                    scripts are thos that you really meant to be global. (And you should
                    only have as many as strictly necessary.)

                    The code looks good. I'd have only one comment.

                    Most often he uses

                    if (condition) {
                    commands
                    }

                    but sometimes

                    if (condition)
                    {commands}

                    I think it would be better to stick with the first formating always.

                    Jenda
                    ===== Jenda@... === http://Jenda.Krynicky.cz =====
                    When it comes to wine, women and song, wizards are allowed
                    to get drunk and croon as much as they like.
                    -- Terry Pratchett in Sourcery



                    Unsubscribing info is here:
                    http://help.yahoo.com/help/us/groups/groups-32.html

                    Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/







                    ____________________________________________________________________________
                    _____

                    This email contains confidential information intended only for the person
                    named above and may be subject to legal privilege. If you are not the
                    intended recipient, any disclosure, copying or use of this information is
                    prohibited. The Department provides no guarantee that this communication
                    is
                    free of virus or that it has not been intercepted or interfered with. If
                    you
                    have received this email in error or have any other concerns regarding its
                    transmission, please notify Postmaster@...
                    ____________________________________________________________________________
                    _____



                    Unsubscribing info is here:
                    http://help.yahoo.com/help/us/groups/groups-32.html

                    Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/




                    Unsubscribing info is here:
                    http://help.yahoo.com/help/us/groups/groups-32.html

                    Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
                  Your message has been successfully submitted and would be delivered to recipients shortly.