The standard Delphi way (from http://www.econos.de/delphi/cs.html) without
begin/end would look like this
if x=1 then
Say(1)
else if x=2 then
say(2)
else if x=3 then
say(3)
else
say(4);
which turns out much better than a cascading indent, and avoids looking
like a nested conditional statement, like Steve's second example. If it's
more complex, and needs begin/end -then:
if x=1 then
begin
Say(1);
Say(10);
Say(3);
end
else if x=2 then
begin
say(2);
say(2);
end
else if x=3 then
say(3)
else say(4);
but use them only when needed. An exception I make is when nesting
conditional statements - even if a begin/end is unnecessary, I use it
anyway, since it's very easy to make a mistake, like in this little
confusingly distorted piece of code:
if x>4 then
while i < 0 do
if y=4 then
y:=0
else
y := -1;
Neven's examples go along with that standards doc (which is by no means the
official Delphi standards guide, but is generally accepted among many
Delphi users), except of for his 'conclusion' ;) Even the exception with
putting a 'begin' on the same line with an else is mentioned there.
I agree with Steve about clutter, but I'd rather have the clutter if it
helps me understand the code. I like to be able to glance at code and see
which 'begins' align with which 'ends'. If I really wanted to avoid
clutter, I'd use some horribly compact language like C ;)
Ed
At 23:05 18/09/2001 +0000, you wrote:
>---------- Original Message ----------------------------------
> >BTW any preferences for "begin end" layout?
>
>Most definately:
>
>if blah then
>begin
> DoStuff;
> if Blat then
> begin
> DoSomething;
> DoOtherStuff;
> end
> else
> DoSomethingDifferent;
>end;
>
>As you can see I totally agree with the other suggestions of using 4
>spaces for indenting - MUCH easier to view the program flow and have been
>using it for years now.
>
>Also, by placing 'begin' in the same logical indentation as both the 'if'
>and the 'end', the logical flow shows up better. this goes also better
>with try/finally statements
>
>if blah then
>try
> AttemptSomething;
>finally
> FreeSomething;
>end;
>
>You will have also noted with the first example, that else is ALWAYS on a
>line of it's own. Note there is no ELSEIF statement in Pascal. The
>transfers messy statements like
>
>if x=1 then
> Say(1)
> else if x=2 then
> say(2)
> else if x=3 then
> say(3)
> else say(4)
>
>
>...bastard of a statement. try the following:
>
>if x=1 then
> Say(1)
>else
>begin
> if x=2 then
> say(2)
> else
> begin
> if x=3 then
> say(3)
> else
> say(4);
> end;
>end;
>
>This allows you to see what exactly will happen in any given situation and
>just as important, you know which 'if' each 'else' responds to.
>
>Steve
>---------------------------------------------------------------------------
> New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
> Website: http://www.delphi.org.nz
>To UnSub, send email to: [EMAIL PROTECTED]
>with body of "unsubscribe delphi"
>Web Archive at: http://www.mail-archive.com/delphi%40delphi.org.nz/
---------------------------------------------------------------------------
New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
Website: http://www.delphi.org.nz
To UnSub, send email to: [EMAIL PROTECTED]
with body of "unsubscribe delphi"
Web Archive at: http://www.mail-archive.com/delphi%40delphi.org.nz/