indent style for D

H. S. Teoh hsteoh at quickfur.ath.cx
Sun Jan 29 15:19:20 PST 2012


On Sun, Jan 29, 2012 at 02:32:48PM -0800, Jonathan M Davis wrote:
> On Sunday, January 29, 2012 14:27:29 H. S. Teoh wrote:
[...]
> > I always code with tab stops set to 8 and line wrapping set to 80
> > columns.
> 
> That's not going to work very well with D. You tend to get far too
> many levels of indentation for that to fly. With static if (and
> potentiall multiple layers of static if), classes, templates, etc. all
> on top of the normal if statements and while loops and the like, and
> it can very quickly reach 3 - 4 indentation levels on simple code, let
> alone what more complex code has. It's that much worse when you use
> much in the way of functional-style programming and chain function
> calls, since then not are the lines indented a fair ways, but they
> tend to get long. I suspect that you'll find 8 character tabs and 80
> character columns to be a frustrating combination, but you can do
> whatever you want.

I used to use 2-space indentation for this very reason. I've written
Perl code with 16 levels of nesting complete with chained maps{} and
other such fun stuff.

But I've since changed my mind, and have come to believe that if a
function requires more than 3-4 levels of nesting, there is a
fundamental design problem with it, and it should be split into smaller
functions. This also helps you refactor code more easily as well as give
you a reason to think about how to better express what you're trying to
do, instead of just going on auto-pilot and using 8 levels of nesting
just because.

Optimizing compilers like gdc will auto-inline single-use functions
anyway, so this shouldn't be a problem even on the premature
optimization side. ;-)

Besides, one of the things I really love about D is closures... with
careful use of closures, almost any code can be reduced to 3-4 indents
at the most, usually only 2-3.


> Phobos uses 4 character tabs with a soft limit of 80 characters (on
> average, a line should fit within 80 characters, but it doesn't have
> to) and a hard limit of 120 characters (a line should _never_ exceed
> 120 characters). And that seems to be working fairly well for us.
[...]

Well, having worked in many different projects under different managers
with different coding styles at my full-time jobs, I've come to accept
that when writing code for a particular project, one should simply
follow whatever conventions existing code in that project uses, however
ugly it may seem. For my own projects, though, I use 8-space tabs and
80-column wrapping.


T

-- 
2+2=4. 2*2=4. 2^2=4. Therefore, +, *, and ^ are the same operation.


More information about the Digitalmars-d mailing list