Oxygene Coding Style

possibly, yes, I forgot, does Delphi encourage begin on the new line (like an animal) or hanging?

This Developer’s Guide Coding Standards Document goes way back to Delphi 4 (1998) :nerd_face:

Begin…End Pair
The begin statement appears on its own line. For example, the following first line is incorrect; the second line is correct:

for I := 0 to 10 do begin // Incorrect, begin on same line as for

for I := 0 to 10 do       // Correct, begin appears on a separate line
begin

An exception to this rule can be made when the begin statement appears as part of an else clause - for example,

if some statement = ... then
begin
 ...
end
else begin
   SomeOtherStatement;
end;

but the preferred way of writing this is

if some statement = ... then
begin
 ...
end
else begin
   SomeOtherStatement;
end;

so that the begin statement always appears indented on the same level as the corresponding if statement.

The end statement always appears on its own line.

When the begin statement is not part of an else clause, the corresponding end statement is always indented to match its begin part.

:exploding_head: of all things, why make an exception for “else”.

but in any case, that coding style is crazy, the begin belongs at the end of the line…

1 Like

Not really…

In Pascal, begin ... end is a compound statement, and putting begin at the end of line is actually butchering that statement. The coding style that I learned decades ago says that the “right” indentation for the if, for, etc. statements is

if cond then
    statement

Why? Because statement appears in the scope of if and should be thus indented.

Building on this interpretation and replacing statement with a compound statement, we get

if cond then
   begin
     statement1;
     statement2
   end

I know, this requires an extra line and an extra indent, but IMO makes a lot of sense. I am using it - and won’t change despite recommendations given above :grinning:.

1 Like

Why? IMO, end should line up with begin. The same as lining up ‘{’ and ‘}’ in C, that was mentioned somewhere else in this thread.

I didn’t want to start any dispute. I just wanted to show a reasonable alternative to your strong belief that begin should come at end of the line.

You’re free to disagree and do it that way, but I, personally, find that unreadable and misleading, as shown in the example given. :wink:

Oxygene will support either formatting, of course

1 Like

That looks highly readable to me :nerd_face:

it makes MORE sense than having the begin just under the if as if it was an unrelated new statement, yes.

that’s the nice thing about Oxygene — whitespace does not matter to the compiler at all, unlike in some other languages, so everyone is free to indent as they feel works best for them or their team… :slight_smile:

I like indenting the begin end for property getters, setters and when your passing a callback with a method invocation.

Hey Marc, just going through the documentation about this topic:

while/do Loops and begin/end blocks.

On its own, the while/do loop only takes a single statement to be executed for each iteration. To execute more than one statement, multiple statements can be grouped using a begin/end Block Statement:

while x > 10 do begin
  DoSomething();
  DoSomethingElse();
end;

Marc, would you write it more like this (below) rather than what is shown in the documentation above? Or is the while/do loop a different scenario? :

while x > 10 do
    begin
        DoSomething();
        DoSomethingElse();
    end;

Also cross-referencing Delphi’s style guide just to see what our cousin(s) are up to.

Free Pascal coding style appears to be in agreement with you :nerd_face:

Never place a begin on the same line as while…do/if…then/…, but always on its own line (and indent it compared to the while/if/…).

{ correct }
if true then
  begin
  end;

{ incorrect }
if true then begin
  end;

{ incorrect }
if true then begin {...} end;

I would use the former. syntax wise, a begin/end block is just a statement like any other, so your and Marko’s variation is technically correct.

But style wise and *conceptually, i consider the begin/end to become part of the larger structure (be it a for, if while or the like, so I think of the endand closing off thewhile, not the begin`.

But yeah, this is all highly subjective, of course, and a matter of taste, of course. Our style guidelines are by no means meant to be compulsory (but we try to keep to them internally, for consistency).

1 Like