in reply to improving the aesthetics of perl code

I didn't intend for this to turn into a style guide, but it seems I've written one. Hopefully, it will prove helpful.

Ytrew's Top Ten Style Points

Replies are listed 'Best First'.
Re^2: improving the aesthetics of perl code
by Anonymous Monk on Jan 22, 2005 at 07:42 UTC

    The fat cat sat on the mat. The fat cat was tired. The fat cat had just eaten. The fat cat ate a lot. That made the fat cat sleepy. The fat cat went to sleep on the mat.

      The fat cat sat on the mat. The fat cat was tired. The fat cat had just eaten. The fat cat ate a lot. That made the fat cat sleepy. The fat cat went to sleep on the mat.

      It is far better to write the simplistic text above than the purportedly "more adult" text below, in my opinion.

      "The aforementioned creature, grown senecent in the ways of corpulence, had taken up residence upon an entanglement of fibrous matter. Bestirring itself to the siren call of the Sandman's lair, it gravitated in a state of satiety, brought on by an act of pedestrian consumption, noteable only by it's excess. Led thereby to the banquet of Morpheus, it took up the preoffered cup, paused, and drank deeply."

      The second paragraph is similar to a lot of code I see in the world today: it suffers from trying too hard. It has several faults. It's excessively wordy. It uses too many confusing metaphors. For example, "Morpheus's banquet" refers to sleeping, not eating.(Morpheus was the ancient greek God of Dreams.)

      It contains both grammar mistakes ("it's" rather than "its") and spelling errors ("preoffered" is particularly confusing, as it looks like "pre-offered" rather than "proffered").

      It also fails to mention that the creature in question is really a cat. Given that the story is about the cat, it's a major flaw.

      Suppose I hire a 14 year old kid as a junior editor. If I tell him "change the cat from fat to thin", and "make him drink, not eat", I know he can fix the first story with ease. I also know that he'll have a harder time with the second story: it will cost me more money, because he'll get confused, and take longer trying to figure it out. It would take a lot of editing to fix the second story.

      If I were to edit the original text, I would find that there's not as much wrong with it. The word "fat" is repeated needlessly. The paragraph doesn't stand alone: it assumes that a specific cat has been previously identified, by the use of the word "the" rather than "a" when refering to the cat. The notions of sleep and causation are repeated.

      I would probably write the story as:

      "Once upon a time, there was a fat cat. The cat sat on a mat. It was sleepy, because it had just eaten a lot. So, it went to sleep on the mat."

      Note that it's still readable by a grade school student, or by a non-native English speaker. I've taken seven years of French in school, and I still stumble when trying to read children's books. I imagine non-native programmers feel similarly with English.

      Remember, the purpose of programming is not creative expression, but rather clear exposition. The CPU needs to know what to do: the maintainer needs to know why you did it. No one needs to know that you passed grade 13 English with a 97% average (except maybe your university English professor).
      --
      Ytrew

        The overweight cat was feeling drowsy having eaten a large meal, and lay down on the mat to sleep.

        Concise, joined up, complete.

        Software constitutes some of the most complex creations of man. Writing software is one of the most challenging endevours man undertakes, and requires education, skills and experience to do well.

        You would not employ a lawnmower service apprentice to work on your Lexus or Mercedes. Or a flight attendant to fly your 767. Nor a med. student to perform open heart surgery. Why would you employ a 14 y/o to edit anything important?

        Equally, employing unskilled, or semi-skilled persons to maintain your business software, and deskilling your programmers to write baby code in order that you can, is stupid and counter productive.

        If you dumb down the code, the maintenance programmer will never aquire better skills. Supervise their work and train them. The rewards will outweight the costs.

        "No one needs to know that you passed grade 13 English with a 97% average"

        Then why is your highest priority item?

        "As others have mentioned, spelling and grammer are very important. Learn to express yourself in English, first."