Recent

Author Topic: function and result  (Read 12811 times)

Thaddy

  • Hero Member
  • *****
  • Posts: 16945
  • Ceterum censeo Trump esse delendam
Re: function and result
« Reply #15 on: October 30, 2018, 04:10:24 pm »
Code: Pascal  [Select][+][-]
  1.  
  2.   result :=true;
  3.   try
  4.      result := true; // again, just for fun
  5.   finally
  6.     result :=false;
  7.   end;

Not such a good idea?
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

440bx

  • Hero Member
  • *****
  • Posts: 5302
Re: function and result
« Reply #16 on: October 30, 2018, 05:07:56 pm »
Anything other than setting the function result upfront is a bad idea.

The reason is obvious: until the function ends successfully, it hasn't ended successfully (as Yogi Berra would point out too) and its result should reflect that. The result should change _only_ when it ends successfully to indicate it did.

Anything else is simply an exercise in turning the trivially simple into gratuitous complexity.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v4.0rc3) on Windows 7 SP1 64bit.

creaothceann

  • Full Member
  • ***
  • Posts: 117
Re: function and result
« Reply #17 on: October 30, 2018, 05:42:01 pm »
There may be a race condition between FileExists and LoadFromFile, so...

Code: [Select]
function TEvarilBuffClass.is_PNSN_already_inserted(const FileName, PNSN : string) : boolean;
var
        i    : integer;
        List : TStringList;
begin
List := TStringList.Create;
try
        try
                List.LoadFromFile(OutputFile);
                for i := 0 to (List.Count - 1) do  if (AnsiPos(PNSN, List.Strings[i]) > 0) then begin
                        WriteToMemoParent(PNSN + ' already inserted');
                        exit(True);
                end;
                exit(False);
        except
                exit(False);
        end;
finally
        List.Free;
end;
end;

jamie

  • Hero Member
  • *****
  • Posts: 6892
Re: function and result
« Reply #18 on: October 30, 2018, 05:50:45 pm »
Nice leaky code there  :)
The only true wisdom is knowing you know nothing

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: function and result
« Reply #19 on: October 30, 2018, 06:29:23 pm »
I would do it like this:
Code: Pascal  [Select][+][-]
  1. function TEvarilBuffClass.checkPNSNalreadyInserted(dirPathOutputFile: string; PNSN: string): Boolean;
  2. var
  3.   ts : TStringList;
  4. begin
  5.   Result := false;
  6.   if (FileExists(dirPathOutputFile)) then begin
  7.     ts := TStringList.Create;
  8.     try
  9.       try
  10.         ts.LoadFromFile(dirPathOutputFile);
  11.         Result := (AnsiPos(PNSN, ts.Text) > 0);
  12.         if Result then
  13.           writeToMemoParent(PNSN+' already inserted... ');
  14.       except
  15.         on e:Exception do
  16.           writeToMemoParent('checkPNSNalreadyInserted Error...'+e.Message);
  17.       end;
  18.     finally
  19.       ts.Free;
  20.     end;
  21.   end;
  22. end;  
  23.  

but this starts looking like an out-of-topic, bragging exercise :)
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.

Thaddy

  • Hero Member
  • *****
  • Posts: 16945
  • Ceterum censeo Trump esse delendam
Re: function and result
« Reply #20 on: October 30, 2018, 06:35:16 pm »
Anything other than setting the function result upfront is a bad idea.
Setting a function result in a finally section is *a lot wierder*.
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

wp

  • Hero Member
  • *****
  • Posts: 12800
Re: function and result
« Reply #21 on: October 30, 2018, 06:49:11 pm »
 :-[

440bx

  • Hero Member
  • *****
  • Posts: 5302
Re: function and result
« Reply #22 on: October 30, 2018, 07:49:23 pm »
Code: Pascal  [Select][+][-]
  1.  try
  2.    try
  3.       ts.LoadFromFile(dirPathOutputFile);
  4.  
  5.       Result := (AnsiPos(PNSN, ts.Text) > 0);
  6.  
  7.       if Result then
  8.         writeToMemoParent(PNSN+' already inserted... ');
  9.     except
  10.       on e:Exception do
  11.         writeToMemoParent('checkPNSNalreadyInserted error...'+e.Message);
  12.     end;
  13.   finally
  14.     ts.Free;
  15.   end;
  16.  

That code contradicts itself.

When Result gets set to true in line 5, writeToMemoParent gets executed and if that causes an exception, the message "checkPNSNalreadyInserted error..." is output.  IOW, an error may be reported yet the function still returns true.

At the very least, in that function, it is unclear what a result of true actually means.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v4.0rc3) on Windows 7 SP1 64bit.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: function and result
« Reply #23 on: October 30, 2018, 08:16:26 pm »
That code contradicts itself.

When Result gets set to true in line 5, writeToMemoParent gets executed and if that causes an exception, the message "checkPNSNalreadyInserted error..." is output.

One could always locate that if at the end of the function. Nevertheless, if writeToMemoParent() raises an exception it will (most probably) do so both here *and* in the except block, and this second exception will not  be catched, will it?

Quote
At the very least, in that function, it is unclear what a result of true actually means.

Huh?! It means Result := (AnsiPos(PNSN, ts.Text) > 0); Exactly the same as in the original code, i.e. that the file contains PNSN.

Maybe this is clearer?
Code: Pascal  [Select][+][-]
  1. function TEvarilBuffClass.checkPNSNalreadyInserted(dirPathOutputFile: string; PNSN: string): Boolean;
  2. var
  3.   ts : TStringList;
  4. begin
  5.   Result := false;
  6.   if (FileExists(dirPathOutputFile)) then begin
  7.     ts := TStringList.Create;
  8.     try
  9.       try
  10.         ts.LoadFromFile(dirPathOutputFile);
  11.         Result := (AnsiPos(PNSN, ts.Text) > 0);
  12.       except
  13.         on e:Exception do
  14.           writeToMemoParent('checkPNSNalreadyInserted Error...'+e.Message);
  15.       end;
  16.     finally
  17.       ts.Free;
  18.     end;
  19.   end;
  20.   if Result then
  21.     writeToMemoParent(PNSN+' already inserted... ');
  22. end;
« Last Edit: October 30, 2018, 08:21:45 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.

440bx

  • Hero Member
  • *****
  • Posts: 5302
Re: function and result
« Reply #24 on: October 30, 2018, 08:44:48 pm »
Code: Pascal  [Select][+][-]
  1.   if Result then
  2.     writeToMemoParent(PNSN+' already inserted... ');
  3.  
As long as writeToMemoParent cannot cause an exception then it should be ok.  If writeToMemoParent can cause an exception then the contradiction is still there.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v4.0rc3) on Windows 7 SP1 64bit.

FTurtle

  • Sr. Member
  • ****
  • Posts: 292
Re: function and result
« Reply #25 on: October 30, 2018, 09:05:00 pm »
I don't understand why people so like long blocks under "if".
On my opinion in this case the most logically is to write:

Code: Pascal  [Select][+][-]
  1. function TEvarilBuffClass.checkPNSNalreadyInserted(dirPathOutputFile: string; PNSN: string): Boolean;
  2. var
  3.   ts : TStringList;
  4. begin
  5.   if not FileExists(dirPathOutputFile)) then
  6.     Exit(False);
  7.  
  8.   // All other without begin-end
  9.  
  10. end;
  11.  
« Last Edit: October 30, 2018, 09:18:55 pm by FTurtle »

creaothceann

  • Full Member
  • ***
  • Posts: 117
Re: function and result
« Reply #26 on: October 30, 2018, 09:16:44 pm »

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: function and result
« Reply #27 on: October 30, 2018, 09:18:47 pm »
If writeToMemoParent can cause an exception then the contradiction is still there.

No, if writeToMemoParent raises an exception in this case, the function doesn't return normally so the result doesn't matter at all.
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.

440bx

  • Hero Member
  • *****
  • Posts: 5302
Re: function and result
« Reply #28 on: October 30, 2018, 09:37:56 pm »
If writeToMemoParent can cause an exception then the contradiction is still there.

No, if writeToMemoParent raises an exception in this case, the function doesn't return normally so the result doesn't matter at all.
If an exception happened, it seems a bit of a contradiction for the function to return a result of true when it didn't execute successfully. 
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v4.0rc3) on Windows 7 SP1 64bit.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: function and result
« Reply #29 on: October 30, 2018, 10:03:12 pm »
If an exception happened, it seems a bit of a contradiction for the function to return a result of true when it didn't execute successfully.

Let's repeat, slowly:

If in my second code writeToMemoParent raises an exception the function doesn't return normally. Because it has raised an uncatched exception. Whatever the function's result is doesn't matter at all since it isn't returned.

Do note, though, that if it were returned it would be the correct result because the intent of the function is to check whether the file contains PNSN. If it were my program, in fact, I would implement just that and let all the writeToMemoParent or whatever to the caller.
« Last Edit: October 30, 2018, 10:05:08 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.

 

TinyPortal © 2005-2018