in reply to Re: crafting a decent CLI
in thread crafting a decent CLI

Actually, you don't have to parse the rest of the do block to figure out what is going on. Once you see ";\ndo {\n" you know (from the ";" and newlines) that it isn't a (sensible) conversion of a block to an expression and (from the "{") that it isn't "do file".

There was a smiley after the paragraph you quote. The point of that was to illustrate how hard the loop control shortcuts can be to parse. Your example was quite small so figuring out where each control change branched to was not difficult. I still prefer "do {" because it makes most people think "loop" much faster and because the branches stand out much more. Hiding a branch with something small that doesn't change indentation (like redo or goto) means that the structure of the code is not nearly as visually discernable.

So I avoid naked blocks, redo, next, and last because they are easy to abuse and require more visual energy to parse. That doesn't mean I never use them, of course (though I've never found a use for naked block as a loop). For example, redo, next, and last are important tools for allowing you to use Perl's most natural looping construct (for(@list)) in more situations rather than resorting to a more error-prone alternative.

As a few other examples of this have shown recently, pulling the "naked block plus redo" out for very simple problems can lead to producing much more complex code than is required because it doesn't put much pressure on you to figure out the structure of the code since they give you such abitrary control of the flow.

And it certainly isn't a tool I would ever suggest to a person already having problems writing fairly simple code in a well-structured manner.

        - tye (but my friends call me "Tye")

Replies are listed 'Best First'.
Re: crafting a decent CLI
by Abigail (Deacon) on Jul 13, 2001 at 11:49 UTC
    Actually, you don't have to parse the rest of the do block to figure out what is going on. Once you see ";\ndo {\n" you know (from the ";" and newlines) that it isn't a (sensible) conversion of a block to an expression and (from the "{") that it isn't "do file".
    I do not quite agree with that. You do know the expression modifiers, don't you? EXPR if EXPR and EXPR for EXPR etc are quite common in Perl. Every now and then you want to do two or more things in the first expression. EXPR, EXPR if EXPR sometimes works, but sometimes it won't. So you use do {EXPR; EXPR} if EXPR. That do is followed by a '{' and typically preceeded by a ";\n". And hopefully you aren't giving special meaning to the newline. ;-)

    -- Abigail

      That is what if( EXPR ) { EXPR; EXPR } is for. So, no, I don't consider that a sensible use of do.

              - tye (but my friends call me "Tye")
        The point isn't that you find it sensible or not. The point was how easy it was for others to parse your code. You weren't assuming I had to parse the bare block I wrote to see whether it's used as a multi-pass loop, do you? I wrote it, so I know.

        If you want to get into style discussions, that's fine, but don't come with arguments "oh, I don't find it sensible so don't apply my arguments on my code".

        -- Abigail