| [reply] |
Using Super search with words 'style' 'good coding' 'good programming' should you provide you tons of useful nodes and advices (too many too mention).
Here are few :
- Keep it simple stupid
- Use strict (when writing them)
- Use explicit name (for the sub and the var)
- Document your code
- Make your code readable (it's even better than just documenting)
- Don't document your code TOO much (tilly convinced me on this one...)
- Be efficient (choose the best algo/structures)
- Test your code.
- Make your code upgradable (let you a chance to change any part of the design)
- Make your API clear (a lot of subtle error come from a misunderstood API)
- Subroutines should fit on a screen (old programming practice, but usually good. If it doesn't it should be possible to break into several smaller subs...)
All in one if I should give you only one advice, I would say :
Just take what you find clever/elegant/useful in other people way of coding and include it to your style.
(and I guess you'll find a lot to learn from the coders hanging in the monastery)
Any way if you are proud of it, and dare to show it to others,
It should mean that it's ok...
;-)
"Only Bad Coders Badly Code In Perl" (OBC2IP)
| [reply] |
This is what I've done: create a repository, although
most of my items are one line fragments and not sub-routines,
at present it's got a directory per idiom, but soon it'll
be in postgres and fairly cook.
Best practices is a big can of worms,
Ideally subroutines should be written like any other
code, concisely, legibly etc. Are you asking for
details on whether to prototype and such?
I kinda did functions at university in lisp, but in
learning the Perl way, I did books, and lo they were good:
- A pretty good book with some generic coding standards is
Kernigan and Pikes "Practice of Programming" chapter 2
seperates well-written from beautification.
- "Effective Perl Programming" also has some insights into
the matter, highlightig issues with prototypes.
- "Debugging Perl is quite useful on functions, and since
reading I've increased my use of return values to return
status.
- Damian Conway's "OO Perl" has two blindingly good chapters
on what you should already know & what you mightn't
already know. There is loads of goodies in there on AUTOLOAD and
gifts that can spice up your subroutines ;-)
The only verbal hints I got were, "it's script dummy,
check your failures and leave as soon as possible",
which works for me.
Now I write functions to be called from a 'jump table' a
hash of references to functions, my initial reason for
doing this was deprecated when I discovered AUTOLOAD. Other
than that I use subs to perfom one overall function and
further more low-level functions under that, I like abstraction
and subs to be no more than 60 lines 1.
1. Which is a marked increase from my Lisp days when functions
never grew longer than 4 lines.
--
Brother Frankus. | [reply] |
perlsub, perlmod and perlmodlib are definitly worth reading...
Greetz
Beatnik
100 nodes of pure crap
... Quidquid perl dictum sit, altum viditur. | [reply] |
Funnily enough the quote engine at the top of the page answered this in the way I feel is most important:
"Be consistant"
Recently I have spent quite a bit more time looking at other people's code than writing my own, so I've developed some pretty strong opinions about what makes good style. (I like subroutines to be less than about 40 lines long and I like them to have explicit return values). Nevertheless, the most important thing for me is consistency. Providing the style of code I'm reading holds throughout the whole program, I can adapt. If it changes for every subroutine with no discernable patter, I get s##tty
Ironically, consistency has to be very carefully balanced with actually improving your style :)
| [reply] |