Your full service technology partner! 
-Collapse +Expand
Search Delphi Group:

-Collapse +Expand Delphi To/From
-Collapse +Expand Delphi Store

Prestwood eMagazine

June Edition
Subscribe now! It's Free!
Enter your email:

   ► KBProgrammingDelphi for W...Language Basics   Print This     
Part of the mo. lesson in the Mar 16 Issue of Prestwood eMag
Delphi Tip:
 A flashcard from our Delphi Flashcards Library
 A tip from our Delphi Tips Page
One Recommended way of writing IF/End IF statements

Format the IF/Endif for easy reading. I have found this to be easy to read and follow:

if (         (something = somethingelse)
      and (x = y)
      and (z = a)
   ) then

To indent the structure and line up the parenthesis makes it, I feel, much easier to read.


Share a thought or comment...
Comment 1 of 3

Uh oh.  The Style Wars begin...

I would code this like:

if ( ( something = somethingelse ) and
     ( x = y                     ) and
     ( z = a                     )  ) then begin

I started using this style probably in the 1970s.  I was introduced to it by IBM's SPF.  SPF at one point in time stood for Structured Programming Facility (later to become System Productivity Facility).  I coded PL/I at that time which was very similar to Pascal (which is why I started using Turob Pascal 1.0 on the PC).

The structured programming editor could actually USE the indentation scheme.  You could exclude all lines from showing.  This resulted in a single line of dashes.  Then you could do a SHOW 10 for instance and the editor would reveal the first 10 lines at the same indent level. 

Consequently, if you had something like this

if ( ( something1 = somethingelse1 ) and
     ( x = y                       ) and
     ( z = a                       )  ) then begin

if ( ( something2 = somethingelse2 ) and
     ( x2 = y2                     ) and
     ( z2 = a2                     )  ) then begin

Before the show would look like this

x  -----------------------------------------------

After the show would look like this

x  -----------------------------------------------
   if ( ( something1 = somethingelse1 ) and
x  -----------------------------------------------
   if ( ( something2 = somethingelse2 ) and
x  -----------------------------------------------

Admittedly in this case, you could not see the rest of the and condition.  But it did give you a good overview of what the code logic was doing.  You could clearly see there were 2 IF conditions.  Then if you wanted to see all the lines in the first if condition, you could have it expand that.  Another thing you could do was do searches on either the excluded lines or the visible lines.  And you could say delete all excluded or non-excluded lines.  You could also copy any set of excluded lines, etc., etc.

But the main thing that happens is your eye starts to scan down the code looking at the important features of the logic.  I would argue that Begin and End are not important to the logic.  IF is important.  Then your eye need only look into the indented section if is interested in that section.  The BEGIN and END are really only significant for the compiler, not for logic.  In fact, a friend of my built a compiled language that was indentation sensitive and needed NO begin..end, or {..} or any other delimiters.  And it was easy to read the language because things at the same "sub" indent level were executed together.

So with my indentation scheme, my eye would scan vertically and would see only IF and another IF, rather than having to vertically parse lines the IF-BEGIN-END then IF-BEGIN-END.  My eye parses it as if one were holding a piece of paper over the code and then revealing its structure by sliding the paper slowly to the right to see what "sticks out".

And again, if I were interested in a particular IF block's content, then my eyes would shift to the next indent block under the IF.

Lastly, I always indent by 3 characters.  This allows the most common blocking statement, the IF, to have its contained material lined up with the first character after the IF keyword.  This bit of white space helps to see the indentation pattern.

Posted 11 years ago

Comment 2 of 3

Oh, on re-reading my post today, I noticed the the outer parentheses in the if statement given aren't necessary and, I would argue, should be omitted since they serve no purpose but to add superfluous syntax.

Posted 11 years ago

Comment 3 of 3

whatever takes you fancy regarding IF Else etc , I always finish the end with a comment


if bleu = grey then

code dent indent etc then else more if 

end ; // bleu = grey

Eric B
Posted 11 years ago
Write a Comment...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.

Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P1186A1
Enter key:
Tip Contributed By Daniel Kram:
Visit Profile

 KB Article #101685 Counter
Since 12/12/2008
Sales Website: Or visit our legacy sales site:

©1995-2021 Prestwood IT Solutions.   [Security & Privacy]