XAML guidelines and best practices

Posted by Corey on February 9, 2009 at 10:33 pm.

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).

XAML practices series, part1

XAML practices series, part2

XAML practices series, part3

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.

Figure-1
image 

Figure-2
image

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.

10 Responses to “XAML guidelines and best practices”

  • 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…

  • 2
    Steve Buchok Says:

    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.

  •  

    [...] more here [...]

  • To work based on best practices is every time a good ideaPeter Loebeö

  • @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? :)

  •  

    [...] XAML Guidelines And Best Practices Source: 85turns Excerpt: [...]

  • nice article Corey, thanks for the info

  • 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.

  • 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.

  • My brother in law would appreciate this post. We were just speaking about this. hehe

Leave a Reply