perltidy

acme on 2007-09-18T15:36:36

I have a lot of Perl code. At work we have a lot of Perl code. I had to come up with a standard Perl style. It's not important that this is the best Perl style ever - instead it's important that this Perl style is good enough and this is the one I use with all my CPAN modules. I use Perltidy to reformat Perl code using the following .perltidyrc (mostly cribbed from Damian's in Perl Best Practices):

-l=78 # max line width is 78 cols
-i=4 # indent level 4 cols
-ci=4 # continuation indent 4 cols
-st # output to STDOUT
-se # errors to STDERR
-vt=2 # maximal vertical tightness
-cti=0 # no extra indentation for closing brackets
-pt=1 # medium parenthesis tightness
-bt=1 # medium brace tightness
-sbt=1 # medium square bracket tightness
-bbt=1 # medium block brace tightness
-nsfs # no space before semicolons
-nolq # don't outdent long quoted strings
-wbb="% + - * / x != == >= <= =~ !~ < > | & >= < = **= += *= &= <<= &&= -= /= |= >>= ||= .= %= ^= x=" # break before operators
-ce # cuddled elses

You may use this too. Or pick your own. If you send patches to my modules, I'll run Perltidy on it and all the code will have the same style ;-)


with a dash of critic

jk2addict on 2007-09-18T16:36:36

Perl Critic + tidy is a blessed thing. It's about the only way I can keep most of my code somewhat consistant, esp after long periods of inactivity.

perltidy is sanctioned babel

Eric Wilhelm on 2007-09-19T00:34:51

I must be the only person in the world who thinks it is absurd to see code formatting as a matter of personal preference.

If

    I just ( you know , like this )
format my English text in any old way,it
    gets awfully hard to read.There are standard grammatical conventions for written text,yet we try to reinvent them for programming
. See how silly it is to break before the operator
?And what's the deal with braces being non-tight
? ( I mean, we don't have space after an opening paren ( and before the closing one ) in English
. )

True, not everything in programming has a parallel to written text, but bracket and brace tightness does, as does the trailing operator.

Really, the only options perltidy needed was line-width and possibly indentation. Both of those really just translate into "our text editors suck" though, don't they?

I'm sure I'll never convince anyone, but making "wrong code look wrong" would be a lot easier if "same code looked same" (ala http://www.joelonsoftware.com/articles/Wrong.html -- though Joel's "real solution" seems a bit crack-fueled, the original goal was an honorable one.) What we're missing here is a logical argument as to why it should look one way vs another -- it gets dismissed as "preference", so we won't ever have that discussion and we'll continue to be babel'd on this very basic level.

I also don't really see the point in composing a config file out of a bunch of abbreviated commands followed by comments which are longer than the unabbreviated commands would have been.

Re:perltidy is sanctioned babel

btilly on 2007-09-19T01:07:39

The problem is that while the big decisions are obvious, the little ones are not. And there are lots of little decisions to make while formatting. While you can get people to agree on some of them, you can't get them to agree on all of them.

For example, based on your post you should not break before operators. However if you read Perl Best Practices, you'll find some excellent reasons given for why you should break before operators. Basically they have to do with how our eyes move when we skim through code, and they relate to something called the end weight problem.

So there is a difference, how do you resolve it? Well I'm willing to bet that Damian's thought a lot more about this topic than you have. I also find his reasons to be pretty good. But I'd not be surprised if you were willing to argue this particular point.

The result? Damian and you disagree on whether you should break lines before or after the operator. You have different personal opinions. We have three options. The first is that you can convince Damian. The second is that Damian can convince you. The third is that we have to live with the difference.

I guarantee that you're not convincing Damian. If you read Damian's book, he might convince you. But if you read Damian's book and you have strong opinions, I'd be shocked if you can't find another practice that you disagree with, that you won't convince him on. So now what? Oh right, you'll have to live with your differences.

Which is exactly the situation that you found so surprising.

Now there is a bigger reason for this, which is very non-obvious. I learned about it from Code Complete. And that bigger reason is that coding expertise is fragile.

Suppose we take someone who is familiar with a particular formatting style. Say, variable names are in CamelCase. You don't cuddle your braces. There is always a space between a paren and whatever is inside it. You use a 4-space indent. Now you plop them down in a code-base that is different. Variables names use_underscores. You cuddle your braces. You use a 2-space indent.

What happens?

Well it turns out that the transition is a shock. the constant minor changes interfere with that person's ability to achieve programming flow. It doesn't matter what the arguments pro or con for those differences are. It doesn't matter whether in the long run the new style might be slightly better. (Or not.) The immediate result is going to be that the programmer is suddenly much less productive. (Some people adjust easily. Some less so. Everyone has to work to adjust.)

And that is what really drives these religious debates. The reasons given for different styles are far less important than just having styles synchronize within a code base. But the decision you make is going to cause some people to win and some to lose.

Re:perltidy is sanctioned babel

Eric Wilhelm on 2007-09-19T19:43:51

...coding expertise is fragile. ... Suppose we take someone who is familiar with a particular formatting style. ... Well it turns out that the transition is a shock.

Exactly. Like I said: babel. The conflict comes from the subtle differences.

And that is what really drives these religious debates. ...

I would rather than the debate be based in reason than religion (call it semantics, but "religious debate" is, by definition, a contradiction of terms.)

Given one internally consistent system, which is logically defensible, we would not be having this discussion and perltidy would not have options.

Re:perltidy is sanctioned babel

btilly on 2007-09-19T20:51:10

I would rather than the debate be based in reason than religion (call it semantics, but "religious debate" is, by definition, a contradiction of terms.)


Actually it is not a contradiction of terms. It is a description.

Debates happen. Both sides bring arguments to bear, and argue. But people wind up arguing past each other, and after a while it becomes apparent that the answers are hugely important to people, but they cannot logically justify that importance.

Because this is so reminiscent of debates over religion, we call debates with these characteristics, "religious debates". (With the subtext being that the One True Brace Style a matter of theology.)

Given one internally consistent system, which is logically defensible, we would not be having this discussion and perltidy would not have options.


Oh how I wish you were right.

Unfortunately, if perltidy only offered one option, it would be ignored by all of the people who don't agree with that one option. It is only able to become popular because it is flexible.

And, truth be told, there is no one option that is right. For instance I already pointed out that there are good reasons to break lines before operators. However you can't do that in many languages (eg JavaScript, VB and Ruby), because they rush to end your line of code when you do that. So the ideal Perl style won't work in those languages.

The result? We're not about to see agreement on how to format code in our lifetimes.

Re:perltidy is sanctioned babel

slanning on 2007-09-19T09:47:58

I must be the only person in the world who thinks it is absurd to see code formatting as a matter of personal preference.

In French, it's conventional to put a space before the question mark -- like this ? They also don't seem to indent new paragraphs. ¡In Spanish, they mark exclamations at the front too! In Czech, we can see, that they put commas before "that" when the independent clause comes first (as in this sentence, so "we can see" isn't parenthetical). In German, they capitalize Nouns and sometimes in Phrases, extra Commas put.

So I think it's absurd that you think it's absurd that formatting is a preference. :) (Though you could probably argue that those languages are consistent "internally", so picking a format for Perl would only be equivalent to picking it for one of those languages. Maybe programming languages each need a few hundred years to settle on formatting conventions.)

Recently I've been experimenting a little with the formatting of my emails, as I don't like all of the "standard grammatical conventions for written text". For example,

Also, it takes frustratingly long for
 any kind of change we make
 to get into an actually-used version of Bricolage.
It was news recently that Emacs was having this problem,
and Debian notoriously had it. The result is that
 people who at first anxiously add a feature to the project
 subsequently lose interest as the feature sits there
 practically forever
 without seeing the light of day till years later.
We suffer badly from this.

(So I was trying to put one "phrase" per line, with continuations indented by one space.) They end up looking almost like poems, which I think makes them too flippant. It doesn't mean I have to accept the standard conventions, though.

I wonder why we even use text and punctuation much any more in programming. I'd think it'd be nicer to abstract those things away. (What was that project whose name I can never remember, S*****y, where you program by plugging images together?) I guess we're used to typing keys on a keyboard, though, so editing text is easiest.

Re:perltidy is sanctioned babel

Eric Wilhelm on 2007-09-19T19:59:29

Though you could probably argue that those languages are consistent "internally", so picking a format for Perl would only be equivalent to picking it for one of those languages.

Yes. And Perl is (quite arguably (on many levels)) linguistically based in English.

Maybe programming languages each need a few hundred years to settle on formatting conventions.

Maybe, but please name some French/ Spanish/ German/ Czech programming languages.

(So I was trying to put one "phrase" per line, with continuations indented by one space.) They end up looking almost like poems, which I think makes them too flippant.

Poems yes, but "poem" does not imply "flippant". Poetry has been quite influential throughout history (though we forget that thanks to technological advances like "fox news".) Poems are memorable because of the rhyme (or non-rhyme), beat, linebreaks, syntactical twists, etc. It's not much different than distending an array/hash declaration across multiple lines (in a tabular form), or adding breaks between chunks of statements within a block.

Re: perltidy is sanctioned babel

Maddingue on 2007-09-20T14:21:34

Maybe, but please name some French/ Spanish/ German/ Czech programming languages.

Rasmus Lerdorf (Denmark) designed PHP.

Bjarne Stroustrup (Denmark) designed C++.

Guido van Rossum (Netherlands) designed Python.

Yukihiro Matsumoto (Japan) designed Ruby.

Niklaus Wirth (Switzerland, german part) designed ALGOL, Pascal, Modula.

Jean David Ichbiah (France) designed Ada.

The Ericsson company (Sweden) designed Erlang.

French professors designed Prolog.

Norwegian professors designed Simula.

INRIA (France) designs OCAML.

etc, etc.

I could go through the thousands of programming languages listed in Wikipedia, but stil have some $work to do :-)

Here's mine

Relipuj on 2007-09-19T08:29:30

# Input/Output
--check-syntax # Ask perl to check the syntax of the file
--backup-and-modify-in-place # Dont output to another file
--backup-file-extension=.tdy.bak # Backup.pl.tdy.bak
--logfile # Save a log of what's going on

# Formatting
--output-line-ending=unix
--format-skipping # Skip formatting between #<<< #>>>

# Indentation
--tabs # Tabs = Indentation Level
--add-whitespace # White spaces to prettify
--add-newlines
--delete-old-whitespace # Don't bother with old indentations
--delete-old-newlines
--ignore-old-breakpoints
--maximum-line-length=128
--no-outdent-long-lines

# Semicolons:
--no-space-terminal-semicolon # No spaces before ;
--no-space-for-semicolon
--add-semicolons # Add them if missing before }
--delete-semicolons # And delete them on epmty statements

# Subscripts (, [, {
--continuation-indentation=8 # Pretify Indent
#--line-up-parentheses #! Doesnt work with tabs
--closing-token-indentation=0 # Same Indent Level as where is was opened
--no-indent-closing-paren
--opening-paren-right
--opening-hash-brace-right
--opening-square-bracket-right
--opening-token-right
--vertical-tightness=1 # Dont break before line end
--vertical-tightness-closing=0
-sot # --stack-opening-token, the long opt name doesn't work!
--comma-arrow-breakpoints=2

# Braces
--cuddled-else
--no-opening-brace-on-new-line
--opening-brace-always-on-right
--no-opening-sub-brace-on-new-line
--block-brace-vertical-tightness=0

# Quotes
--no-outdent-long-quotes
--trim-qw

# Code Blocks
--no-outdent-labels
--no-indent-closing-brace
--blanks-before-blocks
--blanks-before-subs

# Keywords/Functions
--no-outdent-keywords # Dont outdent keywords (loops)
--space-keyword-paren
--no-space-function-paren

# Comments
--indent-spaced-block-comments
--no-outdent-long-comments
--minimum-space-to-comment=8
--hanging-side-comments
--closing-side-comments # End of sub ...
#--closing-side-comment-prefix="## <--- EOf" #! problems with old files
--closing-side-comment-warnings # Warn if modifying an existing comment
--closing-side-comment-maximum-text=64 # Max 64 Chars
--blanks-before-comments

Wow!!

grinder on 2007-09-19T11:27:43

I'm impressed!

Did anyone ever tell you that you have too much time on your hands? :)

Re:Wow!!

Relipuj on 2007-09-19T21:28:50

man, you'd see my other config files and you'd say it took me several years to get them like they are... wait, it took me several years to get them like they are...

if you want some here's a list of my must have (they're in /etc/skel/ when i finish installing a new distribution):
- .bashrc
- .dircolors (very exhaustive)
- .fvwm/* (minimal theme)
- .indent.pro
- .inputrc (works good with a lot of apps)
- .links/links.cfg
- .lynxrc
- .mc/ini (with undocumented functions)
- .perlconsolerc
- .perlcriticrc
- .perltidyrc
- .screenrc
- .toprc
- .vimrc
- .Xmodmap (mapping keys from multimedia keyboards)
- .xrbd/xterm.xdefaults (because xterm is enough to run screen...)

If you are interrested in some just tell me, i'll post them here (or make an archive and post it somewhere if you want everything)

Re:Wow!!

steph on 2007-09-21T09:19:20

yep. I am. Thanks

Re:Wow!!

Relipuj on 2007-09-22T22:56:21

heh, in fact i wasn't really expecting someone to reply, yet you did. Give me a few days to comment the configurations files (when possible) and i'll post them back here, or in a .bz2 file somewhere on the net. Let's say next Sunday (sept. 30) you'll get all the files.

I just ask for the delay to clean up my config directory, it's a real mess in there and i don't have the time to sort everything right now.

Re:Wow!!

steph on 2007-09-24T09:02:13

Take your time. I asked because I was curious to see how people organize their things. Lately I seem to waste time in simple yet important tasks with a net result of less time to code :(. Having a working (and portable) set of config files is certainly an asset.

cheers --stephan

Re: perltidy

jmcnamara on 2007-09-20T11:38:49

You could shorten this by invoking all of the Perl Best Practice options using -pbp or --perl-best-practices.

John.
--

Missing wbb options

ask on 2007-09-21T23:10:21

We have one also based on pbp. I'm not quite sure how it evolved, but it's almost exactly the same.

One difference is that we have "!~ >= = | & = **= += *= &= >= ||= //= .= %= ^= x="

(didn't care to figure out the differences, sorry :-) )

A few other options we use:

--maximum-line-length=100 # be slightly more generous
--warning-output # Show warnings
--maximum-consecutive-blank-lines=2 # default is 1
--nohanging-side-comments # troublesome for commented out code

-isbc # block comments may only be indented if they have some space characters before the #
-ci=2 # Continuation indent is 2 cols

# for the up-tight folk :)
-pt=2 # High parenthesis tightness
-bt=2 # High brace tightness
-sbt=2 # High square bracket tightness

  - ask