Everyone has their own opinion on how source code should be structured and formatted. What really matters when it comes to code is not saving the CPU a couple cycles, rather enabling others to read and understand what you’ve written. This comes from Martin Fowler’s classic book Refactoring:
There is another user of your source code. Someone will try to read your code in a few month’s’ time to make some changes. We easily forget that extra user of the code, yet that user is actually the most important. Who cares if the computer takes a few more cycles to compile something? It does matter if it takes a programmer a week to make a change that would have taken only an hour if she had understood your code.
When it comes to XAML, there aren’t too many opinions/articles talking about best practices (compared to other languages/markup). In fact, the definitive guide for XAML guidelines is Jaime Rodriguez’s (site) best practice web cast series and the resulting whitepaper (pdf).
Jaime’s whitepaper is a must read for anyone in the Silverlight space.
Since my blog is less formal than a whitepaper, I’m able to give a stronger opinion on what I think are the best practices for XAML. So, without further ado here are my thoughts on XAML:
Formatting
First and foremost, I think formatting is the number one concern.
- Attributes should be on a separate line,
- x:Name should come first,
- and related attributes (such as HorizontalAlignment and VerticalAlignment) should be grouped.
Take the below code blocks, Figure-1 and Figure-2, show the difference between having all the attributes on the same line versus on different lines. In my opinion, Figure-2 is much easier to digest than Figure-1. By having the x:Name first, you can quickly identify what the element is. Once you find the object you are looking for, you can then quickly identify related objects since they are grouped together (such as RadiusX and RadiusY, Width and Height, and HorizontalAlignment and VerticalAlignment).
The biggest complaint about Figure-2 is “wasted space”. The code inflates from 8 lines to 30; however, to Martin Fowler’s point, it’s not about you or the computer, it’s about other people being able to read and quickly assimilate to your code.
Naming conventions
Name everything
Try to specify an x:Name attribute for every element in XAML. If nothing else, this will make you think if you really need the object. For example, at times it’s tempting to embed a panel within a panel to achieve a specific layout. In many instances I have caught myself doing this, mainly out of laziness. After refactoring I find that if I tweaked a margin or used a different kind of panel, there would have been no need for the extra panel. Forcing an x:Name on an object really makes you think of the purpose of that element.
Describe elements
Instead of pre or post-fixing element names, give them a descriptive name. Instead of:
- btnSubmit use SubmitButton,
- grdContent use ContentPanel,
- stkPnlNav use NavigationPanel.
Programmers like using the pre-fix because you can quickly see all of the same type when using intellisense, and you can easily identify the type of an element. Admittedly, I come from this camp; however it’s time to change. A couple reasons for the change: 1.) designers will increasing be working in Blend and they are not going to be geeks like most of us and pre-fix with the type 2.)descriptive names are much easier to read and understand the cryptic typed names (think stkPnl=StackPanel or me=MediaElement).
Lastly, give all panels the same post-fix because you never know if you will have to change the type of a panel. I assure you, it’s an enormous pain, and large risk, to change the x:Name in a project. For example imagine having to change “BufferGrid” to “BufferCanvas” to “BufferStackPanel”. Not only do you have to change the XAML, you have to ensure all instances in the code are changed too. Better to go with the generic “BufferPanel” and be done with it.
Disclaimer
Best practices should be followed to the T; however, there are times when we’re in a crunch and the guidelines fall the way side. Best practices are designed to eliminate maintenance time and promote consistency. I am certainly guilty of not following the above guidelines, however it’s my goal to follow these consistently as possible.
Related posts:



February 10th, 2009 at 12:34 am
I prefer the formatting you describe too. It’s as important for me to be able to read it as anyone else. Also it’s a pain to keep scrolling left to right to see everything too when editing. Now, if only blend would format like that…
February 10th, 2009 at 6:11 am
I agree with most of the guidelines with the exception of the prefix. Giving a prefix not only quickly gives you a list of the same type of controls on a page it also tells you what you can do with that control. What happens if you have a control on the page called Gender? This could be a dropdownlist, radio button or checkbox (or something else all together) by having a prefix you immediately know how to set the information or get the information. It is the same reason you have a prefix for vairable names. There is no reason you can’t make something descriptive and use a prefix. One last thing, if developers and designers have to work together they are both going to have to adapt to how the other works, which means trying to stick to each others standards.
February 10th, 2009 at 6:12 am
[...] more here [...]
February 10th, 2009 at 6:13 am
To work based on best practices is every time a good ideaPeter Loebeö
February 11th, 2009 at 4:44 am
@steveThen you will name control GenderCombo, GenderButton or GenderCheckBox
. I was using prefixes before but turned a year ago to using longer suffix because it is easier to read and understand. What means btn, txt, cnt, ddl… and others prefixes?
February 11th, 2009 at 11:10 am
[...] XAML Guidelines And Best Practices Source: 85turns Excerpt: [...]
April 23rd, 2009 at 3:38 pm
nice article Corey, thanks for the info
October 12th, 2009 at 11:29 am
I agree with most guidelines except the one about naming every element. If you follow MVVM, the best way to know you are on the right track is when your View (the XAML) no longer needs any x:Names or event handlers, because you rely complely on bindings to the ViewModel properties.
October 12th, 2009 at 2:55 pm
Thanks Anthony. True MVVM encourages Xaml without names, however, when working with Blend it’s still important to have each element with a name. Much like working in Photoshop you name Layers, Blend should be the same way.
January 3rd, 2010 at 4:50 pm
My brother in law would appreciate this post. We were just speaking about this. hehe
April 7th, 2010 at 10:38 am
tia mowry pussy com
fattest person in the world 2005
pepe de buca
www chip and dale dancers com
coco austin pussy pic
videos descuidos chilenas
poems with homonyms for kids
sbcglobal pop3 and smtp server settings
pantyhose encasement
1970 hairstyle photos
33 new naked winx club
4l80e casting numbers
www youngharriscollege com
milena roucka nude
at t worldnet login isp
walmart my benefit
examples of poems with sensory images
what does fram ph3593a fit
marshall 1971 football results
tokarev pistol parts for sale
April 24th, 2010 at 9:16 am
free oklahoma police scanner frequencies
debajo de las faldas
youtube parejas cachondas
plain colored layouts
carrie underwood pussy fucked
ti and tameka cottle baby pictures
monjas cojiendo y mamando vergas
dihybrid cross problems
murray mower diagram
al extremo tvazteca
skb 500 shotgun reviews
buca di pepo restaurant sacramento california
videos de jaripeos rioverde slp
yapoo s market manj 01
rey de oros en el monte
walmart wedding cakes
kata hikmah
toni bambara biography
beckett oil burner parts list
camaras bajo las faldas
May 2nd, 2010 at 12:39 am
examples of quatrain poems poet
basil plumley
wet tee shirt photos
nikki s car crash
coithienthai truyen nguoi lon video
ariens 524 snowblower manual
ankylosaurus poem
bi rite furniture houston
mujeres culona tonta
playboy pics house wife of ny
colt firearms
22 caliber am15
bitty schram nude in movie thief
buck hunter 50 cal pistol
yl2 27ch2
kendra baskett nude pics in the shower
forbidden lolitia
infant umbilicus
peru nude woman
2008 tacoma pickup belt routing
May 20th, 2010 at 9:38 am
Hello,this is Reynaldo Bouer,just identified your Blog on google and i must say this blog is great.may I quote some of the information found in this weblog to my local friends?i’m not sure and what you think?anyway,Thank you!
May 25th, 2010 at 10:32 pm
hey,I discover that your website is quite informative and useful and we wonder if there is really a possibility of obtaining More articles or blog posts like this on your website. If you willing to assist us out, we can be willing to compensate you… Sincerely, Lynwood Jason
May 28th, 2010 at 3:32 am
meijers super store ads
airsoft acr gun
viejitas buscando vergas
bombshell 19 and crazy lyrics
childrens cornrow pictures
omega psi phi secrets pledge
fn 5 7 x28 ballistics
decorasiones de fiesta cumpleanos mickey y mini
gm rear axle identification numbers
44 mag ballistics
17 hmr vmax bc
coolsat dongle adapter
roaster pig oven
kai raye
remington 1866 pistols
cincinnati prime outlet monroe ohio
braun thermoscan 6022 manual
nakedsoccermoms com
springfield m6 scout for sale
natalya neidhart fake nude pics
May 29th, 2010 at 8:54 pm
Greetings, nice blogging. Want to get money for blogging? Check out: http://bit.ly/PaidWriting
June 4th, 2010 at 4:16 pm
Intriguing article. Had been did you got all of the information from…
June 10th, 2010 at 6:27 am
Hi guys, I’ve found a real good tool for email marketing that amazingly works! Let check this as it may change your business life!
June 27th, 2010 at 1:12 pm
“Words CAN’T say it! In less time than it takes to boot, I had it scanned, diagnosed and FIXED! Did the trick to get my system back in shape and running at maximum speed.”
July 13th, 2010 at 3:08 am
I agree with Antony.I don’t agree with the statement “you should name all XAML elements”Really this is not good approach to name elements.It is not needed as XAML markup when used in MVVM have not to be addresses from code behind at all.Sorry but that is my opinion.Thanks for other great tips. Good article.Thanks,Kirill
September 30th, 2010 at 4:10 am
airsoft is so damn addicting and i love to play it all day;-;
October 12th, 2010 at 3:10 pm
Hi this article was very interesting and funny for me. But it was difficult to find it with google.com. Maybe you should improve it with seo plugins for wordpress like headspace. Just a tip
October 16th, 2010 at 11:47 pm
Fantastic publish! I will subscribe at the moment wth my feedreader software program!
October 17th, 2010 at 11:51 am
we always play airsoft on our home made battle grounds .*:
October 17th, 2010 at 9:51 pm
Before you make just about any purchase of child furniture you should always check around for best characteristics and designs that best suits you and your kid’s specific taste along with liking; because you constantly want to supply the best things to your infant but at the same time, you desire your home to produce a sense of great taste.