Placing begin at end of line is very unpleasent to look at, compact code tip

Started by Skybuck, August 23, 2018, 04:22:21 PM

Previous topic - Next topic

Skybuck

A lot of PascalCoin code is written like this:

if some long line of conditions, blablablablablablablablabla then begin
some more code
some more code
end;

This is very unpleasent to read because one has to make sure that the begin statement is actually there otherwise it would be a bug or a misinterpretation of the code.

To be sure that the "some more code' is actually a full statement block one has to move one's eyes to the end of the line to see if there is indeed a begin statement.

This causes a lot of unnecessary eye strain/mussle movements which makes reading the code very unpleasent. It also makes it a bit harder to follow cause one constantly have to move eye/focus back and forth across long lines of code.  It also causes at least 1 second of time wasted per "line end of begin"

This approach does have a benefit though. Vertically less white space/wasted lines, like in python, this makes the code more compact and vertically it begins easier to see what a procedure/method is doing in totality.

However if individual statement blocks are hard to follow as above, then this is a mute point.

One has to understand the statement blocks first to be able to understand the rest of the code.

So I think I will prefer the normal pascal approach:

if some long line of conditions, blablablablablablablablabla then
begin
some more code
some more code
end;

This will at least make it a bit more easy to follow statement blocks and be sure that it is valid, at the expense of slightly less compact code.

Perhaps splitting up methods into more smaller methods might make it even easier to follow what the code is doing, this technique will make the code more compact. So that is a win-win situation ;)

Skybuck

 CFTechno commented 2 days ago

I use the 'format Project Source' option in Delphi to make it more readable. Only changed the line length from 80 to 160 in the format settings.

Skybuck

I have tried this as well. Unfortunately it's not perfect, for example:

if A <> B then begin

end else begin

end;

will be reformatted as:

if A<>B then
begin

end
else
begin

end;

I would like it better if it was formatted as:
if A <> B then
begin

end else
begin

end;

Unfortunately the formatter can strangely enough not do this in same cases. For now I will assume it is a bug in the formatter, one interesting exercise is to format the original pascal compiler 1.0 source code.

Either it is written in a very strange way for example:

if a<> b then
if b <> c then
if
if
if

It could be that this original compiler is written like a bunch of nested if statements, or there is something wrong with Delphi's formatter =D

But it's not hard to see that it does have some serious and odd limitations.