Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: The joys of bad code

by FoxtrotUniform (Prior)
on Oct 26, 2004 at 04:56 UTC ( [id://402461]=note: print w/replies, xml ) Need Help??


in reply to The joys of bad code

My all-time (least) favourite is an example from Rob Pike's Notes on Programming in C:

/********************************** * * * Add one to i * * * **********************************/ i=i+1;
Don't laugh now, wait until you see it in real life.
I laughed at the time, but I've seen it in real life (second-year programming assignments; TAing can be fun).

--
Yours in pedantry,
F o x t r o t U n i f o r m

"Lines of code don't matter as long as I'm not writing them." -- merlyn

Replies are listed 'Best First'.
Re^2: The joys of bad code
by pg (Canon) on Oct 26, 2004 at 05:54 UTC
Re^2: The joys of bad code
by Merlin42 (Friar) on Oct 26, 2004 at 16:59 UTC
    Its a little scarier when you see:
    /********************************** * * * Add one to i * * * **********************************/ i = i - 1;
    So is it the code or the comment that is wrong?
      So is it the code or the comment that is wrong?
      When the comments don't match the code, I normally assume they're both wrong.
Re^2: The joys of bad code
by Happy-the-monk (Canon) on Oct 26, 2004 at 09:31 UTC

    i=i+1;

    I certainly see this in Perl all the time:
        $self->total( $self->total + 1 );

    These habits just never die.

    Cheers, Sören

      Happy-the-monk,
      This isn't quite the same thing. Unless you make the total method lvaluable, neither
      $self->total++; # nor $self->total = $self->total + 1;
      will work. I think the point wasn't about incrementing a variable by one, but feeling the need to document it. I do see the point in documenting it (not in what the code does, but why it does it), but to each their own.

      Cheers - L~R

        I do see the point in documenting it (not in what the code does, but why it does it)

        So this is off topic: bad documentation does not bad code make.

        Documenting what the code does isn't bad by itself, it just isn't needed at all if the people supposed to be reading the documentation can program already.

        Imagine they can't, that kind of documentation might help them understand what's happening.

        Cheers, Sören

      I certainly see this in Perl all the time:
        $self->total( $self->total + 1 );

      Before automatically writing this off as being bad code, consider:

      • The class has--or appears to have--a public accessor and a public mutator for total.
      • The mutator is free to do stuff other than setting total. For example, it might log that fact that total has changed, or it might notify other agents who have registered interest in that aspect of the the instance.
      • Subclasses are free to add interesting behavior to mutations of total, even if those mutations are generated internal to the class.

      By coding this as   $self->{total}++; you lose these benefits. Sometimes these benefits aren't important, sometimes they're vital. That said, perhaps   $self->increment_total; might have been better.

Re^2: The joys of bad code
by MidLifeXis (Monsignor) on Oct 28, 2004 at 17:05 UTC

    How about this one (seen in a previous $worklife):

    /* Assign 1 to x */ x=1; /* Just to be sure */ x=1;

    --MidLifeXis

Re^2: The joys of bad code
by SpanishInquisition (Pilgrim) on Oct 26, 2004 at 13:49 UTC
    The scary part is, some of them graduate.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2024-04-19 21:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found