1. Computing

Poll: Delphi Coding Styles - Begin/End Pairs - Begin On a Separate Line or After Then?

By September 15, 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.

Some use:

if condition then
begin
end;

some:

if condition then begin
end;

What is your begin/end coding style?

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

Related:

Comments
September 15, 2009 at 5:15 am
(1) TK says:

The order of the examples and the poll entries are in the opposite order and i do not belive in accidents.

September 15, 2009 at 5:46 am
(2) Marcin says:

Years ago I saw something like this:

if
then begin
… code here
end

and I found it very readable for me and use this style everywhere (for, while loops etc).

September 15, 2009 at 5:53 am
(3) zarko gajic says:

@TK: :) An accident. Fixed!

@Marcin: :( uh. Not for me.

September 15, 2009 at 6:06 am
(4) caniremember says:

I always indent the begin/end pairs also

if … then
begin
end;

In an attempt to make it easier to track the pairs when I loose an end during a copy/paste or some other such refactoring.

Occasionally I will do something like this

if statement1 and
statement2 and
statement3 then
begin
end;

But such things are the least of my bad programming habits!

(Everyone knows a single character variable name make the code go faster, dont they?)

September 15, 2009 at 6:46 am
(5) Zarko Gajic says:

@ caniremember : Well, the begin/end indentation is the question for the next poll :)

Yes, all my variables are single character. I only have problems when there are more than 30 of them per method :(

;)

September 15, 2009 at 7:46 am
(6) caniremember says:

Besides, I always enjoy the look of confused frustration on colleagues faces when they are trying to do something to my code.

It has to be said that I run out of single letters long before that, c is always a counter, d is a counter nested in c, e is a counter nested in d etc. I’m logical about it, if not very sensible :-)

I’m starting to change though. More recently (because I am having to teach others how to use and modify my code) I have started to be much more sensible about sTempString instead of just T or S.

Must be getting old!

September 15, 2009 at 8:38 am
(7) Jeroen says:

@Marcin

I use that style also. I know it isn’t standard but it’s easy readable.

September 15, 2009 at 10:00 am
(8) delphigeist says:

I use the “then begin” statement, it’s much easier to read and by the way Delphi is NOT an interpreter, so it’ doesn’t matter if you type even:
if
“expression”
then
begin

September 15, 2009 at 10:34 am
(9) stanleyxu2005 says:

For books, it might make sence to put “then begin” in a same line. You can show more code on a same page. But I think it does reduce the readability of code.

Generally speaking, I prefer to the Delphi Code Style of CodeGear. But one thing I do not follow is the simple if-then-else statement.

I would rather write it like:
{{{!#pascal
if condition then
val := get_value1
else val := get_value2;
}}}
in stead of
{{{!#pascal
if condition then
val := get_value1
else
val := get_value2;
}}}

September 15, 2009 at 5:35 pm
(10) Oliver Giesen says:

begin on a separate line and the whole begin/end-block indented

September 15, 2009 at 6:35 pm
(11) Michael says:

I tend to use:
if condition
then
begin
end

That is probably because when I learned programming many years ago, the Pascal compiler would list the code in that fashion and I got used to it.

When it comes to simple counters or variables, I use i, j, k for loop counters (a relic of Fortran days), c for char and s for string.

September 15, 2009 at 9:23 pm
(12) Mick Arundell says:

In this instance I believe that the coding standards are inaccurate, and here’s the reason
My screen is much wider than it is high,
I plan to write functions and procedures so they fit into one screen to reduce bugs and other errors
If I put begin on a line by itself I loose one line height but if I put begin on the same line I loose 6 characters of width – but it is the end of the line anyway so I loose no real screen.

I wll continue to use begin on the same line and onscreen colour coding for enhanced coding

Mick

September 15, 2009 at 11:24 pm
(13) firefly says:

my coding style:

if Condition1
and Condition2
and Condition3 then begin
// do sth.
end else begin
// otherwise
end;

September 15, 2009 at 11:26 pm
(14) firefly says:

space is removed automatically, it should be:

if Condition1
____and Condition2
____and Condition3 then begin
__// do sth.
end else begin
__// otherwise
end;

September 16, 2009 at 12:25 am
(15) Andrea Raimondi says:

My coding style is a bit complex.

if ShortCond1 and ShortCond2 then
begin
// Multiple lines here
end;

if ShortCond1 and ShortCond2 then
// Single line here, no begin/end

if ShortCond1 and ShortCond2 then
begin
// Multiple lines here
end
else begin
// Multiple lines here
end;

if LongConditionOneOnTheFirstLine and
LongConditionTwoOnTheSecond then
begin
end;

I tend to use one-liners after “then” rarely,
since I use the begin/end block to “protect”
when I am not sure if I will add other code.

Bye,

Andrew

September 16, 2009 at 2:07 am
(16) Ron Tuijnman says:

I use Castalia merely for the feature of marking those blocks (even the nested ones!) automaticly with thin coloured lines. Really love that! Therefor I keep those blocks on separate lines…

September 16, 2009 at 3:02 am
(17) Keith says:

I use very spaced-out code – 8 character tabs (so that after a few indents I am forced to create a procedure or function instead of losing the code off the right of the screen), blank lines here and there and, most importantly, begins on a new line so that blocks are distinct.

For me, this makes debugging and reading easier and, as I always stick to my own style, I can also recognise code by the shape as I am scrolling quickly looking for it.

This was the style I was taught when I learned Pascal many tears ago.

September 16, 2009 at 3:33 am
(18) Loïs Bégué says:

The “old fashioned standard” is ok for specific purposes.
Though I use to format my code like:

Procedure IsTestOk(out anErrorcode: integer): Boolean;
Var
_____TestLoop: Integer;
Begin
_____Try
________If (Condition1 and Condition2) then Begin
___________DoFurtherTest;
___________For TestLoop:=0 to 9 do Begin
_______________writeln(IntToStr(TestLoop), now);
_______________LogThis(‘Test Loop: ‘ + IntToStr(TestLoop));
_______________End;
__________End;

To me, the “end” belongs to the “subcode”, thus the extra indent.

Hence, it’s a visual improvement: it’s EASY TO IDENTIFY THE CODE BLOCKS.

I REALLY DON’T CARE about the length of the code lines – I’m using 2 reasonably modern TFT-screens so I prefer NOT HAVING TO SCROLL DOWN for more code…

my very subjectiv 2 cents :)

September 16, 2009 at 7:18 am
(19) Keith says:

I supose it’s each to his, or her, but for me a code block with the start (i.e. begin) stuffed at the end of the previous line isn’t obvious.

When I get to the point of deciding to cut code out to form a procedure, it’s handy to be able to highlight and cut entire lines only.

Furthermore, if you have a lot of conditions and you miss an “end” out it’s far easier to look down the indent columns for “begin”s and match them to “end”s – for me, at least.

It all reminds me of the discussions about whether the toilet paper should hang down from the front or the back, or whether the toilet seat should stay up or down in between use. My view on both is, who cares?

Do your own thing if only you are going to use the code.

September 16, 2009 at 7:27 am
(20) Keith says:

Oh, and the point about lines lengths was not the width of the screen, as such. It was about the good programming practice of not having tortuous logic routes and hugely indented code. These are a hindrance to debugging and code maintenance. I have trouble with my own programs after a few years, never mind other people’s.

Of course, thirty lines of comment per line of code might explain all…

September 21, 2009 at 5:38 am
(21) Tolga says:

My coding is like that;

if condition then begin
end else begin
end;

If first section takes more than one line and else section exists, i make a empty line like that;

if a > b then begin
ListBox1.Items.Add( “Message” );
ListBox2.Items.Add( “Message” );

end else begin
// code …
end;

September 27, 2009 at 12:01 pm
(22) JorgeN says:

I find heavier indentation helpful and clearer

if XXX
then
begin
aaa
bbb
end
else
begin
ccc
ddd
end

Leave a Comment

Line and paragraph breaks are automatic. Some HTML allowed: <a href="" title="">, <b>, <i>, <strike>

©2014 About.com. All rights reserved.