Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Isolating DNA cont.

by talexb (Canon)
on Dec 21, 2018 at 15:51 UTC ( #1227573=note: print w/replies, xml ) Need Help??


in reply to Isolating DNA cont.

A general comment on developing software that works for me (and I suggest it whenever I can), is to always create balanced operations or braces, then go back and fill in the middle. So you would start with your boilerplate opening:

#!/usr/bin/perl -w use strict; use diagnostics;
You would have collected your input, opened and closed the file:
my $file = $ARGV[0]; open (my $input, '<', $file) or die("Cannot open file $file: $!"); close ( $input );
Then you would have added the skeleton of your while loop:
my $file = $ARGV[0]; open (my $input, '<', $file) or die("Cannot open file $file: $!"); while($input>) { } close ( $input );
Even stopping there, you'd have something that opened a file, read through each record, and closed it. You don't have to remember to finish the loop later, or think about closing the file .. you've already done that, and you can then go back and put the meat into the sandwich.

Adding the if statement (without a condition) brings us to this:

my $file = $ARGV[0]; open (my $input, '<', $file) or die("Cannot open file $file: $!"); while($input>) { if () { } } close ( $input );
To me, that's way easier than writing the opening part of a brace or an operation, then later, trying to remember what I have left to go back and clean up.

Alex / talexb / Toronto

Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

Replies are listed 'Best First'.
Re^2: Isolating DNA cont.
by kcott (Bishop) on Dec 22, 2018 at 11:29 UTC

    G'day Alex,

    ++ I just wanted to add my agreement with, and recommendation of, this method of coding.

    I started doing this many years ago — in fact, possibly a couple of decades ago — and it's made my (programming) life a whole lot easier.

    Nowadays, without really having to think about it, I start sections of code like:

    while (<$fh>) { }

    or

    if ($condition) { } else { }

    I then don't have to remember to add closing braces. I also don't need to worry about aligning the closing braces to get the proper indentation: auto-indent has already done that for me.

    The same works for other balanced characters. Building up a complex expression incrementally like this:

    if () { if (is_whatever()) { if (is_whatever(get_whatever())) { if (is_whatever(get_whatever(get_options()))) {

    almost never results in a "mis-matched parentheses" type error.

    Attempting to type that final expression all at once, often will; and leaves you having to scan back and forth over the expression, counting opening and closing parentheses.

    I also use this method for building up complex regular expressions (using the x or xx modifiers).

    This may not work for everyone; for example, perhaps syntax-highlighting is set up to indicate mis-match problems. However, it certainly works for me and clearly works for you. I would recommend people at least try it.

    — Ken

      Nowadays, without really having to think about it, I start sections of code like

      I literally don't have to think about it because my editor does it for me. It is worth the investment of getting to know your editor of choice so that such boilerplate can be automated away.

          I literally don't have to think about it because my editor does it for me.

        Sure -- and I could do the same, but I prefer to type it myself. That's a personal preference -- whatever makes you the most productive you, go right ahead and do that.

        For instance (he said, donning his fireproof suit), I prefer vim to emacs -- I'm always amazed at what a powerful tool emacs is, but I'm happy with the less powerful vim. I know enough about the command line arguments (o argument for horizontal split, O for my favourite layout, vertical split) and the in-editor arguments (^W ^R to rotate windows, ^W = to make them equal sizes, gq} to wrap things, marking a block and to have perltidy clean it up) to be productive.

        Alex / talexb / Toronto

        Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

        "I literally don't have to think about it because my editor does it for me."

        I suspect that's not strictly true. Wouldn't you need to set up some sort of macros, shortcuts, hotkeys, etc. and then activate them to get the boilerplate added to the code. I'd love an editor that just knows exactly what I wanted, without me having to think about it, and then just automatically adds the appropriate code; however, I don't think such a beast exists.

        "It is worth the investment of getting to know your editor of choice ..."

        I've been using vi (or related versions, e.g. vim) for over 30 years. I believe I know it well.

        "... so that such boilerplate can be automated away."

        Every construct for every (programming, markup, etc.) language I use? I don't think so. Muscle memory requires no actual thinking: I'm usually thinking about the code logic to be added whilst I automatically type the basic construct boilerplate (which, in general, is only of the order of about a dozen keystrokes or so).

        — Ken

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2019-07-21 18:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If you were the first to set foot on the Moon, what would be your epigram?






    Results (9 votes). Check out past polls.

    Notices?