function DoSomething: boolean;
begin
Result := False;
if FDone then begin
Result := True;
end;
end;
The 'begin' is at the end of the line rather than on a line of its own. I believe the original reason for this was to reduce the number of lines of code, this helped save paper when printing out code at schools and universities.
This format I personally cannot stand and cannot see a modern valid argument for doing this. I have not printed out code for years and think the argument that it saves some paper to be a very weak one compared to the improved readability of the 'begin' being in-line with the end.
This is how I would write the function:
function DoSomething: boolean;
begin
Result := False;
if FDone then
begin
Result := True;
end;
end;
As you can see the 'begin' and 'end' are in-line and it is more readable. But why is it more readable? I have been trying to understand why I think it is more readable and here are some reasons:
- The begin/end is in-line so the eye can naturally move vertically down to see the end of the code block.
- It matches the function and procedure begin/end pattern, therefore is more consistent. The begin/end of a function or procedure is usually in-line, although I have seen code where this is not the case.
- The 'begin' is visually linked to the 'end', consider the Gestalt principle of proximity and continuity (see diagrams below).
The diagram above shows squares to represent the 'begin' and 'ends', the first group uses the dangling begins and the second puts the 'begin' on a new line making the 'begin' and 'ends' in-line. It seems easier to link the second group because they are in-line.
I have seen recently that Apple's Swift language uses the 'Egyptian' style, for example:
func tableView(tableView: UITableView) -> Int {
return 10
}
Using this format in Delphi the function would look like this:
function DoSomething: boolean; begin
Result := false;
if FDone then begin
Result := true;
end;
end;
This would make the begin/ends more consistent, however I like this even less.
It is a matter of personal preference, and I prefer the in-line begin/ends and in-line curly brackets in C#, I cannot see how I will ever change my opinion on this.