mx Free mxml

Tags: Flex
Word Count: 407

I was reading through my blog roll this morning (and when I say this morning I really mean early afternoon because I am off) when I came across a very interesting post. We have all either used flex or at the very basics have seen some MXML code. Here is a quick hello world example that just puts a button on the screen.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
    
    <mx:Button label="Hello World!"/>
    
</mx:Application>

Nothing special going on there right. Mrinal Wadhwa has a great article about writing flex applications without the mx prefix to each of the tags. All you have to do is simply change the application tag to match the one below. Then all of your tags can be written without using mx: prefix. I thought well this is great but I am still a newbie and really need the code insight that Flex Builder offers. To my amazement you still have your code insight. Mrinal offers up a couple of reasons on why this approach is better and in my opinion I could not agree more.

    1. I think this makes the code a lot cleaner
    2. There are many who do not use Flex Builder and for them this saves a lot of typing.

Here is the new code, go ahead and change your application tag to the following and have fun saving time. What a great tip Mrinal!

<?xml version="1.0" encoding="utf-8"?>
<Application xmlns="http://www.adobe.com/2006/mxml">
    
    /* now you can use the tags without mx prefix and code hinting still works */
    <Button label="Hell World!"/>
    
    
</Application>


Comments

#1 Posted By: Simeon Posted On: 7/6/07 1:54 PM
In general i am all for less typing. However since the second thing you build after your hello world applications starts to use custom components, you are going to have namespace prefixes in your code anyway. I think the mixing of prefixes and non prefixed tags is very distracting.

I like using the mx namespace because it creates a look of consistency through my application.
#2 Posted By: Dan Vega Posted On: 7/6/07 2:00 PM |
Author Comment
It makes sense to me that when you start importing new namespaces you may run into problems. Please remember that I am a Flex newb and from very basic applications this seems easier to me. I may change my mind when my Flex skills increase. Thanks for the thoughts though, it is something I will be sure to look out for.
#3 Posted By: Mrinal Posted On: 7/6/07 2:34 PM
Hi Dan,
I'm very glad you like the tip. This will not cause any problem when you start using other namespaces for your custom components,

Simeon,
I guess its a question of preference .. one point to add though (for people who are new to this) is that even when inside components you can still follow this approach ... for example when you are inside a component inheriting from a Canvas you could set the namespace in the canvas tag as xmlns=" and then the code of the component will be mx free as well ... so I think things are consistent enough ... again this is a preference thing.
#4 Posted By: Evert Posted On: 7/6/07 3:09 PM
There's actually no issue with importing new namespaces. You can mix and match freely..

This is really an XML namespaces discussion, instead of a Flex one.. I would want to suggest to anyone who uses MXML to understand how XML works and read up on for example XML namespaces.
#5 Posted By: Josh Posted On: 7/6/07 5:46 PM
Personally, I prefer to use the mx namespace, but certainly, I can understand why someone who doesn't use Flex Builder would like to code in MXML with less typing. :)
#6 Posted By: Tony Posted On: 7/6/07 5:54 PM
I agree with Simeon. If you made this change, ad then added the most basic of custom components it would be given (in FlexBuilder) the default prefix 'local'.

To me the prefix conveys userfl information about where the classes come from, and sister classes in the same project shoud have no prefix, and classes from the framework should be maked as such.

Just my $0.02
#7 Posted By: Tink Posted On: 7/6/07 6:29 PM
I concur and advice against this. Consistancey is key!

"e inside a component inheriting from a Canvas you could set the namespace in the canvas tag as xmlns=" and then the code of the component will be mx free as well ... so I think things are consistent enough"

So when you come to use this custom component in another class, along with Adobe components and a couple of others u have created in diff packages, your back to having to use namespaces (or back to having a mix which I would really try to avoid).

"Please remember that I am a Flex newb and from very basic applications this seems easier to me"

Start as you mean to go on and don't get into bad habits!
#8 Posted By: Mrinal Posted On: 7/7/07 8:25 AM
Guys,
I do not agree on the consistency point ... I do see that the mx prefix obviously states that all tags starting with mx prefix represent classes from the flex framework ..... but the same message could very easily be conveyed by not using any prefix for classes from th flex framework .... many other XML based languages do it that way (XHTML, XUL etc) ... the default name space does not have any prefix and everything else does ...... to me these languages seem quite consistent and readable.

One point against this approach that I do agree to is this .... If your code s going to be public facing and absolute novice flex programmers are going to look at it ... then this approach may throw them off a bit as all the mxml that they have seen until this point used mx prefix and your code does things differently.

So i still think its a preferences thing.
#9 Posted By: Keith Peters Posted On: 7/9/07 11:14 AM
I think it comes down to best practices. Yeah, it may work, it may be easier, but it is not a best practice. It may never even come back to bite you, but it does put you out of step with everyone else who is using best practices. Fine for your your personal projects, but you go get a job doing Flex somewhere, and the other devs are going to slap you.
#10 Posted By: joeflash Posted On: 7/9/07 12:35 PM
Normally I would agree this is a time-saving tip. For writing pseudo code on whiteboards, it could definitely make things easier. But, as others have said, getting into the practice of not using the mx namespace will look funny as soon as you start using custom namespaces.

Given that typing the mx namespace takes no time at all when using the auto-completion in Flex (Ctrl-SPACE), which is quite powerful compared to Flash or DW, I would agree with the above that, as a best practice, the mx namespace should be included at all times.

Although from a capabilities standpoint it's good to know that Flex is 'flex'-ible ;) enough to assume the mx namespace has been included if the developer prefers not to use it.


Post Your Comment







Show Captcha

If you subscribe, any new posts to this thread will be sent to your email address.

Copyright © 2007 Dan Vega | BlogCFC was created by Raymond Camden. This blog is running version 5.8.001.