Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Re: What training do YOU need?

by rir (Vicar)
on Oct 21, 2002 at 14:35 UTC ( #206847=note: print w/ replies, xml ) Need Help??


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

Good post!

Pick on a common example of code that can easily be improved using idiomatic constructs. Something like changing C-style for(;;)'s to foreach(@array) or map{..}@array. Make the first change manually explaining the reasons and benefits,

I realize you probably just quickly chose a this construct as an example. But your choice is unfortunate.

Changing C-style loops to foreach loops is not as trivial as you would have it appear. The presentation would be impressive, but misleading.

I might point out the advantages of foreach loops. And how the C-style loop is more cumbersome but more adaptable. These folk are probably very comfortable with this C phrasing. Saying this is how to improve the code is unnecessary optimization and quibbling over style at the risk of breakage.

I like presentations that include workload and mental state, to wit:

"It took me two hours to decipher this code."
"Thirty minutes to get this rewrite, that has these problems. Notice how I used ..."
"A hour of exploratory testing and minor tweaks. At this point I felt like I had a good understanding of ..."

To the original post:

If these are not fairly simple scripts and requirements are vague I'd first want my crew to instrument the old stuff to tee off both I and O on demand. Assuming this is reasonable.

I cannot imagine the function-free java code these people are writing.


Comment on Re: Re: What training do YOU need?
Re: Re: Re: What training do YOU need?
by demerphq (Chancellor) on Oct 22, 2002 at 09:33 UTC
    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....

      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....

      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://206847]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2014-08-29 17:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (283 votes), past polls