[CLOSED] IF ... ELSE vs IF lazarus / 77-Embedded ---------------------------------------------------------------------- julkas · 2019-10-22 15:59 I have some functions in the following form Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ...If funa(x) thenbegin...Result := true;endelseResult := false; Maybe it would be better rewrite them as Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ...Result := false;If funa(x) thenbegin...Result := true;end; Pros and cons? Keep in mind my target is embedded. ---------------------------------------------------------------------- howardpc · 2019-10-22 16:11 If you write it thus:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i case funa(x) of  True:     begin      ....      Result := True;    end;  False: Result := False;end;you avoid a potential double assignment to Result (in your second proposal) of first False and then True. ---------------------------------------------------------------------- Handoko · 2019-10-22 16:13 I usually set the default value for Result at the very beginning of the function. So I prefer: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function Calculate(x: SomeType): Boolean;begin  Result := False;  If x is_not_valid then Exit;   // ...  if funa(x) then  begin    // ...    If something_bad then Exit;    Result := True;  end; end; ---------------------------------------------------------------------- 440bx · 2019-10-22 16:29 Quote from: howardpc on October 22, 2019, 06:11:59 pmIf you write it thus:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i case funa(x) of  True:     begin      ....      Result := True;    end;  False: Result := False;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. ---------------------------------------------------------------------- lucamar · 2019-10-22 17:00 You could also combine the call to the function with the assignmet to Result: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ...Result := funa(x);If Result then begin DoSomething;end;... 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. ---------------------------------------------------------------------- julkas · 2019-10-23 08:05 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 · 2019-10-23 09:00 Quote from: julkas on October 23, 2019, 10:05:04 amThanks 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...