Hi I'm trying to tab some code, please could someone check to make sure I have done this correctly (Changed some code for simplicity):
begin
if Password <> Database['Database']
then showmessage ('Message')
else
if NewPassword <> Retype
then showmessage ('Message')
else
begin
if Message (yes, No, etc) =yes
then
begin
List
List
List.post;
showmessage ('Message')
end
else close;
end;
end;
As noted in the other answers, style is highly subjective, and for that reason it is difficult to give an objective answer. However, something that can be said is how your code should look according to Embarcadero's Object Pascal Style Guide, and that is what I will attempt to answer (with some more detail than you asked for). When the same issue occurs in multiple locations, I will only mention it once.
Here, the casing of
showmessage
should beShowMessage
, which is also the name given to the procedure in theDialogs
unit. There should not be a space betweenShowMessage
and(
.Your use of
then
on the next line is unusual but okay, and you're right to indent it in that case.This second
if
statement is part of theelse
branch of the firstif
statement, and should be indented accordingly, possibly by placing it on the same line as theelse
.begin
andend
should not have extra indentation: the substatements should be two spaces to the right of theelse
above.The fact that you have a space before the
=
, but not one after it, is not something I've seen before, but this is not a situation where the style guide expresses any preference, so that is okay.This deserves an extra mention:
else
followed by a statement is explicitly called out as having fallen out of favour, but nonetheless okay, in the style guide.Something else worth mentioning is that your use of two spaces for indentation is exactly what the style guide says to use.
Personally, I'm an
else if
guy and also I'm against hangingthen
:But in your case I'd better avoid large compound statements and heavy nesting in
else
branch:Sometimes the rule about writing normal flow code to
then
branch and abnormal toelse
branch becomes a false friend and inviting writing a lot of nested statements toelse
which in turn only hurts readability instead of improving it.This is a coding style question, and may not survive here very long. :-) (Coding style is very much a matter of personal opinion, and there are an awful lot of different ones out there.) I'll give it a shot, anyway. :-)
I'd do it slightly differently. IMO, this clearly shows the proper pairings of
if..else
andbegin..end
to a new programmer:In my own code, I'd do it a little differently still (but only a minor difference). I'd move the
else
if password
to the same line (it reduces one level of indent, and to me makes the flow of the code more clear. We have three possible options, and there are three options clearly shown (if this
,else if this
,else this
):There are only a couple of other code areas where formatting sometimes makes a difference. I'll try to quickly touch as many of them as I can think of off-hand.
Case statements:
While statements:
Repeat statements:
For loops:
For..in loops:
Try..finally:
Try..except:
Try..finally with try..except:
Here's how I would format that code:
The main differences are:
then
at the end of the same line thatif
is on (unless there are multiple conditionals).else
statements in line withif
statements