Skip to main content
March 11, 2010
Question

Concept of Subroutine or Method in MXML?

  • March 11, 2010
  • 1 reply
  • 14488 views

Hi. Does Flex MXML have the concept of a subroutine (like VB) or a method (like ActionScript)?

Reason I ask is that I have some MXML code which will be duplicated (yecch!) unless I can refactor it into a subroutine of some sort.

Ben Ethridge

Senior Java Developer / Flex Newbie

    This topic has been closed for replies.

    1 reply

    March 12, 2010

    I'm not quite sure what you are asking. You can put <mx:Script> blocks in your MXML to define methods in ActionScript. You can define self-contained Components and reuse them within your application or across applications. You can also use MXML States to handle some reusability aspects really neatly. It depends on what you're trying to achieve.

    March 12, 2010

    Most computer languages, even in the early days, had the concept of a "subroutine".  In Visual Basic they are called "Sub". In COBOL they were called PERFORM statements, if I remember correctly. In java they are called methods.

    It falls under the general rule of "DRY" programming, i.e. Do Not Repeat Yourself:

    http://en.wikipedia.org/wiki/Don%27t_repeat_yourself

    That's the new term for a very ancient rule of good program design.

    Obviously I could recode any MXML in ActionScript, which appears to be DRY, but if I want to stay at the MXML level, I have not yet found a way to (ahem) "stay DRY" :-)

    Could possibly be done by making everything a component, but that seems to be a bit of a kludge, i.e. using components for something other than their original intent.

    Ben

    March 17, 2010

    Sometimes it feels like we're going backwards, i.e. the newer languages are more primitive than the older ones. For example, JSP is the only language in J2EE that has the concept of an include file (go figure)...and this (relatively) new MXML does not even have sub'ing.  Pretty sad.

    I just tried using the <mx:Module> to solve the above problem, but alas, even though I selected the "Optimize for application", it was not able to understand that the variables in the <mx:Module> were declared in the <mx:Application>. Is there a way to tell it? i.e. have it behave like a parameterized method?  If there isn't, there should be.

    Otherwise, if I want to eliminate code duplication, I think my only option is to recode the entire app in ActionScript (as opposed to MXML).  :-(

    Sorry for the rant, but IMHO, like relational database theory, all languages should be DRY coz code duplication is probably the #1 nemesis of coders.  I'm hoping Adobe is listening and will enhance for DRY next version.

    Ben