Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Re: Re: What training do YOU need?

by demerphq (Chancellor)
on Oct 22, 2002 at 09:33 UTC ( #207012=note: print w/ replies, xml ) Need Help??


in reply to Re: Re: What training do YOU need?
in thread What training do YOU need?

But your choice is unfortunate.

Interesting comment. Id be interested in hearing more about why you think this was an unfortunate choice.

And how the C-style loop is more cumbersome but more adaptable.

A C-Style for loop is always functionally and internally equivelent to a suitably constructed while loop. The primary differences being that many programmers from a C tradition find the for(;;) syntax more readable, while the rest of us find the while syntax more readble and maintainable. I am in the latter group. :-) Nine time out of ten (more really) when I see someone use a C-Style for loop they are not exploiting the equivelence to a while , but rather using a slightly less readble, more error prone and less efficient form of a for (LIST) loop. I avoid the term foreach because foreach is a synonym to for in perl, they are identical.

E:\Smoke\source>perl -e "foreach (my $i=0;$i<10;$i++) { print $i,$/}" E:\Smoke\source>perl -e "for (my $i=0;$i<10;$i++) { print $i,$/}"
Insofar that the majority of its use is to simulate for ($m..$n) where $m<$n I think advising people to convert to the more legible and less error prone form is probably a good idea. And I also think that its a good idea to use while () {} continue {} instead of for(;;) if only because it tends to be easier to read. Having said that in the time ive been using perl I think ive needed the construct a mere handful of times.

Ultimately id argue that for(;;) is a construct mostly provided to reduce the pain for C junkies, and that in the long term better constructs that are more intuitive and less error prone are available and so should be encouraged.

--- demerphq
my friends call me, usually because I'm late....


Comment on Re: Re: Re: What training do YOU need?
Select or Download Code
Re^4: What training do YOU need?
by Aristotle (Chancellor) on Oct 22, 2002 at 13:30 UTC
    Having said that in the time ive been using perl I think ive needed the construct a mere handful of times.

    To reinforce the point.. I've literally never used it. There was not a single instance I wasn't equally or better served with the other constructs.

    And to be honest, considering the number of cases I advised people not to use for(;;) for whatever they were using it for, and the number of times I restructured loops in others' code to not use it, if it was to disappear from the language I wouldn't shed a tear. Actually, scratch that, I'd be cheering.

    Makeshifts last the longest.

      ive needed the construct

      For the record I didn't mean for (;;) but rather the while () {} continue {} construct which once or twice ive found has come in useful. For instance

      while (<$fh>) { s/foo/bar/ and next; s/yada/yada/ and next; #lots more of the same } continue { print $_; }
      Which _can_ be written without the continue, but when the innerds get a bit complex the continue can result in simpler code. (Sorry, but I had to make it clear ive never written a perl script with for (;;) in it ;-)

      Actually, scratch that, I'd be cheering.

      No disagreement there... It was that construct that seriously put me off of C when I first encountered it. In fact I still haven't quite got over the trauma of having to use it. :-)

      Now, the question is will Perl 6 have the obscene construct in it? My guess is probably... *sigh*

      --- demerphq
      my friends call me, usually because I'm late....

( Re:)+ What training do YOU need?
by rir (Vicar) on Oct 22, 2002 at 14:57 UTC
    There is not much more. The context was about changing  for(;;) to  for(@array) or to  map {} @array and doing it with an editor macro to the worst work of a weak group of perlers. In a training presentation to that group.

    My points were:

    • This, in its presentation, would trivialize the issues.
    • The  for(;;) and  for(LIST) constructs are very different.
    • This a rather small issue, surely there are more important problems in the above situation.

    Your comparison of the  for(;;) and  while loops is a different issue. For the simple cases I prefer a  for( $i=1; $i < 10_000_000; $i++) as all the loop's controls are there upfront. This assumes good practice, i.e. you don't fiddle with  $i while looping. If you mess with  $i then you should use a  while loop.

    You say:

    Usually when someone uses a C-Style for loop they are not exploiting the equivalence to a while , but rather using a slightly less readable, more error prone and less efficient form of a for (LIST) loop.

    Which is exactly the difference I was refering to with: more cumbersome but more adaptable.

    If brevity caused my unclarity, please let me know.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://207012]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (13)
As of 2014-04-17 14:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (449 votes), past polls