Variables by reference

Samúel Jónasson's Avatar

Samúel Jónasson

22 Aug, 2011 03:06 PM


I have a question for you clever thinkers out there regarding the use of variables by reference. As I understand it, complex objects are always passed on as reference in AS3. So if class B modifies a value object passed to it by class A, then the modification will affect the value object in class A as well as in any other class that is storing it. It is almost as if the value object is a singleton.

Do you utilize this behavior for your advantage?

I have an example from my own code to explain better. I have a LocalizationModel that stores a LocalizationVO. This vo stores a current language id and UI strings that have to be localized. I pass this vo to any view that needs localized strings for buttons and such. Then whenever the language is changed I have a command modify the language id in LocalizationVO. Mediators can then instruct their views to update all strings. In most examples I have seen of similar logic, the language id would be sent along with the request to update - but that seems redundant. The view already has a reference to the vo that the command changed - so why send it - or any info already in it again. The view can simply update all strings according to the language id in it's referenced vo because the change the command made are already there.

There must be a valid reason for coders not to use this functionality. Maybe for readability?

Is it considered bad practice to utilize the fact that complex objects are always passed as a reference?


  1. Support Staff 1 Posted by creynders on 23 Aug, 2011 04:49 PM

    creynders's Avatar

    VO members should be read-only. The reason why it should be avoided to have writable VO members (and this is why I hate data binding in flex too) is because it's really hard to maintain and debug. If a value is changed incorrectly you'll spent a lot of time finding out where exactly it goes wrong.

  2. 2 Posted by Paul Robertson on 23 Aug, 2011 04:57 PM

    Paul Robertson's Avatar

    By the commonly-held definition, value objects are just value containers
    passed along with messages, so they aren't meant to last.

    I've personally always liked the idea of using variables by reference
    instead of pass-along value objects. I'm not sure why, perhaps because
    it might save memory (less object instantiation)?However, I think in
    that case they'd be more "model-y" than "value object-y". That's all
    just names of course. I've never actually gone so far as to try building
    an app that way, so I can't say whether it would actually work in
    practice. I think I'd certainly need to implement some sort of data
    binding, by which I mean at least having a "change" event for each
    property in the object so you can propagate value changes to the screen.


  3. 3 Posted by Samúel Jónasson on 24 Aug, 2011 09:27 PM

    Samúel Jónasson's Avatar

    Thank you for you thoughts on this.

    I do understand the concerns about readability. But since I came from PureMVC where my proxies usually stored all their data in single "SmartVO" I have been creating my models in Robotlegs to hold all their data in a single vo. The properties in them may be simple values or "nested" vo's. So that vo "is a model" and I only use commands to update their values. So for me readability is not an issue.

    But after reading your thoughts on this I do feel like I should not be applying the PureMVC best practices to my models in Robotlegs.


  4. Ondina D.F. closed this discussion on 01 Nov, 2011 02:00 PM.

  5. Ondina D.F. re-opened this discussion on 01 Nov, 2011 02:00 PM

  6. Support Staff 4 Posted by Ondina D.F. on 01 Nov, 2011 02:00 PM

    Ondina D.F.'s Avatar

    If you need more help with this, please feel free to re-open this discussion. Please open new threads for new issues.

  7. Ondina D.F. closed this discussion on 01 Nov, 2011 02:00 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts


? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac