Hi !

A while back there was a brief discussion about the relative merits of passing 
an object parameter directly vs passing it as a pointer. As I remember, the 
conclusion was that a pointer wasn’t needed because 4D is just passing a 
reference to the object - it isn’t copying the object in the called method as 
it would do for, say, a text field.

So I proceeded to do object library code just passing the object reference 
directly to save on dereferencing pointers etc. However, now I’ve realised a 
huge problem with this approach which is that it isn’t portable. For example, 
as soon as you use the code in a component it breaks because 4D can no longer 
reference the object across the component-host boundary.

Not only that, even if you pass the object to the component as a pointer, ANY 
further use if the dereferenced object pointer as a parameter in a subsequent 
method call will fail.

So, lets say you have a library method that does this:

===== myObjectMethod ===
C_OBJECT($1)

…and you use that anywhere in a component, it will always fail if its argument 
ultimately originated in a host. (Whereas it wouldn’t fail in a host-only 
call). This is actually consistent behaviour with other data types when you 
think about it, but what isn’t consistent is that the other data types wouldn’t 
work in the host either whereas objects do. (i.e. when you pass an object 
directly as a method argument, any processing on the object will be reflected 
in the calling method’s scope as well which would not be the case for, say, a 
text value).

CONCLUSION
====================
My inclination is to conclude that passing objects directly is not very good 
practice at all because it doesn’t result in consistent behaviour with other 
data types and isn’t portable. Worse, it makes the code difficult to audit if 
you use components because it only takes a single call to break an entire call 
chain that would otherwise perform 100% under test, either in a host 
environment or when unit testing the component.

To get consistent and portable behaviour we’d have to do this:

myMethod(OB Copy($object))  <— to behave like a classic parameter pass where 
the data is duplicated in the called method

or

mtMethod(->$object) <— to behave like a classic pointer, where the source data 
is processed and not duplicated in the called method

but not

myMethod($object)  <— Iceberg waiting for Titanic
====================

What do other people think about this ? Is anyone else bothering about this 
distinction ?

Best Regards

Peter

**********************************************************************
4D Internet Users Group (4D iNUG)
FAQ:  http://lists.4d.com/faqnug.html
Archive:  http://lists.4d.com/archives.html
Options: http://lists.4d.com/mailman/options/4d_tech
Unsub:  mailto:[email protected]
**********************************************************************

Reply via email to