Forum > Embedded

[CLOSED] IF ... ELSE vs IF

(1/2) > >>

julkas:
I have some functions in the following form

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---...If funa(x) thenbegin...Result := true;endelseResult := false; Maybe it would be better rewrite them as

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---...Result := false;If funa(x) thenbegin...Result := true;end; Pros and cons?
Keep in mind my target is embedded.

howardpc:
If you write it thus:
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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:
I usually set the default value for Result at the very beginning of the function. So I prefer:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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:

--- Quote from: howardpc on October 22, 2019, 06:11:59 pm ---If you write it thus:
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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.

--- End quote ---
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:
You could also combine the call to the function with the assignmet to Result:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---...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.

Navigation

[0] Message Index

[#] Next page

Go to full version