A
Antoon Pardon
Op 2004-07-08 said:Antoon> I would prefer to indent sucu code as follows:
Antoon> while True:
Antoon> code
Antoon> if condition: break
Antoon> code
Antoon> Why? because the loopbreaker is IME part of the
Antoon> loopstructure, not an ordinary if statement in the
Antoon> loopbody.
It's still going against the underlying block structure, so a source
code "prettifier" would screw it up even if Python allowed it.
That is the reason I don't use prettifier. Indentation IMO should
illustrate the logic of the algorithm, not the structure of language
you wrote the algorithm in. Sure these two often go together but
sometimes they don't. In that case I find it a real pain that
python forces me to illustrate the structure of the language
and not the structure of the algorithm like when you have a
loop with the termination condition not at the top.
Python23/Tools/Scripts/pindent.py (DOS)
/usr/share/doc/python2.3/examples/Tools/scripts/pindent.py (Debian)
Antoon> This is why I prefer free form languages. If you need
Antoon> certain control structure that is not directly in the
Antoon> language but can be simulated, you can still indent your
Antoon> code according to the structure you have in mind as
Antoon> opposed to the structure that is forced upon you by the
Antoon> simulation.
You realize that this approach is slightly heretical, do you?
Indentation should follow the "real" block structure *exactly*,
anything else is an error that confuses the reader.
Why should the indentation follow the block structure instead of
structure of the algorithm
The while 1 - break structure doesn't even need extra clarification,
because the break is typically in a very idiomatic place - right after
the assignment in the beginning, or at the very end if it's
semantically a repeat-until loop (which are rare).
I have loops that don't conform to the above description. Like
the following.
loop
shift = 0
delta = 1
full = 1L
while (self.bits & full) == 0:
full = (full << delta) + full
shift = delta
delta = delta * 2
breakif shift == 0:
self.offset = self.offset + shift
self.bits = self.bits >> shift
And this is just a simple example. Indented like I did here,
clearly shows where the loopbreaker occurs, which is less
clear when loopbreaker is indented the same way as the rest
of the loop.
So why should I be forced to indent in a way that doesn't
reflect the structure of the algorithm, simply because
the language is not rich enough.