tcf03 has asked for the wisdom of the Perl Monks concerning the following question:
2006-09-14 Retitled by planetscape, as per Monastery guidelines
Original title: 'aesthetics'
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: improving the aesthetics of perl code
by Tanktalus (Canon) on Jan 21, 2005 at 14:17 UTC | |
(minor typo - it's "aesthetic".) Check out perldoc perlstyle - that's a great place to start. | [reply] |
Re: improving the aesthetics of perl code
by matthewb (Curate) on Jan 21, 2005 at 14:40 UTC | |
I bookmarked a Slashdot comment (!) a few months ago that I thought raised some points that are mostly worthy of consideration. Along similar lines, the much-missed Abigail's coding guidelines contain style recommendations that demand reading.
MB
Update: put <em> tags around the word `mostly'. | [reply] |
by jonadab (Parson) on Jan 21, 2005 at 16:53 UTC | |
I bookmarked a Slashdot comment (!) a few months ago that I thought raised some points that are mostly worthy of consideration. Worthy of consideration, certainly, and many of them I quite agree with. I do think, however, that some of them are a bit misguided. I'm not convinced that $Mixed_Case_Globals, for example, are a great idea. Here are some more of my reservations... His point 6, about blocks, I tend to disagree with; to me, that's redundant and wastes vertical space, resulting in the need to scroll more than is really necessary in order to see all of a function or block; this can really impede the reader's ability to follow the code in some cases, because when a block goes past a screenful it's *harder* to see where it starts and ends, not easier. If you pull up the braces inside that block onto their respective previous lines and let the indentation speak for itself, you can see more (if not the whole thing) at once. The indentation makes it visually clear where blocks end even if your text editor _doesn't_ do brace matching (which it does if it's not fundamentally lame). But if you put every brace on its own line, you can almost double the length of the function, in terms of the number of lines. Point 11, about map, depends greatly on the background of your programmers. If you expect the code to be maintained by someone coming from a background in languages such as Java, it's probably good advice, but if the person doing code maintenance can be expected to have a couple of years' background in Perl or lisp, this concern goes away entirely. In some cases map adds significant clarity (assuming you understand lists). I absolutely disagree about unless. It may not be common in other computer languages, but it's a very straightforward cognate for the English word "unless" and means exactly what you would think it would mean. Using an explicit main() is completely pointless unless your main goal is to confuse C programmers about what language they're reading. (OTOH, he suggests putting an explicit exit before your post-defined subroutines, which is good practice IMO.) Regarding one-letter variables: the point is valid, but his stance is IMO a bit hard-nosed. I like to use a one-letter variable when a given script or function really has one main object that it keeps working with a lot -- such as the record object that a function in a DBI script is working with, or the one WWW::Mechanize object that a web automation script is using every line or two. However, it is certainly true that overuse of short variable names is to be reserved for the Obfuscated Code section. Point 18, about minimizing implicit pronouns, is one of the worst, most extremely misguided pieces of Perl style advice EVER. Cluttering the program with a couple of hundred stupid unnecessary extra variables that only hang around for a couple of lines does NOT make it more clear. I agree with 19 in principle, theoretically, but I'm guilty of violating it without remorse with alarming regularity, usually when the scalar is either an index into the array or the iterator in a foreach over the keys of the hash in question. Number 22 I also tend to disagree with: in many cases all it does is add extra lines, pushing more of the function off to the next screenful. Number 28 reminds me of COBOL. Either write POD or don't, but this style suggestion is silly either way. Number 29 seems to have forgotten that we have __END__ for this purpose. OTOH, suggestion 14 (about having a routine that logs debug messages) is something I consider to be Very Good Style that is often missed, and many of the other suggestions are quite good too. It's definitely worth looking at and considering -- just don't take it as some kind of definitive authority; many of the points it makes seem geared more toward verbosity and C-like-ness than clarity. "In adjectives, with the addition of inflectional endings, a changeable long vowel (Qamets or Tsere) in an open, propretonic syllable will reduce to Vocal Shewa. This type of change occurs when the open, pretonic syllable of the masculine singular adjective becomes propretonic with the addition of inflectional endings." — Pratico & Van Pelt, BBHG, p68 | [reply] |
Re: improving the aesthetics of perl code
by g0n (Priest) on Jan 21, 2005 at 14:26 UTC | |
Having maintained other peoples code for far too many years, the one thing I would say is comment. Lots and lots of comment. Not just:
which can be difficult to see in a long script, but
Or your own personal variant of it. Also, give variables reasonably explanatory names, and make fairly liberal use of whitespace. Those would be my suggestions anyway. Why not post a code snippet and let the monks bitch about it? :-) charles. Addendum: applause for wanting to make your code easier to read though - most people wouldn't bother! | [reply] [d/l] [select] |
by TimToady (Parson) on Jan 21, 2005 at 16:36 UTC | |
No, that's not what it says at all. It says: Semicolon omitted in "short" one-line BLOCK.where "one-line" should be taken to include the curlies, and "short" should be taken to mean something that can be seen as a kind of visual "pill". So it's saying it's okay to omit the semicolon in but not in So for the most part you're basically in violent agreement with perlstyle. | [reply] [d/l] [select] |
by g0n (Priest) on Jan 21, 2005 at 17:07 UTC | |
| [reply] |
by tcf03 (Deacon) on Jan 21, 2005 at 14:46 UTC | |
| [reply] [d/l] |
by jdporter (Paladin) on Jan 21, 2005 at 22:44 UTC | |
That's totally wrong. Ordinarily, you either read from a file a line at a time, using while, or you read the entire (rest of the) file by assigning to a list. You can't meaningfully combine the two approaches, which is what it looks like you've tried to do. Below is how to write your loop using the one-fell-swoop approach, with the other transformations included: Here's something similar but using a line-at-a-time approach: hth. | [reply] [d/l] [select] |
by pingo (Hermit) on Jan 21, 2005 at 16:23 UTC | |
Something like this, perhaps: I may be missing something blindingly obvious, though. :-) | [reply] [d/l] [select] |
by Tanktalus (Canon) on Jan 21, 2005 at 16:29 UTC | |
by jdporter (Paladin) on Jan 21, 2005 at 22:25 UTC | |
by sleepingsquirrel (Chaplain) on Jan 21, 2005 at 22:43 UTC | |
by wfsp (Abbot) on Jan 21, 2005 at 16:44 UTC | |
If a print statement is a bit unwieldy I often use join to get it to read better. I would have considered $ping instead of $p. I prefer a space either side of an = (sometimes you do too, sometimes you don't!). I think that 2 space tabs use less real estate than 4 if there is deep nesting.
foreach $_(@TMP)
} else { All very minor. | [reply] [d/l] [select] |
by g0n (Priest) on Jan 21, 2005 at 16:15 UTC | |
Anyone else? | [reply] |
by hardburn (Abbot) on Jan 21, 2005 at 22:23 UTC | |
I prefer to use map over foreach when possible:
But maybe that's just because I like writing LISP in Perl :) "There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni. | [reply] [d/l] [select] |
by BrentDax (Hermit) on Jan 24, 2005 at 09:56 UTC | |
Personally, I'd probably use a heredoc for this:
Of course, I wouldn't write HTML like that, but continuing in that direction will only send me way off-topic... =cut | [reply] [d/l] [select] |
by hardburn (Abbot) on Jan 21, 2005 at 22:19 UTC | |
For subroutine comments, use POD, not ugly flowerbox comments. Better still, name your subroutines and clearly label the input params, and your documentation will take care of itself. "There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni. | [reply] |
by Anonymous Monk on Jan 24, 2005 at 15:01 UTC | |
For subroutine comments, use POD,I disagree with that. POD is used to make user documentation. Unless you play silly tricks with =for or =begin, anything that you document with POD, will end up in the user manual. That gives you two options: POD is for the user, comments are for the coder. And while they may be the same, they usually aren't. | [reply] |
by hardburn (Abbot) on Jan 24, 2005 at 18:57 UTC | |
by Anonymous Monk on Jan 24, 2005 at 15:21 UTC | |
Lots and lots of comment.When I was a programming novice, I didn't comment much. When I was a programming acolyte, I commented a lot. Now I am a programming master, and I try to minize the comments in my programs. Why, you may ask. Doesn't more comments make it easier to understand what code does? Yes, it does - however, comments don't solve the cause. Comments fix the symptoms. Comments are used when the program isn't clear. If you write clearer programs, you need less comments. It's like taking painkillers. They cause the headache to go away, but it's better to stop banging your head. | [reply] |
Re: improving the aesthetics of perl code
by rlb3 (Deacon) on Jan 21, 2005 at 14:23 UTC | |
One of the best things you can do is look at the perlstyle pod if you have a Unix or Unix like system. From the command like type: perldoc perlstyle. Or you can find it HEREI wish I could help more but I'm still tring to understand all this myself. 2006-09-14 Retitled by planetscape, as per Monastery guidelines
| [reply] |
Re: improving the aesthetics of perl code
by perrin (Chancellor) on Jan 21, 2005 at 15:15 UTC | |
| [reply] |
Re: improving the aesthetics of perl code
by trammell (Priest) on Jan 21, 2005 at 16:08 UTC | |
| [reply] |
by tcf03 (Deacon) on Jan 21, 2005 at 20:06 UTC | |
| [reply] |
Re: improving the aesthetics of perl code
by Ytrew (Pilgrim) on Jan 22, 2005 at 06:20 UTC | |
To sum up: make every aspect of your code a form of documentation. Don't use $_ when you could use a more meaningful name. Try coding defensively: will your current line of code still make sense after someone else throws 100 lines badly formatted code in the middle? If so, then the code is probably self-documenting enough. The most useful book on programming that I ever read was called "Debugging C": it taught that the most effective way to debug programs was to write them in a simple, straightforward way in the first place. It's very good advice. Good luck! Keep practicing! The first step towards better style is trying to learn!
-- | [reply] [d/l] [select] |
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. | [reply] |
by Ytrew (Pilgrim) on Jan 22, 2005 at 22:33 UTC | |
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).
| [reply] |
by Anonymous Monk on Jan 23, 2005 at 00:42 UTC | |
by Ytrew (Pilgrim) on Jan 23, 2005 at 21:23 UTC | |
| |
Re: improving the aesthetics of perl code
by gaal (Parson) on Jan 22, 2005 at 17:57 UTC | |
Explain more code to your co-workers. | [reply] |
Re: improving the aesthetics of perl code
by Anonymous Monk on Jan 21, 2005 at 18:13 UTC | |
| [reply] |
by CloneArmyCommander (Friar) on Jan 21, 2005 at 21:57 UTC | |
| [reply] |
by TimToady (Parson) on Jan 22, 2005 at 17:23 UTC | |
Unfortunately, there's no decent word that I know of for an acronym definition made up after the acronym. I believe the OED says "later rationalizations" about Perl's glosses. I think "fakeronym" is cute, but it seems like we should really make up an acronym for it. We could call it a LATE, but I don't know what that stands for yet... :-) | [reply] [d/l] |
by Anonymous Monk on Jan 22, 2005 at 21:35 UTC | |
by CloneArmyCommander (Friar) on Jan 27, 2005 at 19:34 UTC | |
Re: improving the aesthetics of perl code
by jdtoronto (Prior) on Sep 13, 2006 at 18:14 UTC | |
I see others have pointed out some different approaches to dealing with filehandles and the like. Only other suggestion I would make is to use CGI more effectively by not printing HTML directly - always use CGI/ And finally, use perltidy. I have it in the toolbox in my Komodo so I can run it over my code any time. It actually mkes it easier to spot problems when I can see thngs like the indentation level go wrong! It also gives me a consistency of style I am too lazy to get any other way. | [reply] [d/l] [select] |