Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Confessional: why I wrote bad Perl code.

by vladb (Vicar)
on May 20, 2002 at 14:08 UTC ( #167838=perlmeditation: print w/ replies, xml ) Need Help??

Although most of the time I would prefer keeping my confessions private, I thought this one had to be made public for common good and enlightenment. So here it goes…

Recently I was revisiting my old code and couldn’t believe what I saw. In a few words: pure quagmire! Most of my earlier coding techniques (if one could call them such {grin}) are in direct opposition to the ones I now cherish and uphold. While writing this node Re: Oh wise Brothers where will you guide me now? up in hopes of helping an aspiring monk to set his foot straight on the path to Perl sainthood, I recalled that it was not so long ago (a mere ten months..) when I too wasn’t aware of a lot of simple truths. Some of a few things that spring to mind are ...

  • Abusing global variables (that is using them a lot in many indecent ways).

  • Few subs and more code dumped in the main package outside of any definite subroutine.

  • Including configuration code in the same file where the main script code was kept. The beauty of this was that I would also scatter certain crucial configuration variables throughout my code in no particular order/organization.

  • More obfuscation and less documentation (a breathtaking mix don’t you think?).

  • No visible sign of sanity check code (using evals to catch dies/warnings, checking for boundary values etc, checking hash keys for definedness and existence to avoid vivification).

  • Sloppy data structures. This included either building too complex nested combos of AoH and AoA (normally mashed together) when a simpler Hash structure would have done the job and vice versa.


Unfortunately, I still continue writing poor code to the present day, albeit to a lesser degree. I should admit however (in a futile attempt at covering up a good measure of my sins) that most of the time the driving forces behind my writing sloppy Perl code a mix of these factors:
  • Tight deadlines.
    There always seems to be a significant disjoint between the needs of the management and that of mere Perl practitioners (usually classified in the ‘programmers’ group). Many a times, in outlining their project schedule, managers give little regard to various software design needs and documentation. This leaves only so much time to write sloppy but (hopefully) usable code.

  • Code was meant for personal use.

  • Code was only a temporary solution to a problem or a patch.

  • I was too lazy to think any better.



And what are (were) your reasons for writing sloppy Perl code?

_____________________
$"=q;grep;;$,=q"grep";for(`find . -name ".saves*~"`){s;$/;;;/(.*-(\d+) +-.*)$/;$_=&#91"ps -e -o pid | "," $2 | "," -v "," "]`@$_`?{print" ++ $1"}:{print"- $1"}&&`rm $1`;print"\n";}

Comment on Confessional: why I wrote bad Perl code.
Select or Download Code
Re: Confessional: why I wrote bad Perl code.
by krujos (Curate) on May 20, 2002 at 15:17 UTC
    Some days you have to do it. Most often I am writing a script that will be used once, five minuets after I was asked to write it, and then thorwn away. Lots of time I have to go with the first idea I have (which usually is not the best) and turn it around. The nice thing about perl, is you can get away with it (I know this is not exclusive to perl, but....).
Re: Confessional: why I wrote bad Perl code.
by thelenm (Vicar) on May 20, 2002 at 16:41 UTC
    ++vladb! Good discussion. I think I've written (and still write, though I should know better) sloppy code for all of the reasons you mention. And it all comes back to bite me in the butt every time, because the bad assumptions I made all turn out to be wrong.

    Code was meant for personal use?
    At some point someone else will find out about it, start using it, maybe even modify it, and then you'll be expected to maintain it. I write handy throw-away scripts all the time, but the problem is that I never throw them away, because they're handy. And I share them with people who know just enough Perl to be dangerous. Unfortunately, when they extend my handy scripts for themselves, they do it in the same sloppy style I used in the first place, because I thought it was just a throw-away. And since I'm the resident Perl programmer, I get to fix those scripts when they break.

    Code was only a temporary solution or a patch?
    As people use the code and want it to do more and more things, it will grow, one feature at a time, until it is an unrecognizable blob of spaghetti that gives you a headache just to think about. Again, you'll be expected to maintain it. I've spent the last couple weeks undoing many of the mistakes I originally made with a small program that was only supposed to do one simple thing once upon a time. I used a few global variables and a few sloppy subroutines with side effects in package main. Well, you can guess what the code came to look like. Dozens of globals, dozens of sloppy subroutines with side effects, all in package main. Total spaghetti. And it had come to be used for all sorts of things I had never envisioned. Ugh.

    Too lazy to think any better?
    I've come to see that writing sloppy code is false laziness. I may save myself a few seconds and some design effort in the short term, but eventually I will pay ten-fold in undoing entrenched sloppiness when the program grows. If I were truly lazy, I would never write sloppy code in the first place.

    I think you've hit the nail on the head in identifying some reasons we write sloppy code. Of those, it seems that they can all be prevented with a little effort except for Tight deadlines, which are generally controlled by someone other than the programmer.

    I am also tempted by Cleverness. It's fun to write one clever statement that could instead be written as three straightforward, easily maintainable statements. But later on, when I try to modify clever code I've written, I've often thought, "What the heck?" and been confused. I've done that enough times to realize that readable code is better than clever code, if you ever have to modify it. Thanks for bringing up this painful (but important) subject.

Re: Confessional: why I wrote bad Perl code.
by CukiMnstr (Deacon) on May 20, 2002 at 20:27 UTC
    Not knowing any better?
    Sometimes you just don't know any better, so you code spaghetti. And then some time later (maybe a couple of hours later, sometimes years later) you find a piece of code that does the same thing your sloppy code does, but in a more clever/clean/readable/efficient... way, and you go "whoa! Why I didn't do it like this?" Because you didn't knew any better at the time. I know I don't write "good" code at this time, but I certainly write better code today than I did yesterday (using strict, passing arguments to subs in hashes, bind()ing variables when working with DBI...), just because my wonderings in the Monastery, practice and reading have shown me better ways of doing things. And when I find a better way of doing something, I try (if I have a little time) to go back, one script at a time, and change the sloppy way to the clean way, hoping to really learn the technique. So I guess everyone still writes "bad Perl code" if they compare their code written today with the code they could write tomorrow.

    just a random thought,

      Very similar to not knowing any better:

      Because I am a crappy programmer
      If a good programmer thinks things through carefully before starting, then I'm not a good programmer. If a good programmer carefully reviews CPAN for existing implementations before beginning anything new, then I'm really not a good programmer. And if a good programmer implements just the features needed at a given time... well, maybe I should pick another profession.

      But that's just a small taste of how bad I am. I'll switch to a slower algorithm to avoid using a temporary variable. I abbreviate variable names to make things fit on one line. I do things in strange ways just because I heard about them or thought "I wonder if you could...", and I use the result in production scripts. I parse HTML and XML with regular expressions. Often. I write code that I know won't work when given filenames with spaces. I'll sometimes keep using code because I'm proud of how little time it took me to write the initial (almost) working version. I'll use C instead of C++ because C compiles faster (so I can get back to gdb faster). I'll use Inline::C for something that would be easier to write as straight C, just because I feel like using perl. I'll spend two hours optimizing code that I plan to run once, where the initial version would have finished in half an hour. I'll rewrite efficient, elegant code because I couldn't understand the algorithm in the original, and end up with a sloppier version of the same algorithm. And then I'll use my inferior version when the time comes to plug it into the rest of the system.

Re: Confessional: why I wrote bad Perl code.
by strat (Canon) on May 21, 2002 at 11:48 UTC
    vladb++

    Another problem that hit me sometimes is the question "Hi Martin, could you just write a script for doing xyz? Takes you less than 30 Minutes? Yes, afterwards you can throw it away, we won't need it any more..."

    When the script is running somehow, enhancements are required. "... just this little enhancement, and you can forget it. It will save us some hours of work...".

    And by and by functionality is increasing, and the script gets worse and worse. And suddenly, sometimes gets the great idea: "...if you just add this feature, we could use it for other problems as well...".

    I try to prevent such situations by trying to write every script as good as possible (if possible because of time and the like). I try not to say, it will take me about 30 minutes, but instead I say: 2 hours. So, at first it always takes me more time than necessary at the beginning, but saves me lots of time (and reputation as well) in future.

    The (nearly) only throw-away-scripts I write are one-liners or the like (I normally don't save them to a file)...

    But nevertheless, I sometimes produce code that I'm not so much proud of, because of many reasons already mentioned in this thread. But I will keep on learning.

    Best regards,
    perl -le "s==*F=e=>y~\*martinF~stronat~=>s~[^\w]~~g=>chop,print"

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://167838]
Approved by ybiC
Front-paged by pdcawley
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (11)
As of 2014-09-19 08:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (133 votes), past polls