[Courses] Style (was Lesson Three: Basic Declarations & Expressions)
Akkana
akkana at shallowsky.com
Sat Oct 12 14:24:00 EST 2002
Jenn Vesperman writes:
> I _strongly_ discourage style pedanticism. Everyone who programs
> develops their own style that works for them.
>
> Everyone's mind works differently. Everyone's -sight- works differently.
[ ... more sensible advice snipped ]
Jenn is absolutely right, and here's another reason why: if you ever
end up collaborating with other programmers (either as an employee,
or as a volunteer on an open-source project), I guarantee you'll
eventually find yourself working in a file that's formatted to
something other than your own preference. Rather than wasting
time on flamewars about whether it's good or evil to put the brace
on the same line or its own line, it's usually best just to agree
on a style that the whole project will use.
For your own code, of course, use whatever you find easiest to read.
If you're starting the project, you get to set the style rules.
The only style point on which I might be inclined to be pedantic is
mixing of tabs and spaces. Using all tabs is a valid choice; using two
or three or four or eight space indents are all valid choices. But
if you mix spaces and tabs to achieve indentation, I guarantee that
regardless of how you set your tab stops, a sizeable number of readers
of your code will have tabs set to something else, and your indentation
will look awful. And the "Tab stops have always been eight spaces"
crowd will hammer it out all night with the "I set tabs to four spaces
because that's easier to read" crowd, and they'll never convince each
other, and meanwhile the code will look wrong to everyone because a
"4-space-tab" person edited an "8-space-tab" person's code and you
ended up with a file that has bits of both, looking right to none.
In emacs, (setq tabify nil) tells it not to insert tabs (use
spaces for everything). There's also (setq indent-tabs-mode nil),
and I'm not sure what the difference is. In vim, it's set expandtab.
Oh, one other useful thing: if you want to enforce a spacing for a
particular source file, you can specify the style in an emacs modeline
at the top of the file: for example,
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
Emacs will pick this up and use it in its C code formatter, so any new
code you type in c-mode or c++-mode automatically gets indented to the
right level. Is there a way to get vim to read these modelines?
...Akkana
More information about the Courses
mailing list