Page 1 of 1

What use hasInstance not isKindOf

Posted: Fri Aug 07, 2009 1:22 pm
by ConvertFromOldNGs
by Scott >> Tue, 12 Aug 2008 9:12:48 GMT

I see use hasInstance but not understand what purpose?

Please explain why not use isKindOf?

Use isKindOf easier read code later so can use always is ok?

Re: What use hasInstance not isKindOf

Posted: Fri Aug 07, 2009 1:22 pm
by ConvertFromOldNGs
by Dean Cooper >> Tue, 12 Aug 2008 9:36:25 GMT

You're correct that isKindOf is generally used most often. However, hasInstance is useful if the object reference you're wanting to check has been deleted (e.g. in a delete notification) because it doesn't dereference (i.e. access) the object, so it can be used to test a reference even if the object it references is no longer valid.

isKindOf is also faster, though you have to be doing a lot of them in a short timeframe to really notice the difference. For example, the test script below does 2m iterations of each check. Over three runs (the first being after a restart of JADE) using a 6.2.15 database server and a fat client, it produce the following output:

isKindOf = 1 seconds
hasInstance = 3 seconds

isKindOf = 1 seconds
hasInstance = 3 seconds

isKindOf = 1 seconds
hasInstance = 3 seconds

So isKindOf is consistently the fastest type check.

testTypeChecks();

constants
Iterations : Integer = 2000000;

vars
n, startClock : Integer;
b : Boolean;
o : Object;
begin

o := ObjectiveArea.firstInstance;

startClock := app.clock;
foreach n in 1 to Iterations do
b := o.isKindOf(ObjectiveArea);
endforeach;
write "isKindOf = " & ((app.clock - startClock) div 1000).String & " seconds";

startClock := app.clock;
foreach n in 1 to Iterations do
b := ObjectiveArea.hasInstance(o);
endforeach;
write "hasInstance = " & ((app.clock - startClock) div 1000).String & " seconds";
end;

Dean.

Re: What use hasInstance not isKindOf

Posted: Fri Aug 07, 2009 1:22 pm
by ConvertFromOldNGs
by Patwos >> Tue, 12 Aug 2008 22:16:45 GMT

Further to Dean's message, I believe around the same time that isKindOf was significantly improved in performance, it used to be similar in performance to hasInstance, it was also changed to work on deleted object references without triggering an error 4 exception.

In Dean's example of a notification that resulted from an object delete you can now safely use isKindOf in current Jade releases, whereas in older Jade releases you had to use hasInstance to avoid error 4 exceptions.

Hope that helps,
Pat.

Re: What use hasInstance not isKindOf

Posted: Fri Aug 07, 2009 1:22 pm
by ConvertFromOldNGs
by Scott >> Thu, 14 Aug 2008 5:17:13 GMT

Thankyou for comments. I use always isKindOf with no problems!