Today, I just discovered a nasty habit of mine: guessing at code and syntax. I use the word "discovered" because I carried it out on a subconscious level. As I think about it now, it's been the source of countless frustrating hours trying to figure out why my code doesn't work the way I think it should. I want to share this because it's probably safe to say that I'm not the only who has this vice.

By "guessing at code" I mean writing code you only think is right without looking it up to make 100% sure. Out of pride, we convince ourselves that we know Perl and don't need any damn book to tell us how to code. What makes this habit particularly insidious is that by the time you've written and executed a whole block of code, you forget the parts you guessed at and start assuming that it's correct. This can lead you on a wild goose chase that could have been avoided if you just double checked your code against a reference work. So, if you have the least doubt about how to carry something out, look it up!

$PM = "Perl Monk's";
$MCF = "Most Clueless Friar Abbot Bishop";
$nysus = $PM . $MCF;
Click here if you love Perl Monks

Replies are listed 'Best First'.
Re: Never guess at code!
by epoptai (Curate) on Jul 03, 2001 at 22:01 UTC
    I know what you mean but am not sure it's about pride. Trial and error, and guessing are key strategies people use to learn, expand and deploy their language skills. I'm always learning new things about perl when ignorance and incomprehensible documentation demand experimentation. It's also natural for acquired skills to settle into habitual patterns of unconscious behavior so they can be used spontaneously without having to think about them.

    I agree with your conclusion that this tendency needs to be overcome when using language to speak with machines. They demand a much more exact grammer and syntax since the other constructs we habitually use to convey meaning such as inflection and body language don't apply (yet).

    So I think that saying 'never guess at code' is a little too strict :-). Instead, guess away and learn new ways to do things, but always double check your guesses, and question your own coding habits.

    --
    Check out my Perlmonks Related Scripts like framechat, reputer, and xNN.

Re:(Jepri) Never guess at code!
by jepri (Parson) on Jul 03, 2001 at 22:21 UTC
    I guess it all depends on your programming mentality. I've been at it for a while now (although not all Perl), and I have never been able to stand writing more than a few lines of code without checking that it works.

    This is probably a personality thing - I just get really depressed if I can't see the results of my code running every half hour or so. I have this dread of writing some elaborate routine that takes ages to implement, and then discovering that I can't do it like that and having to scrap all that work. I have to say since I came to Perl that's happened a lot less :)

    I also can't imagine coding without half a dozen help windows open showing the docs for the modules I'm using in my program. I do know people who like to do the whole program in one shot, then go back through and pick up the problems later. Personally, I can't stand debugging, so I do whatever I can to make sure I never have to do a huge debug session. I dread trawling through thousands of lines of code to find the bug.

    Also, similar to the tip mentioned above, I usually have a window open to a program called test.pl, wherein I test anything that I'm not totally sure of. A lot of my program gets written in there, and then copied in to the body of the main bit when I'm totally sure it workd.

    ____________________
    Jeremy
    I didn't believe in evil until I dated it.

      jepri, thanks I found your post very enligtening. I've never seen anyone other than myself program so I'm very interested in hearing how you "debug while you code". I guess I do a little bit of both. Sometimes I just charge on and let the compiler and warnings catch my errors (hopefully), other times I'll write just a couple of lines and execute it. Obviously, it's the times when I rely on the compiler and Perl warnings that I get nailed because they can't catch errors in logic or my misunderstanding of the Perl interpreter works.

      $PM = "Perl Monk's";
      $MCF = "Most Clueless Friar Abbot Bishop";
      $nysus = $PM . $MCF;
      Click here if you love Perl Monks

Re: Never guess at code!
by voyager (Friar) on Jul 03, 2001 at 22:59 UTC
    From "The Pragmatic Programmer", tip #44:
    Don't program by coincidence
    The gist of this is " ... if something seems to work, but you don't know why, make sure it isn't just a coincidence".
Re: Never guess at code!
by mikeB (Friar) on Jul 03, 2001 at 21:51 UTC
    I often keep a perl -de0 window open to verify constructs that are less familiar. That and a copy of Camel III go a long way to keeping me on the straight and narrow.
Re: Never guess at code!
by schumi (Hermit) on Jul 03, 2001 at 22:55 UTC
    I think in a way most of us are more or less guessing at the code we're writing, until we actually test it. But as a beginner who's only recently started to "speak" Perl, I usually trying to find a way to do what I want, and then look it up in a book to find out the exact syntax, usage, etc. That of course means that whenever I'm coding I have at least two books, and often enough the Perl CD Bookshelf, lying around - or falling to the floor, for that matter.

    After some time, the things I have to do again all the time, and of course the basic stuff, just flow freely, but as soon as I want to do something new, I go and look it up - trying to keep it in mind for the next time, of course. And often the most useful resource, is PerlMonks. Although the books tell me how to do things, here I find more specific examples and sometimes even the exact solution I need to my problem, because someone has had it before, posted it in the Seekers of Perl Wisdom and got a helpful answer.

    Something else that I find helpful, and in fact do all the time, is going back to my "old" code and see whether I've had the problem somewhere before, and if possible, just copy the code and adapt it.

    But then, looking things up doesn't mean the code then actually works. Sometimes I have a problem which I think I know in what direction the solution has to go, and look that up. But then I realise that in fact, while this particular solution would work for another problem, I didn't assess the problem correctly and hence chose the wrong solution. Erm... Am I making sense here?

    Anyway, I think looking things up doesn't hurt my pride. Point one, I am a beginner, after all, and point two, even if I weren't, there's always something that I haven't done before and hence haven't got a clue about. Just like you cannot speak a language perfectly, because a) you always make mistakes even in your first language, or neologise, or b) you may speak a dialect, or c), and this is the point, you don't know any kind of jargon which is not from your own field of work. Similarly, if I'd be working on a linux machine, I wouldn't have a clue, and wouldn't concern myself with, any Win32-stuff. (I wish...) Even if you know Perl, you don't know all of it.

    --cs

Re: Never guess at code!
by VSarkiss (Monsignor) on Jul 03, 2001 at 23:38 UTC
    I think all of you are wrong. I never read the manual (laziness), I always slap-dash things together as quickly as possible (impatience), and I think the damn compiler should just DWIM (hubris).

    I hope a smiley wasn't required.

    Seriously, though, one of the greatest things about Perl is that the learning curve is not steep. It doesn't take long before code that you think is correct actually is correct. That's probably why you soon lose the habit of looking up references. A pity, since the documentation is so easy to access from just about anywhere.

Re: Never guess at code!
by toma (Vicar) on Jul 04, 2001 at 00:27 UTC
    The way I think about this is "never fly any higher than you are willing to fall." As you write the code, you should be asking yourself, "how will I know that this line of code is working?" Maybe you should put in a print statement, or maybe you'll check values in a debugger. Another trick is to use assertions, such as:
    ($loop_count > 0) or die "Loop counter: `$loop_count' value non-positi +ve";<P>

    Watching other people code, especially someone who is very good, is educational, and can be fun. I picked up a few good habits by watching a "Perl Death Match," which involves watching people code as they compete.

    It should work perfectly the first time! - toma