PrestwoodBoards.com
-Collapse +Expand

Delphi

Search Delphi Group:

Advanced
-Collapse +Expand Delphi Group Home
-Collapse +Expand Message Board
-Collapse +Expand Knowledge Base
-Collapse +Expand Delphi To/From
To/FromCODEGuides
-Collapse +Expand Delphi Study Test
PRESTWOODCERTIFIED
-Collapse +Expand Delphi Store
PRESTWOODSTORE
-Collapse +Expand Members Only
Prestwood Tip Jar
Tip Jar
Finding something useful?

Add to the
Tip Jar!

Prestwood eMagazine

Subscribe now!
Enter your email:


   PrestwoodBoardsKBProgrammingDelphi for Win32Language Basics   
Go To Random Article
Part of the mo. lesson in the Jul 10 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
begin
..
end;

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


Tip Contributed By Daniel Kram:
rdkram
Email Approved! E Unk

Comments

Oldest To Newest

Add Comment
Reader...
mtiede
Rank: Cadet 3rd Year

Joined: May 2009
Location: Grand Island, NY USA
mtiede -Collapse +Expand
Email Approved! E NY USA

Member Points: 36
Visits: 19
MB Posts: 1

KB Articles: 0
Comment 1 of 2
Sunday, January 17, 2010

Uh oh.  The Style Wars begin...

I would code this like:

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

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
   ...
   end;

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

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.


Reader...
mtiede
Rank: Cadet 3rd Year

Joined: May 2009
Location: Grand Island, NY USA
mtiede -Collapse +Expand
Email Approved! E NY USA

Member Points: 36
Visits: 19
MB Posts: 1

KB Articles: 0
Comment 2 of 2
Friday, July 09, 2010

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.

Would you like to comment? Reply? Ask a question? Say thanks?
+Add Comment

 KB Article #101685 Counter
867
Since 12/12/2008

Sponsored Ad
Tip Contributed By Daniel Kram:
rdkram
Email Approved! E Unk
 
791 People Online Now!!  
Online Now: Sign In to see who's online now!  Not a member? Join Prestwood now. It's free!