Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

What to do when you realize you've written bad code

by l2kashe (Deacon)
on Oct 16, 2002 at 15:11 UTC ( [id://205737]=perlmeditation: print w/replies, xml ) Need Help??

Ive been working on processing a particularly nasty log file. It's mainly due to the server logging things as soon as the events occur as opposed to at the end of a transaction. My previous attempt with an older rev of the software produced a 1200 line beast to process just that one file accurately. I still have auxillary functions, which are usefull and accurate, but the main functions need a total rewrite.

So now the codes been updated for the server, and its time to update the processing code.... and here I still am. Mainly due to the formating of the log, and the sheer size of it, I've hit stumbling blocks and slowly working back through them, but the log is definately winning the battle..

What do you do when you realize you've written unmaintainable code? I mean I take pride in the code I write, and it comes as a fairly substantial blow to my confidance. Its caused me took look over other lengthier code I've written, and that still looks clean/extendable.

I guess I'm looking for (support|war stories) from other perl junkies who've been where I'm at now.

print "Sorry\n" if ($post =~ /wrong forum/o);


/* And the Creator, against his better judgement, wrote man.c */

Replies are listed 'Best First'.
Re: What to do when you realize you've written bad code
by Aristotle (Chancellor) on Oct 16, 2002 at 15:21 UTC

    We all make mistakes. With time and experience you will learn to anticipate and correct them before you even make them; but that doesn't happen overnight. No baby learns to walk without falling many a time.

    It is definitely a step you should be proud of to be unsatisfied with your own code. Much as it dismays you now that you've written awful code, the fact that you did and that you also recognized it means you have the necessary frame of mind to grow.

    I started into Perl with CGI scripts, and looking back I have to say, golly, were they ugly. I made all the typical mistakes (no strict (but warnings!), handrolled CGI parser, tons of ugly verbose code, everything). But it was a necessary step in the process.

    Don't beat yourself up over it. If it doesn't look doable cleanly now, do it as you can, and think back to it in a few months - I bet you'll say "duh! I could have this and that." We learn continuously.

    Two articles you may want to read: Teach yourself programming in Ten Years for the human side and Big Ball of Mud for the technical one.

    Makeshifts last the longest.

Re: What to do when you realize you've written bad code
by VSarkiss (Monsignor) on Oct 16, 2002 at 15:35 UTC

    What do you do when you realize you've written unmaintainable code?
    Refactor. Basically, you sit down with the code and improve it. But you don't try to improve it by adding new functionality, or making it faster; you do it by making the code more maintainable. Usually this involves making it simpler, smaller, less redundant, more modular, and so on.

    The fact that you recognize good code from bad means you can accomplish it. What you need is a systematic approach: prepare tests first, then decide what you're going to try to attack. After each substantial change, re-test it to make sure you didn't break anything. Where you stop is a matter of judgement: how big it is, how much time you have, and similar considerations.

    Refactoring can be a very educational experience. It forces you to consider alternate ways of doing things. And after a few rounds, you'll get in the habit of using the more maintainable solutions in the first place. Plus, it matures you to admit, "Yes, I wrote really nasty code." ;-)

    I can recommend a good book about refactoring by Martin Fowler. The examples are in Java, but the principles in the first part of the book apply to most languages.

      Thanks for the book reference...

      /* And the Creator, against his better judgement, wrote man.c */
Re: What to do when you realize you've written bad code
by ignatz (Vicar) on Oct 16, 2002 at 16:40 UTC
    Here's a war story from my life as a dot.bomb veteran:

    I had spent months of 70 hour weeks building my dream publishing system. I had meditated for endless hours on how to anticipate every need that the armies of product managers would pull out of their ass at a moment's notice. 24 hour a day Audio/video streams, multiple instances with different look and feels, web and email content feeds, flexible modular content, real time web based publishing tools for our multiple offices, built in safety checks and roll backs, automated testing and bug reporting. I could change anything anytime anywhere at the drop of a dime. It was the culmination of my years of working with publishing frameworks. It was my masterpiece.

    Then I had the meeting.

Re: What to do when you realize you've written bad code
by BrowserUk (Patriarch) on Oct 16, 2002 at 16:32 UTC

    Personally, it's this very aspect of programming that keeps me doing it.

    The fact that I can look back at code I've written a few years ago, or even weeks in the case of my Perl, and notice that I no longer do things inthe way I used to, means that I never get to the point were I could say "I've done it it all so I'm bored".

    For the last few years I have kept an archive of code I have written (originally on optical disc, now CD's, DVD's soon.) and I wish I had started doing this 20+ years ago. I started out with the intention that I would be able to use it for code re-use and whilst there have been occasions when i have been able to simply crib something straight from the original version and use it, its a rarity. Most often I look back at the original, work out how I did it then and then re-code from scratch. The results are (mostly) a big improvement but because I can normally get back into the mindset I had when I wrote the original, I benefit from that as a starting point.

    Back in the early 80's a company in the UK produced a program that caused a lot of hype. They called it "The Last One", with the meaning that it was the last program that human beings would ever need to write! One of my lecturers at that time suggested that those of us who were looking to making a career from programming might like to rethink as "it is patently obvious that before long all programs will be written by computer". I'm glad that a) I ignored this 'sage' advise and b) programming continues to be more craft than engineering. As a one-time mech.eng. (draughtsman) who had become bored with lack of challenge in that field.

    Despite being of (now very apparent) mature years, my 'yearn to learn' is still strong, and is, in many ways, the saving grace of my sanity in a world filled with corporate dogma, rhetorical coding standards and the rule of lowest common denominator.


    Long live the craftsman's Guild of Itinerent Software Labourers.
    And long live its defacto creed of 'live to learn, whatever the source'. Down with nee-syndrome.
      Most often I look back at the original, work out how I did it then and then re-code from scratch. The results are (mostly) a big improvement

      An experience I share; I am continuously reminded of the old adage that you only really understand a problem after you've solved it. By definition this also means that you continuously improve your understanding of a problem with every iteration.

      I have found that even recoding once more yet usually yields another substantial improvement - third time's the charm, so to say.

      Makeshifts last the longest.

Re: What to do when you realize you've written bad code
by Sidhekin (Priest) on Oct 16, 2002 at 15:26 UTC

    Support: Realize that you have improved as a coder.

    War story (in progress): I am currently reviewing some code I wrote back in 1998 (shortly after learning there was a version 5 of perl). Amusing. Horrifying. Humbling, at times, but not enough to shake my hubris. And reassuring: Those years have not been wasted :-)

    The Sidhekin
    print "Just another Perl ${\(trickster and hacker)},"

•Re: What to do when you realize you've written bad code
by merlyn (Sage) on Oct 16, 2002 at 15:25 UTC
Re: What to do when you realize you've written bad code
by dws (Chancellor) on Oct 16, 2002 at 17:09 UTC
    What do you do when you realize you've written unmaintainable code?

    Either refactor it or throw it away and start over.

    In either case, try to take things one "story" (use case) at a time, building and testing small increments, and refactoring the code when you're done. Refactoring is like cleaning the kitchen when you're done cooking. It leaves less places for bugs to hide, and sets you up for a clean start for the next meal.

    The key to making this kind of development work is having an up-to-date test suite. Otherwise, you're never quite sure if you broke something.

    If I step into a mess, I'll try to write some test cases before I start to refactor. Often, the simple (hah!) act of getting tests cases running is enough to uncover substantial problems.

Re: What to do when you realize you've written bad code
by Mr. Muskrat (Canon) on Oct 16, 2002 at 15:23 UTC

    When I am not thinking clearly, I hack away at the beast until I start thinking clearly.

    When I am thinking clearly, I will start from scratch. I try to get a different perspective on the problem before I start coding on the project again.

Re: What to do when you realize you've written bad code
by Molt (Chaplain) on Oct 16, 2002 at 15:34 UTC
    Simple solution, confess your sins and be absolved. Either that or read the war stories others have confessed and generally feel better about things.
Re: What to do when you realize you've written bad code
by l2kashe (Deacon) on Oct 16, 2002 at 16:00 UTC
    Thanks folks. I am/was in the process of refactorization, but wanted to stop in and look at what people had to say.

    There is nothing more diffcult than growth, and it's just one of those days, so I needed some moral support. It's a nice change of pace to find that on the 'net.

    Thank you all :)

    /* And the Creator, against his better judgement, wrote man.c */
Re: What to do when you realize you've written bad code
by Jenda (Abbot) on Oct 18, 2002 at 09:51 UTC

    Well ... what does the log look like, what do you actually do with it and how did you do it?

    Maybe if you give us more info, we can suggest a totaly different approach. Something you might have overlooked that might turn this beast into something simple (and maybe not:).

    This has happened to me a few time. While reading or listening to such suggestions I usualy thought something like "What? ... no ... that can't work ... actually ... let's see ... I AM STUPID!" ;-)

    Jenda

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (6)
As of 2024-03-28 15:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found