What use hasInstance not isKindOf

For questions and postings not covered by the other forums
ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

What use hasInstance not isKindOf

Postby ConvertFromOldNGs » Fri Aug 07, 2009 1:22 pm

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?

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: What use hasInstance not isKindOf

Postby ConvertFromOldNGs » Fri Aug 07, 2009 1:22 pm

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.

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: What use hasInstance not isKindOf

Postby ConvertFromOldNGs » Fri Aug 07, 2009 1:22 pm

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.

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: What use hasInstance not isKindOf

Postby ConvertFromOldNGs » Fri Aug 07, 2009 1:22 pm

by Scott >> Thu, 14 Aug 2008 5:17:13 GMT

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


Return to “General Discussion”

Who is online

Users browsing this forum: No registered users and 28 guests