Thoughts on Formatting Code

Tom Hukins tom at eborcom.com
Fri Aug 21 02:24:33 PDT 2009


Everyone has an opinion on how best to lay out code:  spaces or tabs?
2, 4 or 8 character indentation?  cuddled or uncuddled elses?

I find discussing these things dull.  I have my preferences and I have
good reasons for them.  Other people have different preferences and
have good reasons for them.

This morning, in a fit of mild Friday distraction, I've been thinking
about forming conscensus on coding style, or dealing with coding style
preferences as a group, either at work or on an open source project.

I've heard of people using coding standards, but these seem a bit
restrictive.  I worked at one company where we devised a set of coding
guidelines that suited us, but I find in most workplaces that
unwritten guidelines emerge informally.

I've heard stories about people using perltidy as a commit hook in
version control systems to ensure conformance to house style.
Individual developers can apply their own perltidy preferences to code
after checking it out, so they edit it in the way they prefer, while
code in the repository adheres to a policy.  I wonder how well this
works in practice.

Maybe Perl::Critic would help, or policy tests for the code.  I like
policy tests that check all code uses strict, but I question their
value for anything contentious.

Whenever you understand any set of rules well, you know when and why
to break it.  So I dislike universal policies.  Except, maybe for a
policy to reject universal policies.

Whenever I edit existing code, I try to retain the original author's
style, or maybe improve it slightly if I find it horrid.  Often I find
poor style (yeah, it's subjective, but I mean really bad code)
indicates poor thought processes and unreadable code regardless of any
reformatting.

Generally, I like the idea of making sure people who can edit your
code have a rough idea of what they're doing and usually play well
with others.  I suspect this reduces the need to suggest or enforce
policy.  But everyone makes mistakes:  I like having tools that point
out when I might have done something wrong.

So I wonder what the rest of you think, and more importantly how
you've dealt with such things.  What worked and what failed?

Tom


More information about the MiltonKeynes-pm mailing list