Ïðèãëàøàåì ïîñåòèòü
Ñóìàðîêîâ (sumarokov.lit-info.ru)

Preface

Previous Table of Contents Next

Preface

"Worldwide, there are well over 200 billion lines of software that are fragmented, redundantly defined, hard to decipher, and highly inflexible . . . organizations run the risk of being mired down by a mountain of legacy code."

—William Ulrich, Legacy Systems: Transformation Strategies

graphics/pr01.gif

Congratulations! Let's say you just graduated with a computer science degree and now, bucking the economic trend, you've landed a job at a prestigious company with a large information technology department. You're going to be replacing Bill, a programmer who won the lottery and was not seen or heard from again, save for a postcard from Puerto Vallarta two weeks later. Your coworkers warn you not to mention the postcard to your supervisor. You sit in Bill's cubicle throwing out pieces of vendor advertising left in the center desk drawer, thinking about how you're going to apply the elegant principles and sublime paradigms that professors inculcated in you at college. Just then, your supervisor arrives and, leaning over your shoulder, taps at your keyboard, bringing up a file.

"This is the last program Bill was working on. We think it's almost finished. We're behind schedule, so see if you can get it done by Thursday at the latest."

As he leaves, you look at the program's tangle of misindented lines and cryptic variable names, searching for comments, but the only ones you can find read, "XXX–Must change" and "Kludge!–But should work." You wonder whether this is a corporate hazing ritual, but your instinct tells you otherwise.

Welcome to the real world.

In the real world, you're lucky if you get to spend all your time developing one new program after another. Much of the time you'll have to deal with someone else's. In the real world, programmers take over responsibility for programs written by people they might not know, like, or agree with. Even if you're fortunate enough to avoid this situation, you'll still have to maintain your own code; and one day you're going to look at something you wrote two years ago and ask, "What idiot wrote this?" Thereby arriving at more or less the same situation as the less fortunate programmers.

This book is about taking over Perl code, whether written by someone else or by yourself at a time when you were less wise about maintainability. Many problems of code inheritance are common to all languages, but I have noticed them especially in Perl.

Why does Perl tend to foster maintenance issues? The answer to this is the dark side of Perl's strength and motto: "There's More Than One Way To Do It" (enshrined in the acronym TMTOWTDI). Perl provides so many ways to do it that someone else quite possibly picked one that wasn't your way, or might have used several different ways all in the same program.

The medical metaphor for this book stems from the rather drastic nature of the work we do as maintenance programmers. Often we must perform triage, deciding what code is worth saving and what is beyond redemption. Frequently we only have time for first aid, applying a field dressing to a ruptured program. We also have a hard time explaining our bills to the client. There may not be a Hippocratic Oath for programming, but it wouldn't hurt to come up with one.

I wrote this book because I kept finding myself telling my students, "I'm going to teach you how to program Perl well, but I'd have to teach you a lot more before you could take over a program that wasn't written well, and you wouldn't appreciate taking that much time away from learning how to write good programs of your own." So I've written a book to fill that need.

Perl is to computer languages as English is to human languages: bursting with irregular verbs, consistent only when it's convenient, borrowing terms from other languages to form a great melting pot of syntax. Most computer languages are described in terms of some kind of functional niche (Pascal: teaching computer languages; FORTRAN: numeric analysis; Prolog: rule-driven expert systems; etc.). Perl's makers simply describe it as "a language for getting your job done." (See the preface to [WALL00].) Perl hosts a fantastic conglomeration of syntactic devices that allow a programmer from virtually any background to find a familiar foothold for learning the language.

The full picture isn't as chaotic as this might imply: Larry Wall and others have done a brilliant job of tying together these eclectic devices into a framework that has an essential beauty. Therefore, just as the British speak of a "BBC English," while many people program Perl with, say, a LISP or C accent, there is something approaching an "accentless Perl" style that leverages the language's features to their best advantages. I will show how you can "speak Perl like a native" in order to optimize the maintainability of your programs.

It's true that you're officially allowed to program Perl in "baby talk" and Perl gurus have promised "not to laugh." (See the same preface.) But by the same token, while aviators call any landing you can walk away from a good one, what I'm doing in this book is helping you avoid having your pilot's license revoked.

Perl is like those people behind the travelers' help desk in airports; it's very good at understanding you no matter how poor your command of their language is. Because there are so many ways to write a Perl program that is not only syntactically correct (Perl makes no objection to running it) but also semantically correct (the program does what it's supposed to—at least in the situations it's been tried in), there is a wide variety of Perl programming styles that you might encounter, ranging from beautiful to what can charitably be described as incomprehensible.

The savvy among you will take that information and ask, "Where do my programs fit on that scale?" Because someone else may end up inheriting your code, and you'd prefer that they not end up sending it to authors like me as bad examples to go in books like this. See Chapter 5 for more advice on avoiding scorn.

If your experience or image of Perl is limited to short, mundane scripts, this book will appear to be overkill. I want you to know that Perl can quite easily accommodate large—as in tens of thousands of lines of code, multiple modules, and multiple programmers—projects. Projects of the size that demand rigorous requirements, documentation, and testing. If you're used to Perl programs escaping that sort of attention, I believe that is partly the result of a misperception of the role and power of Perl.

For example, if a C program is written to fulfill some requirement and turns out to be 1,000 lines long, then the common reaction is, "This must be serious . . . we'd better have code walkthroughs, acceptance testing, operational readiness reviews, and static code analyses. Oh, and don't forget the Help Desk training and documentation."

But if a Perl program that fulfills exactly the same requirements weighs in at 100 lines (and 10:1 is a typical compression ratio for C code to Perl), the reaction is more likely to be, "Ah, a simple utility . . . and in a plebeian scripting language to boot. Just plunk it in the delivery directory and get on with the next task."

When a Perl program reaches the 1,000-line mark, however, the honeymoon is probably over. Much of what I have to say addresses large programs. Chapter 3 in particular will show you how to get the respect of development teams who are used to putting everything through regression testing.

Please also see my earlier book with Ed Wright, Perl Debugged (Addison-Wesley, 2001) for more advice on good practices for developing and debugging Perl programs.

    Previous Table of Contents Next