Rodrigo Palhano wrote:
this

if ( payments per year <> 12 ) then begin
 Result := 1;
 exit;
end;
if ( rate = 0 ) then begin
 Result := 2;
 exit;
end;

is better than this ?

if ( payments per year <> 12 ) then begin
  Result := 1
Else if ( rate = 0 ) then
  Result := 2;

or even this ?

In my opinion, yes.

First of all, you would need to append an else clause to your example that the code following my block would need to be placed in.

e.g.

if ( payments per year <> 12 ) then begin
 Result := 1;
end else if ( rate = 0 ) then begin
 Result := 2;
end else begin
 .. lots o' code here
end; // end if - else if - else

From a code readability standpoint, if I am someone trying to understand the above code and I want to know what happens when the rate is zero, I see that a result code is set. I then need to parse down to the end of the multiple if-else if-else code to see if anything else happens after that block of code.

With the code I prefer (at the top of the email), it is clear that a result code is set, and then BAM! (as Emeril Lagasse would say), I am done. It also reduces another level of indentation, which I like.

Finally, during code maintenance, some other person working on the code might append new code at the end of the routine, but not place it inside the "end else begin" block, not thinking about what might happen with the result-code generating (and more exceptional) cases.

These are just my preferences, I'm not saying they are the best or are THE correct way of doing things. In my experience, I have found that this method works well for *me*.

Alan
_______________________________________________
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply via email to