|Pathologically Eclectic Rubbish Lister|
(OT) Editor-neutral code formattingby Ovid (Cardinal)
|on Feb 07, 2002 at 01:01 UTC ( #143767=perlmeditation: print w/replies, xml )||Need Help??|
Recently, I've had some discussions with grep regarding coding standards. Some of the standards have been fairly straight-forward. Code must use strict, run clean under warnings (unless special permission is sought under exceptional cases), encapsulated functions, etc. Those are pretty standard, but grep noticed that my coding style has changed a little bit and he asked me about it. Since I've been evaluating several different editors such as Homesite, Textpad, vim, etc, I have adopted an "editor-neutral" coding style. grep was considering making this a standard, but so many programmers consider this a near-religious issue that I am concerned that we won't see enough benefit compared to programmer gripes.
Here are the basic points of the style that I am using:
1. Indent data structures and arguments to subs rather than leaving them hanging off to the right:
Instead, I do this:
2. Curly braces should be aligned vertically over themselves on the same scope, rather than trailing off the end of a line. Quick, where's the missing brace:
If you vertically align corresponding braces:
To me, the problem just leaps out at me. As I sometimes wind up with really complex structures, vertical alignment of the braces seems to help.
3. Indentation can start with tabs for leading whitespace, but embedded whitespace must be spaces.
That might sound a bit weird, but many editors have tabs at different settings. Maybe they're 8 spaces. Maybe they are 4 or 2 (I've heard rumors of them viewing as 6 spaces, what heresy!). By ensuring that only leading whitespace (/^\s+/) is tabs (spaces are okay, too), but all embedded whitespace is comprised of spaces, you ensure that code won't wind up with a bizarre visual appearance, depending on the editor. For example, I recently encountered the following code (tabs are changed to spaces so you can see what I mean):
Now, that's pretty ugly, but that's with tabs rending four spaces in width. I opened this up to work on it in an editor that renders tabs as being eight spaces wide:
That's an even worse mess. I took some time to convert it:
Note that rule 3 doesn't do that much good if rule 1 is ignored.
Now, here's the sad truth: I don't like this. Somehow, it grates against my sense of aesthetics. So why am I doing it? Because I think that in the long run, code is much cleaner and easier to read, regardless of the editor one chooses. Further, I feel that scoping issues will be easier to track down like this.
Here's the rub: is enshrining this into standards the way to go? I think many programmers would rebel. I really don't think we'll get much benefit. grep wants the standards, though, to ensure that we don't wind up with a bunch of different programmers reformatting everyone else's code every time they work on it (yes, I'm guilty of that :). Any thoughts?
Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.
Back to Meditations