Recent

Author Topic: [CLOSED] IF ... ELSE vs IF  (Read 4145 times)

julkas

  • Guest
[CLOSED] IF ... ELSE vs IF
« on: October 22, 2019, 05:59:34 pm »
I have some functions in the following form
Code: Pascal  [Select][+][-]
  1. ...
  2. If funa(x) then
  3. begin
  4. ...
  5. Result := true;
  6. end
  7. else
  8. Result := false;
  9.  
Maybe it would be better rewrite them as
Code: Pascal  [Select][+][-]
  1. ...
  2. Result := false;
  3. If funa(x) then
  4. begin
  5. ...
  6. Result := true;
  7. end;
  8.  
Pros and cons?
Keep in mind my target is embedded.
« Last Edit: October 23, 2019, 05:45:51 pm by julkas »

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: IF ... ELSE vs IF
« Reply #1 on: October 22, 2019, 06:11:59 pm »
If you write it thus:
Code: Pascal  [Select][+][-]
  1. case funa(x) of
  2.   True:
  3.     begin
  4.       ....
  5.       Result := True;
  6.     end;
  7.   False: Result := False;
  8. end;
you avoid a potential double assignment to Result (in your second proposal) of first False and then True.

Handoko

  • Hero Member
  • *****
  • Posts: 5130
  • My goal: build my own game engine using Lazarus
Re: IF ... ELSE vs IF
« Reply #2 on: October 22, 2019, 06:13:20 pm »
I usually set the default value for Result at the very beginning of the function. So I prefer:

Code: Pascal  [Select][+][-]
  1. function Calculate(x: SomeType): Boolean;
  2. begin
  3.   Result := False;
  4.   If x is_not_valid then Exit;
  5.  
  6.   // ...
  7.   if funa(x) then
  8.   begin
  9.     // ...
  10.     If something_bad then Exit;
  11.     Result := True;
  12.   end;
  13.  
  14. end;
« Last Edit: October 22, 2019, 06:20:38 pm by Handoko »

440bx

  • Hero Member
  • *****
  • Posts: 3944
Re: IF ... ELSE vs IF
« Reply #3 on: October 22, 2019, 06:29:21 pm »
If you write it thus:
Code: Pascal  [Select][+][-]
  1. case funa(x) of
  2.   True:
  3.     begin
  4.       ....
  5.       Result := True;
  6.     end;
  7.   False: Result := False;
  8. end;
you avoid a potential double assignment to Result (in your second proposal) of first False and then True.
That is true but, in more complex logic the assignment to "result" depends on guaranteeing that one of the paths will be taken, an undesirable characteristic in the code.

Initializing/presetting the value of "result" guarantees that the value of "result" will not just be some random value in some potentially rare cases.  Such conditions/bugs are often very difficult to pin down.

The OP's second option is solid, the first is inherently prone to cause problems as new conditions are added.


(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: IF ... ELSE vs IF
« Reply #4 on: October 22, 2019, 07:00:19 pm »
You could also combine the call to the function with the assignmet to Result:

Code: Pascal  [Select][+][-]
  1. ...
  2. Result := funa(x);
  3. If Result then begin
  4.  DoSomething;
  5. end;
  6. ...

That makes it a direct "variable" comparison rather than a slightly less direct comparison with a function result and ensures: 1) that Result is always set and 2) it's set just once.
« Last Edit: October 22, 2019, 07:02:33 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

julkas

  • Guest
Re: IF ... ELSE vs IF
« Reply #5 on: October 23, 2019, 10:05:04 am »
Thanks all for replies.
After reading your posts and articles about embedded programming best practices, I think that IF without ELSE would be better approach.
I will check ASM generated code for AVR.

Thaddy

  • Hero Member
  • *****
  • Posts: 14201
  • Probably until I exterminate Putin.
Re: IF ... ELSE vs IF
« Reply #6 on: October 23, 2019, 11:00:51 am »
Thanks all for replies.
After reading your posts and articles about embedded programming best practices, I think that IF without ELSE would be better approach.
That is simply not true. A jump can save you. Indeed, check the assembler.
If and if.. else have completely different meaning. Just in case you did not understand it... O:-)
« Last Edit: October 23, 2019, 11:07:25 am by Thaddy »
Specialize a type, not a var.

 

TinyPortal © 2005-2018