Recent

Author Topic: function and result  (Read 12812 times)

440bx

  • Hero Member
  • *****
  • Posts: 5302
Re: function and result
« Reply #30 on: October 30, 2018, 10:50:25 pm »
Let's repeat, slowly:
yes, good idea, make that very, very slowly.  A function that suffers an exception - in your last piece of code, one that it doesn't even recover from -  cannot return a value indicating it was successful.   This really shouldn't be difficult to grasp.

IOW, exceptions are not indicators of successful execution.    Slower than that and, soon continental drift will be considered fast.

(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 #31 on: October 30, 2018, 11:05:59 pm »
<sight> Yeah, whatever you say. Bye.
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.

jamie

  • Hero Member
  • *****
  • Posts: 6892
Re: function and result
« Reply #32 on: October 30, 2018, 11:37:52 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;

My comments about the leaks were the fact that you EXIT all over the place but you never free the List you created.
The only true wisdom is knowing you know nothing

Zoran

  • Hero Member
  • *****
  • Posts: 1948
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: function and result
« Reply #33 on: October 30, 2018, 11:44:48 pm »
My comments about the leaks were the fact that you EXIT all over the place but you never free the List you created.

He does in finally block.
Finally block is executed, even after Exit. See the example at the bottom of this page.
Swan, ZX Spectrum emulator https://github.com/zoran-vucenovic/swan

jamie

  • Hero Member
  • *****
  • Posts: 6892
Re: function and result
« Reply #34 on: October 30, 2018, 11:53:24 pm »
That behavior must be different than my old Delphi cause using EXIT in my D3 would simply Exit the procedure and that's it, it would not jump to the
Finally section Unless there were some bug in Delphi, who knows

The only true wisdom is knowing you know nothing

creaothceann

  • Full Member
  • ***
  • Posts: 117
Re: function and result
« Reply #35 on: October 31, 2018, 12:09:53 am »
using EXIT in my D3 would simply Exit the procedure and that's it, it would not jump to the Finally section

Maybe you used exit outside of a try block.

ASerge

  • Hero Member
  • *****
  • Posts: 2411
Re: function and result
« Reply #36 on: October 31, 2018, 01:01:46 am »
A function that suffers an exception - in your last piece of code, one that it doesn't even recover from -  cannot return a value indicating it was successful.
Because in this case the function does not return control to the call point, and that there was set as result is meaningless.

jamie

  • Hero Member
  • *****
  • Posts: 6892
Re: function and result
« Reply #37 on: October 31, 2018, 01:07:56 am »
This EXIT feature had to come out some time after my D3, I just tested it and it does not jump to the finally section, it simply exits the
procedure.
  I do have D7 installed on a laptop, I guess I could test that one...
 
anyways it's no biggie, thanks.
The only true wisdom is knowing you know nothing

440bx

  • Hero Member
  • *****
  • Posts: 5302
Re: function and result
« Reply #38 on: October 31, 2018, 01:23:29 am »
A function that suffers an exception - in your last piece of code, one that it doesn't even recover from -  cannot return a value indicating it was successful.
Because in this case the function does not return control to the call point, and that there was set as result is meaningless.
I agree that the result is meaningless in that case.  My point is simply that in a well written program no function would return a value indicating that it executed successfully when it actually didn't.  Anything else, just isn't "clean".   It's just not good programming.

(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 #39 on: October 31, 2018, 01:39:34 am »
And ASerge's and my point is that setting the result and returning the result are different things. Setting the result as soon as you know it is good programming, whether a later exception will prevent returning it or not.

Of course it is better, as I said above, to try to avoid the possibility of that later exception--which may or may not be easily done. Since the OP used that call to WriteToMemoParent() in his code one would assume he prefers to do it that way, so I followed his lead. Which is good forum etiquette.

As it is also good etiquette not to stay off-topic for too long, this is my last post about this. Ciao!
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 #40 on: October 31, 2018, 02:21:22 am »
Setting the result as soon as you know it is good programming, whether a later exception will prevent returning it or not.
You don't know the result of a function until the function returns _normally_.  Unless a function executes successfully - which includes not exiting through an exception - it shouldn't be setting a result indicating that it executed successfully since it obviously didn't.

Of course it is better, as I said above, to try to avoid the possibility of that later exception--which may or may not be easily done.
Apparently some people (no idea who they are, of course) have difficulties writing decent code.  If writing good code is too difficult for someone then they should probably be doing something else - hopefully something they have some talent for, which makes it relatively easy for them to do it right.

Which is good forum etiquette.
At least you're concerned about good forum etiquette.  Add concern for good code to that and you're all set.

As it is also good etiquette not to stay off-topic for too long, this is my last post about this. Ciao!
That is good news.  Hopefully this time it will actually be that way.

Additionally, it seems you didn't notice that I replied to Serge, not to you.

Ciao (hopefully.)
(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 #41 on: October 31, 2018, 02:37:41 am »
This EXIT feature had to come out some time after my D3, I just tested it and it does not jump to the finally section, it simply exits the
procedure.

Works as expected.

jamie

  • Hero Member
  • *****
  • Posts: 6892
Re: function and result
« Reply #42 on: October 31, 2018, 06:02:45 pm »
Lets clarify this, it works as YOU expected, not me, cause it does not work that way with my old Delphi nor is the EXIT procedure even
documented to work that way, it clearly states that it exits the procedure or function, not any block boundaries.

The only true wisdom is knowing you know nothing

Thaddy

  • Hero Member
  • *****
  • Posts: 16945
  • Ceterum censeo Trump esse delendam
Re: function and result
« Reply #43 on: October 31, 2018, 06:19:17 pm »
Exit() in FreePascal was first. (Not Exit without parameters) Delphi added it later. E.g. D7 doesn't even know that syntax.
It would help if some people have at least a minute grasp of such things. Probably too young.
Well, putting things in perspective has always been an old man's job.,,, 8-)
« Last Edit: October 31, 2018, 06:25:38 pm by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: function and result
« Reply #44 on: October 31, 2018, 08:17:13 pm »
[...] it does not work that way with my old Delphi [...]

IIRC (it's quite some time ago) it was considered a bug and it was corrected in Delphi 3.02. There was also a correction--again IIRC--in the try..finally section of the Language Guide. So it's possible that you both are right, depending on your respective (sub)versions.
« Last Edit: October 31, 2018, 08:18:56 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