4

they both seem to accomplish the same things with different syntax, whats the point of using two different technologies. Please highlight every possible benefit of using mxml. Also are there scenarios when one is more beneficial than the other and why.

Please clarify this runtime behavior of mxml vs AS3 as discussed in Oreilly Flex 4 Cookbook page 1:

" Many newcomers to Flex wonder how MXML and ActionScript relate to one another. The MXML compiler (mxmlc), after parsing through the different idioms, translates them into the same objects, so that this:

<s:Button id="btn" label="My Button" height="100"/>

and this:

var btn:Button = new Button();
btn.label = "My Button";
btn.height = 100;

produce the same object. The major difference is that while creating that object in ActionScript (the second example) creates the button and nothing else, creating the object in MXML adds the button to whatever component contains the MXML code. The Flex Framework handles calling the constructor of the object described in MXML and either adding it to the parent or setting it as a property of the parent. "

timmaktu
  • 163
  • 1
  • 1
  • 12

7 Answers7

8

Usually,

  • MXML is used to build the user interface
  • ActionScript is used to code the logic of your application

Of course, you can also build the user interface with ActionScript but :

  • it will take you more time
  • MXML views are easier to read than AS views

In terms of user interface, ActionScript should be used only to manage view elements (add, remove, ...) at runtime.

Florian F
  • 8,822
  • 4
  • 37
  • 50
  • +1 good answer. One other thing: building full UIs in ActionScript results in very verbose (and, IMHO, ugly) ActionScript code, which can be difficult to maintain. Since MXML is designed for easily specifying UI components and their layout, it simply makes sense to utilize it. – bedwyr Nov 01 '10 at 02:37
  • 1
    I think we've all heard about Flex's spaghetti code, using mxml may make for pretty code but it doesn't follow that the underlying code is as "pretty". For small projects, I'd rather build my own UI with AS3! – PatrickS Nov 01 '10 at 03:58
2

Edit

Within a Flex application, one is expected to favor using mxml over AS, provided both uses are available. In case mxml doesn't provide a solution , the user can fallback to AS3. Flex is a framework, therefore you would expect that the emphasis is put towards mxml, rather than AS3. Like most frameworks , the idea is to hide the details in order to facilitate design and supposedly make programming more accessible.

Everything comes at a cost though, you can't really expect mxml to be faster than AS3 since it's built on top of it. A logical conclusion would be to decide on a case basis. What are your app requirements, is there really a performance gain in using this component or not etc... Apart from the fact that Flex as a whole is bound to be slower than AS3, I don't think you can set general rules. One could add that if one is worried about performance, why choose Flex in the first place...

End of Edit

The Flex framework is geared towards building RIAs, therefore it comes with a bunch of components to that effect and mxml which mainly deals with the layout of these components, Actionscript being the language the Flex framework is built upon, will typically be used for the logic of your application.

The question is not really why choose one over the other. It's the same approach you would have if you decide to develop using a language directly or using a framework based on this language. It's like comparing JQuery & Javascript or CodeIgniter & PHP .I don't mean to imply that the reasons to use JQuery are the same regarding using Flex. Whilst JQuery will attempt to hide browser specificities and make JS more accessible , Flex's purpose is entirely different.

If you're developing RIAs, Flex makes sense, if you're developing games, not really. All you can do with Flex can be done with AS3 , only you wouldn't want to spend time developing DataGrids or HBoxes whilst you could spend more time on your app structure & design.

On the other hand, if you're developing a website or a game , you wouldn't want to add the weight of the Flex framework to your swf, simply because you've added a few Image components and a couple of Labels.

Flex is not adapted to every situation, unlike my previous Javascript or Php examples , Flex's purpose is not to make the language it's based upon more accessible or easier to deal with. Flex has a very specific target.

PatrickS
  • 9,539
  • 2
  • 27
  • 31
  • I agree with your post but I think it doesn't answer the question. The question wasn't about choosing the Flex framework vs a plain AS project. I think he asked, in a Flex project, when to use MXML over AS – Florian F Nov 01 '10 at 10:54
2

A similar question might be "what is the advantage of using HTML over Javascript for the UI?"

I suppose you are really asking "what is the advantage of using a declarative UI over an imperative one?"

Declarative UIs are more like configuration -- no logic (or in the case of MXML, very little logic). Declarative UIs separate the specification of the view from the behavior and data.

Tooling almost always prefers declarative UIs as well, because declarative specs tend to be extremely well suited to serialization... much easier for a tool to update MXML than ActionScript with properties and such.

As you get more familiar with the concept, declarative UIs start to feel more natural, and most people tend to prefer them.

Brian Genisio
  • 47,787
  • 16
  • 124
  • 167
0

Productivity, you create view elements with MXML faster than with AS3.

Narek
  • 1
  • 1
0

Under the surface MXML get's converted to the same AS3 bytecode as if you'd write in plain AS3 so the question if MXML makes the app slower comes down to how well the Flex compiler does the conversion.

As for the original question I would say that MXML is for visual development but as said before you hit the limits when trying to create custom components. Personally I write Flex apps in 99.9% plain AS3 because I can't stand MXML. The only MXML file is the required Entry class so the compiler knows it should be a Flex app that it compiles.

BadmintonCat
  • 9,416
  • 14
  • 78
  • 129
0

Either way your app is going to get converted to an ActionScript object.

The advantage of using MXML is user friendly and more control on the look and feel of application. sometimes its easy to fix issues aligining your components on the right pane or panel with MXML.

The advantage i see using actionScript is command over your application, you have more control. Sometimes your MXML does not get you the itch of what you are looking and actionScript gives you more power.

I would suggest you to play with both to build an application which can give you less headache and better results.

Thalaivar
  • 23,282
  • 5
  • 60
  • 71
-4

None, Flex is for learning, as you learn more you use more .as and then one day... you use 0 Flex. For one it's slow.

(edit - answer to comments) 'Why is flex slow' - it creates computer generated .as code. Equivalent functionality in .as is faster and smaller. 'MXML separates UI from code' - I'd say no it does not. First of ... it's XML.

If you are new, check out other frameworks, like Away3d, FDT and MinimalComponents. As you get more advanced, you use less MXML and one day no MXML, you are writing pure .as. Anything in Flex can be done in HTML5/.js better. Also .as is much better than HTML5/.j It's like a learning tricycle, and good for corporate developers that do drag and drop, they think of themselves as programmers. Software engineers... don't stay with Flex for long before evolving. Here: http://www.youtube.com/watch?v=TL2t8eOs1XE I feel sorry for you if you stay in Flex and don't advance.

  • Actually, your app won't be faster if you write AS code instead of MXML. As long as you're using the Flex framework, whether it's written entirely with ActionScript, or with a MXML/AS mix, performances will be the same – Florian F Nov 01 '10 at 10:59
  • I think you need to back up that statement... why is Flex slow? – Gregor Kiddie Nov 01 '10 at 12:27
  • maybe you mean that AS code generated from mxml might be ugly? – www0z0k Nov 01 '10 at 15:26
  • I assume you mean "MXML" is for learning. If that is what you think, then I would say that you are doing it wrong. MXML is for separating the UI from the logic. If you get to a point in Flex where you are writing 0 MXML and all AS3, then you are doing nobody a service. – Brian Genisio Nov 01 '10 at 21:50