in reply to Does bad code really teach you to write good code?

A good approach is to start from the place that your code could be either bad or good. Once it actually works, as in, solves the problem, realize that that's the most important thing.

Ask yourself how clearly visible the solution logic is. If your program flow jumps right out at you and doesn't need explanation, it has a chance at being good code. Ask your cow-orker if he/she can see it as clearly and explain it.

Then, as has been posted, go find some samples of other code and try to understand it. It doesn't matter if the code is considered "good" or "bad", only that it's somebody else's. Pay attention to your own internal conversations while looking over the code. What questions are you looking to answer?

One of the problems inherent in looking at other peoples' code is that everybody's idioms are different, and everybody's "working subset" of Perl is different. Code that's a natural for an OO Perl addict is obtuse to someone from the dark ages (like me) who thinks parentheses have their uses and IF should come before THEN.

More important than syntax is to understand the various patterns of coding in modular fashion. Here's a good exploration to start with. It's more important to learn how to approach a problem in an analytical manner than it is to learn from the bottom up what "good code" is, although writing code that mirrors the flow WILL help you maintain your program DTR.

Bad code -- or rather, the maintenance of bad code -- does teach one a lot. I've had the good fortune of being asked to maintain both good code and bad (hey, I did get paid to do it!), and both experiences have helped me greatly. I've also had to go back and look at my own work many a time, and the view from after another turn of the spiral always yields more insight. "Good code" is such a moving target. Good code is code that works and can be understood by another, no matter what set of idioms it uses. Elegant code is in the eye of the beholder, and, once again, it's more in the problem solving approach than it is in the details of the code.

Of course, doing your best to emulate good code writers will get you much further than hanging with slackers! < grin > :D
  • Comment on Re: Does bad code really teach you to write good code?

Replies are listed 'Best First'.
Re^2: Does bad code really teach you to write good code?
by Gekitsuu (Scribe) on Jun 18, 2005 at 06:55 UTC
    Thank you very much for the good advice I plan on reading the guide on my night off so I can really enjoy it.