Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Meaning of "Clean" Perl code

by neversaint (Deacon)
on Jul 25, 2005 at 14:34 UTC ( [id://477838]=perlquestion: print w/replies, xml ) Need Help??

neversaint has asked for the wisdom of the Perl Monks concerning the following question:

Most respected gurus,

I often bump up with a term "Clean" code.

Coming from a non Computer Science background. I wonder what does it mean?
Is there any definition for it?
Does it has different meaning for different programing languages, e.g. Perl Vs Python Vs C.?

Especially when Perl is often dubbed as "line noise", then how would one describe/defend the meaning of clean Perl Code to the nonbeliever.

Replies are listed 'Best First'.
Re: Meaning of "Clean" Perl code
by eyepopslikeamosquito (Archbishop) on Jul 25, 2005 at 15:21 UTC

    It is perhaps easier to describe "unclean" code. For example:

    • Code with lots of code smells (see also wikipedia Code_smell)
    • Lots of magic numbers
    • A 5000-line main program
    • A sub that reads and writes global variables
    • A sub that does not have a single purpose; for example, instead of a sin function and a tan function, someone defines a sin_and_tan function
    • Some of a sub's parameters are not used
    • A sub that is 1000 lines long, aka "Big-Arsed Function"
    • Duplicated code
    • Code with gaping security holes
    • Code that leaks resources
    • Code that is not thread-safe or signal-safe

    Conversely, some clean Perl code attributes are:

    • strict-safe
    • warnings-safe
    • taint-safe
    • Good variable naming
    • Minimize variable scope
    • Prefer lexicals to globals
    • Good commenting
    • Consistent indentation and visually pleasing layout
    • Easy to understand
    • Simple, Clear, General
    • Easy to use module interfaces
    • Comprehensive test suite

    Finally, TheDamian's new book Perl Best Practices provides much sound advice on writing clean Perl code.

    Update: See also Is it correct? by GrandFather:

    • Does it work correctly?
    • Do you understand it?
    • Would anyone else understand it?
    • Will you understand it in a month's time?
    • Does it strike a good balance between terseness and verbosity?
    • Could you make changes to it without it being likely to break in unforeseen ways?
    • Is it fast enough?

    Update: See also: On Coding Standards and Code Reviews and Re: I need perl coding standards (Coding Standards References)

Re: Meaning of "Clean" Perl code
by japhy (Canon) on Jul 25, 2005 at 14:50 UTC
    This is simply my own interpretation. I deem my code clean when:
    • The physical layout of the program is consistent (internally and with my other programs). Module uses (including strict and warnings) are at the top, followed by the program flow, followed by subroutines (declarations excluded), and then the end of the program. Below is POD as needed. My indentation is consistent throughout, my brace style is consistent, etc. There shouldn't be a chunk of code that looks out of place.
    • All variables have the proper scope, and are declared when it is most fitting to do so.
    • No code is obfuscated, and any code that is less than intuitive is commented clearly.
    • The code is structured in paragraphs. I try to keep the number of consecutive non-empty lines to a sensible number. This way, comments can act as "headers" to my paragraphs of code.
    Argument checking is done in most cases, and if I'm writing a module, I'll always use Carp's functions to raise errors.

    I've probably left out some concepts, but it comes down to this: would I feel confident showing my code to my fellow developers (or peers in general), and would I expect them to understand the operation of my code without me intervening?


    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
Re: Meaning of "Clean" Perl code
by Happy-the-monk (Canon) on Jul 25, 2005 at 15:28 UTC

    Depending on your sense of homour, you might want try to use Acme::Bleach to create really clean code.

    cheers, Sören

Re: Meaning of "Clean" Perl code
by ikegami (Patriarch) on Jul 25, 2005 at 14:48 UTC
    It's more of a relative thing: Something is "cleaner" than something else. There's no threshold beyond which something is considered clean or unclean. I suppose the definition of "cleaner" is "easier to maintain". Many thing can contribute to cleansiness, but the list is subjective. I'd say the foremost item on that list is consistency in style (whatever that style may be).
Re: Meaning of "Clean" Perl code
by Ben Win Lue (Friar) on Jul 25, 2005 at 15:29 UTC
    My definition of "clean" would be "absence of dirt"!

    And dirt would be:

    1. Obsolete code. Code that was used before the world changed, but nobody cleaned it up
    2. Misleading comments
    3. Ugly code that tries to cope with the changing world where to complete redesign would have been cleaner
    This list implies, that code becomes unclean by aging. On the other hand, even fresh code is not always clean....
      Probably obvious, but unsecure code is also dirt .
Re: Meaning of "Clean" Perl code
by samizdat (Vicar) on Jul 25, 2005 at 16:02 UTC
    While the suggestions by other posters have much merit, to me, "clean code" is an elegant expression of an algorithm that stands by itself.

    If I need to add comments, beyond describing the mechanism I'm implementing and its data structures, I'm writing dirt and it's time to take a break before I start burying bugs that'll take me days to dig out.

    I stay away from a lot of Perl's MTOWTDI features because I'm not comfortable with their readability. I probably go way too far in that direction, but I prefer that my bricks be consistent and interchangeable more than being able to leap tall buildings at a single bound.

    At my workplace, we have a requirement that all tools and data be reproducible, maintainable and recoverable for thirty years. That affects one's perspective considerably. :D
Re: Meaning of "Clean" Perl code
by Anonymous Monk on Jul 25, 2005 at 14:43 UTC
    There's no definition of "clean code". Code is labelled (subjectively) as "clean" to imply (a lot of) other code is "unclean". And thus that such code is Bad(TM).
Re: Meaning of "Clean" Perl code
by tphyahoo (Vicar) on Jul 25, 2005 at 17:26 UTC
    My little laundry list goes:
    • Use warnings
    • Use strict
    • Use taint if it's a web app.
    • Less than 5 global variables (or substitute your own *small* integer limit)
    • sane use of whitespace
    • <controversial>use fields if it's object oriented</controversial>
Re: Meaning of "Clean" Perl code
by tlm (Prior) on Jul 26, 2005 at 04:29 UTC

    As you can see, there are as many definitions of "clean" as there are programmers (which means, according to my definition of "clean", that "clean" is not a very clean concept). Anyway, for me a "clean design" is one that exhibits, few extraneous dependencies (between its parts, between it and external modules, between the app and the OS, between the app and the phases of the moon, etc.). I.e. loose coupling.

    the lowliest monk

Re: Meaning of "Clean" Perl code
by DaWolf (Curate) on Jul 25, 2005 at 21:04 UTC
    I come from a non Computer Science background too.

    In MY (as strict as it gets) opinion, clean code is a code that is easily understable. You just read through and understand everything that's going on. It's a matter of style.

    I believe people often refer to Perl as noise because of one of it's main characteristics: TMTOWTDI, therefore you can bump into a code that does something you understand in a manner that you don't.

    Just my 2 cents.
Re: Meaning of "Clean" Perl code
by bunnyman (Hermit) on Jul 25, 2005 at 19:05 UTC
    If someone states that Perl is just "line noise," they are probably so closed-minded that they will not allow themselves to be convinced otherwise. At least, that is my experience.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (6)
As of 2024-03-29 10:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found