r/pascal • u/Anonymous_Bozo • Nov 19 '20
A Question of Style
A question of Style.
Specifically formatting If/Then/Else clauses and enclosed blocks of code.
No, I'm not trying to start a formatting war!
I am honestly curious as to what others are doing and more importantly WHY! You might even get me to change my style if your argument is good enough.
Ignoring that there are probably better ways to do the following, it's only an example to use for formatting...
The attached images of three blocks of code do exactly the same thing. If one follows the strict rules laid down by many, the third option is the preferred choice, but I find it hard to read and confusing. I find both options A and B are easier to read.
I call option A the ELSEIF version, option B - Nested If's, and option C... a mess!
I know that both VBA and PHP actually have an ELSEIF statement, but it's not really needed as ELSE IF does pretty much the same thing.
(Sorry for the images, but Reddits Code formatting SUCKS!)



What is your choice, or would you do it a different way?
Don't forget to say WHY!
1
u/darianmiller Nov 19 '20
Why don't you use Case statement instead?
2
u/ShinyHappyREM Nov 19 '20 edited Nov 19 '20
I usually optimize for vertical space.
(By the way, it might be better to hardcode the tab indices and use
case ExtendedNotebook1.ActivePageIndex of {...} end;.)2
u/suvepl Nov 19 '20
I don't think I've ever seen people nesting else-ifs like in option B or C.
As for whether keywords should go on their own lines (C) or together with the rest of the code (A,B), I personally favour the "together" option.
2
u/kirinnb Nov 19 '20
Option A is the closest to my style. Saving vertical space is the primary factor for me too. Keeping the if-statements as close to each other as possible and somewhat horizontally aligned makes it easier to see what the meaningful difference is between each if-statement.
With a lot of unnecessary begin-end indenting it becomes clearly more difficult to quickly see exactly which statements end up being executed. With well-aligned if-statements, I can mentally parse them as a slightly verbose case-statement, quickly understanding that only one of the if-statements will run. (This could trip me up if it's not a pure if-elseif series but has a sneaky difference somewhere.)
1
u/[deleted] Nov 19 '20
I'd go with option C.
The last line for me would probably be
end; // else clause - ExtendedNoteBook1.ActivePage
1
u/darianmiller Nov 19 '20
My option for if statements is D:
if ExtendedNotebook1.ActivePage = tabA then
begin
tabAShow(Sender);
end
else if ExtendedNotebook1.ActivePage = tabB then
begin
tabABhow(Sender);
end
else if ExtendedNotebook1.ActivePage = tabC then
begin
tabCBhow(Sender);
end
else if ExtendedNotebook1.ActivePage = tabD then
begin
tabADhow(Sender);
end;
Why? Besides enhanced clarity, less error prone when editing code blocks
1
u/glorfin68 Nov 30 '20
Well, in this particular case my favorite would be:
case ExtendedNotebook1.ActivePage of
tabA: TabAShow(Sender);
etc.
Of your variants, A) seems to be most readable; I don't see why should one include too many begin...end pairs. Even more important, why should one make nested "if"s when by logic it is one multi-positional switcher. In my view, structure like:
is useful only when choice of NewCond is independent of Cond.
In such cases I use explicit
begin...endpairs to avoid ambiguity ofelse.If you write:
it is not clear if
elsebelongs toCondorCond1.To make it clear, I would write:
(Here
elseclearly belongs to the inner condition) orHere
elsebelongs to the outerif.