Recent

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

julkas

  • Sr. Member
  • ****
  • Posts: 412
  • KISS principle / Lazarus 2.0.0 / FPC 3.0.4
[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 »
procedure mulu64(a, b: QWORD; out clo, chi: QWORD); assembler;
asm
  mov rax, a
  mov rdx, b
  mul rdx
  mov [clo], rax
  mov [chi], rdx
end;

howardpc

  • Hero Member
  • *****
  • Posts: 3178
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: 3188
  • 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: 1199
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.


using FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

lucamar

  • Hero Member
  • *****
  • Posts: 2081
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 2.0.2/2.0.4  - FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

julkas

  • Sr. Member
  • ****
  • Posts: 412
  • KISS principle / Lazarus 2.0.0 / FPC 3.0.4
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.
procedure mulu64(a, b: QWORD; out clo, chi: QWORD); assembler;
asm
  mov rax, a
  mov rdx, b
  mul rdx
  mov [clo], rax
  mov [chi], rdx
end;

Thaddy

  • Hero Member
  • *****
  • Posts: 9183
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 »
also related to equus asinus.