Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Maintainable code is the best code

by theorbtwo (Prior)
on Oct 03, 2001 at 03:21 UTC ( #116301=note: print w/replies, xml ) Need Help??


in reply to Maintainable code is the best code

my %hash1; #Hash for stuff my %hash2468; #hash for even nos my@blahblah; # Is this even necessary? my %someOtherKindOfHash; # Maybe stuff here my %h3; #you might want this hash, too.

That is a complete mess of a declaration of global variables. Yet, I see this over and over in code I'm given. Instead, why not do something like

# Predeclared hashes. Used throughout the script. my %hashForStuff; my %hashForEvenNumbers; my %hashForEvenMoreStuff; my %hashThatIThinkIMightNeed; # Is this even necessary? # my @blahblah;

I like your post in general, but have one thing to say to this: Noooooo!

Take a look at what you're replacing this with. It's even worse then what you started with. "hashFor" is just 7 characters that repeat "%". No gain except making every expresion that they appear in that much longer. And longer code is harder to understand then shorter code that does the same thing (in general). The mind boggles when it sees long things. The fact that much of the code isn't really doing anything but telling you what you already know doesn't come into effect until later. It's the same reason that obvious comments make unreadable code.

A good replacement might be:

# Globals my(%stuff, %morestuff); my %evens; #For even numbers # Some other stuff that I don't feel like manualy coppying # and then making a comment because mozilla's copying seems # to be broken (or perhaps I am)
Everything is grouped by use, names are descriptive, but no longer then is acatualy useful.

However, both of us are missing the real problem here. To quote Linus: "To call a global function 'foo' is a shooting offense". WTF is "stuff"? Telling the reader what stuff is is an absolute neccessity. You should always be able to tell what a varaible should contain, and with container varibles, by what it's indexed. If %evens is "for even numbers", why isn't it an array in which only even indeces are used? Perhaps "for even numbers" isn't really what the author meant, but rather "of even numbers".

I reccomend everybody read Linus' coding standards. Not because I think Linus is a God, but because they're damm good coding standars. (If you don't have the kernel source on your box, BTW, look at the Linux Cross Reference.)

Thanks,
James Mastros,
Just Another Perl Abbot

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://116301]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2020-10-25 22:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (249 votes). Check out past polls.

    Notices?