1. Computing & Technology

Discuss in my forum

Zarko Gajic

Poll: Delphi Coding Styles - Begin/End Code Indentation?

By , About.com GuideSeptember 21, 2009

Follow me on:

in Polls :: After running a few coding challenges I've noticed that developers use various coding styles when it comes to Begin / End pairs and code indentation.

Some of the previous polls asked where do you place "begin": after then or on a separate line. 75% ( considered "correct" related to Delphi coding styles) put begin on a separate line.

This time let's see how you (and others) indent begin / end code blocks...

Some use (begin/end not indented related to if, code block indented):

if condition then
begin
  code
end;

some (begin/end indented related to if):

if condition then
  begin
    code
  end;

What is your begin/end code indentation style?

Btw, only one of the above is "correct" due to the Delphi coding standards! Are you using the correct one?

Related:

Comments
September 21, 2009 at 6:11 am
(1) Levend :

I never use any of the two given styles (honestly, I do not care about “correct Delphi coding style”).

Due to the limited screen estate I tend to put “begin”, “end” and “else” statements like this:

if begin

end else begin

end

As you can see I do not lose any “unnecessary” lines.
The above examples have “unproductive” lines, i.e. all the lines having only “begin”, “else”, “end” do not do any real computing…

Levend.

September 21, 2009 at 7:09 am
(2) Diego :

I use this:

if condition
then begin
block;
end;

while condition
do begin
block;
end;

September 21, 2009 at 7:13 am
(3) Vlad :

i’m use both styles, but second style more often.

September 21, 2009 at 7:36 am
(4) Lars Fosdal :

I also use Diego’s style. Took me many years to arrive at that style, and it adds clarity and unabiguity

if condition
 then action
  else otheraction;

if (condition or condition2) //
 and condition3
then begin

end
else begin

end;

September 21, 2009 at 10:16 am
(5) Rob :

if condition then begin

end;

September 21, 2009 at 11:58 am
(6) Loïs Bégué :

*** considering the “indented begin situation” ***

Humans are strange animals !

One of their idiosyncrasy consist in the ability to easily “identify space”.

Hence, code indentation should better follow the principle: “indent to the next space”.

e.g.
IF (OneVar >= AnotherVar) then
___Begin
________DoWhatYouWant;
___End;

or
While (OneVar >= AnotherVar) do
______Begin
___________DoWhatYouWant;
___________DoEvenMore;
______End;

Darwin, though, showed that “evolution” and “mutation” are tightly bound together.
The result is hardly predictable and often looks chaotic.

Well, I presume a lot of us are “mutant” :)

September 21, 2009 at 12:01 pm
(7) Gianpiero :

I really prefer the third option (java style):

if condition begin

end else begin

end

for (…) begin

end

more compact and readable.

September 22, 2009 at 4:42 pm
(8) Dan :

Actually, I use none of the above (well, I use the first one when dealing with code auto-generated by the IDE, but only because I’m not about to change perfectly-working code for the sake of style).

I use:

if (condition) then
begin
(code)
end
else
begin
(code)
end;

I have found that doing this consistently (matching up begin/end pairs and keeping the code belonging to the pair similarly matched) has helped me to locate missing or extra ‘end;’ code.

The only down side is that I’ve also had to adopt a different way of handling “begin-less” ends, like:

case (condition)
(comparison) : begin
(code)
end;
end; // case

(where the “//case” is actually in the code as a reminder to not bother looking for a matching “begin”). I code Try/Except and Try/Finally in the same manner.

September 22, 2009 at 5:06 pm
(9) Peter Luijer :

I always indent all code that can be considered as a block, (partly) separated from the rest of the context.

So in the case of If…Then…Else it would be:

If Condition Then
Begin
// Do stuff
End
Else
Begin
// Do other things
End;

I always like to keep my code readable and dividable in separate blocks and by using indentation like this, it is.

The only Begin…End-block that doesn’t need indentation is the first “Block” in a method:

Procedure DoSomething(AValue : Boolean);
Begin
// Do your stuff
End;

I also heavilly use CamelCasing for readability, but that’s just personal preference and another topic ;-)

Greetz,

Peter.

September 22, 2009 at 5:09 pm
(10) Peter Luijer :

Hmmm, it looks like our comment can not be indented…

My code above should read like this:

..If Condition Then
….Begin
……// Do stuff
….End
..Else
….Begin
……// Do other things
….End;

(Of course: think away the dots) ;-)

September 22, 2009 at 5:42 pm
(11) Alan Lloyd :

The important elements of a conditional are “if”, “else” & the closing line (an “end” if a begin is used). These should be the obvious nodes of a chunk of code. If there is no “begin” and “end” in the conditional code then there should be a commented “end” to the conditional.

So my conditional code looks like this . . .

if (SecInfo and PoolDWdFlag) 0 then begin
if (SecInfo and ActiveDWdFlag) 0 then
ImageIndex := Pool // active pool
else
ImageIndex := -1; // no image for inactive pool
end
else
if (SecInfo and DiskDWdFlag) 0 then
ImageIndex := Disk
else
if (SecInfo and EMailDWdFlag) 0 then
ImageIndex := EMail
else
{secretary}
if (SecInfo and SecOnLeaveDWdFlag) 0 then
{secretary (shortly) on leave}
ImageIndex := SecOnLeave
else
{secretary present}
ImageIndex := Secretary;
{end; if (SecInfo and SecOnLeaveDWdFlag) 0 else}
{end; if (SecInfo and EMailDWdFlag) 0 else}
{end; if (SecInfo and DiskDWdFlag) 0 else}
{end; if (SecInfo and PoolDWdFlag) 0 else}

This I think meets aall the needs for code clarity.

Alan Lloyd

September 22, 2009 at 5:44 pm
(12) Alan Lloyd :

My example is totallly destroyed by the removal of the leading spaces – WHAT HAPPENED.

Alan Lloyd

September 22, 2009 at 6:00 pm
(13) Dan :

OK, once more, without leading spaces (note to everyone else — use leading non-whitespace characters to make your point, or it will be all for naught).

if (condition) then
..begin
..(code)
..end
else
..begin
..(code)
..end;

I have found that doing this consistently (matching up begin/end pairs and keeping the code belonging to the pair similarly matched) has helped me to locate missing or extra ‘end;’ code.

The only down side is that I’ve also had to adopt a different way of handling “begin-less” ends, like:

case (condition)
..(comparison) : begin
……………..(code)
……………..end;
..end; // case

September 22, 2009 at 11:19 pm
(14) Dagan Hoover :

I’m really anal about code formatting. I always use:

function Blah(const Blah: String): Integer;
var
Var1 : Char;
Var2 : Boolean;
begin
If Var2 Then
begin
Var1 := ‘!’;
end
Else
begin
Var1 := ‘!!’;
end;
end;

September 23, 2009 at 1:17 am
(15) Sleek :

ALWAYS…

if condition then
begin
//block
end; {if}

case condition of
ord1: begin
end; {ord1}

ord2: begin

if condition then
begin
//block
end
else
begin
end; {if-else}

while condition do
begin
//block
end; {while}

etc.

Makes the output code much more readable.

September 23, 2009 at 1:20 am
(16) Sleek :

ALWAYS…

if condition then
begin
//block
end; {if}

case condition of
ord1: begin
//block1
end; {ord1}

ord2: begin
//block2
end; {ord2}

ord3: //block3;

else begin
//block_else
end; {else}
end; {case}

if condition then
begin
//block
end
else
begin
end; {if-else}

while condition do
begin
//block
end; {while}

etc.

and ALWAYS begin..end starts in the same height.
Makes the output code much more readable to you and to others who may use it in the future…

September 23, 2009 at 3:16 am
(17) Didier :

Pretty much style described by Dan ie indent and align begin and end together to form a 'block of code'

if then
..begin
..code here
..end
else
code for else here;

On long code (over 1/2 page) I tend to comment the closing end with the original condition

Didier

September 23, 2009 at 9:33 am
(18) Sleek :

My example was also ruined by the trim of leading spaces…

September 23, 2009 at 12:09 pm
(19) Miguel :

I prefer:

if (condition) then begin
//
end else if (other condition) then begin
//
end else begin
//
end;

September 23, 2009 at 1:11 pm
(20) Pablo :

I teach Pascal and delphi from 1982 an i use indentation for clarity to read programs of the students, example:

begin
if condition then
begin
statement1..
statement2
end
end;

September 23, 2009 at 1:14 pm
(21) pablo :

Your program eats all my blanc spaces
Here again

begin
__if condition then
__begin
____statement1..
____statement2
__end
end;

September 23, 2009 at 2:40 pm
(22) Attila :

I completely agree with Pablo !
I learnt Pascal with 3.0 version. There was no OOP that time, everybody used structure programming.
One kind of program design and documentation tool was the Jackson methode.

When the program stepped into a deeper block, on the Jackson schema you have to step 1 level deeper too. It was true for selection, iteration , sequence too.
There for there is no logic to indent begin-end because it is just delimiter for the block

September 24, 2009 at 7:13 am
(23) Loïs Bégué :

@Attila:
- I also started with Pascal 3.0.
- “Logic” is subject to change.

IMO It’s a mistake to argue with “..25 years ago..”

At that time, it was NOT really possible to improve the readability of the code.
Now, it is possible !

Old: Screen width = 80 chars/line max
New: Screen width > 200 chars/line

Just consider the “visual” improvement through code indentation.

proof of concept:
Just consider the situation of visually impaired developers.

September 24, 2009 at 10:03 am
(24) Brian Gooch :

I not sure what is a “correct” coding style: for me it is the ease of reading that is important. Hence:

if some condition then begin
—blah
—blah
—end else begin
——no blah
——no blah
——end;

Indents are always 3 spaces and max line width = 80 char.

I also prefer {} for comments rather than // owing to the ability to use more than one line in a comment block.

Comments are always right aligned with the 80 char margin.

Thus code to the left; comment to the right.

Very easy to read.

September 24, 2009 at 1:16 pm
(25) Martijn Coppoolse :

I’m with Levend, Gianpiero and others (aka the ‘third way’):

__if True then begin
____// indented code
__end else begin
____// indented code
__end;

and the only time I don’t use begin/end after if..then, is when writing a set of short if statements, like so:
__if Value[0] = ‘test’ then ValueX := 6;
__if Value[1] = ‘test’ then ValueY := 1701;
__if Value[6] = ‘test’ then ValueZ := 74656;
and then I make sure it all aligns.

September 29, 2009 at 3:46 am
(26) Andre :

For god’s sake, will we ever see Pascal/Delphi if-then-else structures changed to unambiguous Modula-type structures and stop this nonsense?

November 20, 2009 at 6:43 pm
(27) Fernando :

I also use

If condition Then Begin
___do something
Else
___do something else
End;

Just because it looks compact and clear to me. I can see where the conditions are and where the code executed

Leave a Comment

Line and paragraph breaks are automatic. Some HTML allowed: <a href="" title="">, <b>, <i>, <strike>
Related Searches code indentation delphi poll

©2012 About.com. All rights reserved.

A part of The New York Times Company.