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:


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.
I use this:
if condition
then begin
block;
end;
while condition
do begin
block;
end;
i’m use both styles, but second style more often.
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;
if condition then begin
…
end;
*** 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”
I really prefer the third option (java style):
if condition begin
…
end else begin
…
end
for (…) begin
…
end
more compact and readable.
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.
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.
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)
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
My example is totallly destroyed by the removal of the leading spaces – WHAT HAPPENED.
Alan Lloyd
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
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;
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.
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…
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
My example was also ruined by the trim of leading spaces…
I prefer:
if (condition) then begin
//
end else if (other condition) then begin
//
end else begin
//
end;
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;
Your program eats all my blanc spaces
Here again
begin
__if condition then
__begin
____statement1..
____statement2
__end
end;
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
@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.
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.
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.
For god’s sake, will we ever see Pascal/Delphi if-then-else structures changed to unambiguous Modula-type structures and stop this nonsense?
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