Документация
HTML CSS PHP PERL другое

4.5 Restyling

 
Previous Table of Contents Next

4.5 Restyling

What should you do when you're presented with an existing program whose author didn't follow your favorite style guideline and whose layout looks like a Jackson Pollock painting?[7]

[7] Make than a Pollock imitation. Uri Guttman referred me to an article showing how genuine Pollocks exhibit fractal ordering: [TAYLOR02].

Several attempts have been made at writing beautifiers, but the clear winner as of publication time is perltidy,[8] by Steve Hancock. You can also download perltidy from CPAN. Use Perl::Tidy as the module name to install.

[8] http://perltidy.sourceforge.net/

perltidy writes beautified versions of its input files in files of the same name with .tdy appended. It has too many options to document exhaustively here, but they are well covered in its manual page. Its default options format according to the perlstyle recommendations. My personal style is approximately equivalent to:


perltidy -gnu -bbt=1 -i=2 -nsfs -vt=1 -vtc=1 -nbbc

As good as it is, perltidy can't make the kinds of optimizations that turn the layout of a program into a work of art. Use it once to get a program written by someone else into a "house style," but don't let your style be a straightjacket. For instance, the preceding settings generated this formatting:


return 1 if $case_sensitive[length $word]{$word};

return 1 if $case_insensitive[length $word]{lc $word};

But I could further beautify that code into:


return 1 if $case_sensitive  [length $word]{   $word};

return 1 if $case_insensitive[length $word]{lc $word};

Note that you can separate subscripting brackets from their identifiers by white space if you want. In fact, Perl is incredibly permissive about white space, allowing you to write such daring constructs as:


$   puppy = ($dog     ->offspring)[0];

$platypup = ($platypus->eggs)     [0];

although whether you really want to mix left and right justification without any, er, justification, is questionable.

4.5.1 Just Helping Out?

If you are modifying code that belongs to someone else, but not assuming responsibility for it, follow its existing style. You'll only annoy the author by "helpfully" reformatting their code. That's equivalent to going into the bathrooms at a house you've been invited to and changing the way all the toilet paper rolls are hung.

    Previous Table of Contents Next
    © 2000- NIV