<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Corey Schuman &#187; best practices</title>
	<atom:link href="http://www.85turns.com/tag/best-practices/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.85turns.com</link>
	<description>Silverlight Consultant</description>
	<lastBuildDate>Tue, 26 Apr 2011 20:57:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.1</generator>
		<item>
		<title>XAML guidelines and best practices</title>
		<link>http://www.85turns.com/2009/02/09/xaml-guidelines-and-best-practices/</link>
		<comments>http://www.85turns.com/2009/02/09/xaml-guidelines-and-best-practices/#comments</comments>
		<pubDate>Tue, 10 Feb 2009 05:33:57 +0000</pubDate>
		<dc:creator>Corey</dc:creator>
				<category><![CDATA[UI]]></category>
		<category><![CDATA[best practices]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[wpf]]></category>
		<category><![CDATA[XAML]]></category>

		<guid isPermaLink="false">http://www.85turns.com/2009/02/09/xaml-guidelines-and-best-practices/</guid>
		<description><![CDATA[Everyone has their own opinion on how source code should be structured and formatted.&#160; 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.&#160; This comes from Martin Fowler’s classic book Refactoring: There is another user of your source [...]]]></description>
			<content:encoded><![CDATA[<p>Everyone has their own opinion on how source code should be structured and formatted.&#160; 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.&#160; This comes from Martin Fowler’s classic book <a href="http://books.google.com/books?id=1MsETFPD3I0C&amp;printsec=frontcover">Refactoring</a>:</p>
<blockquote><p><font style="background-color: #ffffff">There is another user of your source code.&#160; Someone will try to read your code in a few month&#8217;s’ time to make some changes.&#160; We easily forget that extra user of the code, yet that user is actually the most important.&#160; Who cares if the computer takes a few more cycles to compile something?&#160; 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.</font></p>
</blockquote>
<p>When it comes to XAML, there aren’t too many opinions/articles talking about best practices (compared to other languages/markup).&#160; In fact, the definitive guide for XAML guidelines is Jaime Rodriguez’s (<a href="http://blogs.msdn.com/jaimer/default.aspx">site</a>) best practice web cast series and the resulting whitepaper (<a href="http://www.cookingwithxaml.com/content/xamlguidelines/xamlGuidelinesDraft0.pdf">pdf</a>).</p>
<blockquote><p><a href="http://blogs.msdn.com/jaimer/archive/2008/12/16/xaml-practices-series-part1.aspx">XAML practices series, part1</a></p>
<p><a href="http://channel9.msdn.com/shows/Continuum/XAML-Guidelines-Part-2/">XAML practices series, part2</a></p>
<p><a href="http://blogs.msdn.com/jaimer/archive/2009/01/13/xaml-guidelines-part-3-draft.aspx">XAML practices series, part3</a></p>
</blockquote>
<p>Jaime’s whitepaper is a must read for anyone in the Silverlight space.&#160; </p>
<p>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.&#160; So, without further ado here are my thoughts on XAML:</p>
<h2>Formatting</h2>
<p>First and foremost, I think formatting is the number one concern.&#160; </p>
<ul>
<li>Attributes should be on a separate line,</li>
<li>x:Name should come first,</li>
<li>and related attributes (such as HorizontalAlignment and VerticalAlignment) should be grouped.</li>
</ul>
<p>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.&#160; In my opinion, Figure-2 is much easier to digest than Figure-1.&#160; By having the x:Name first, you can quickly identify what the element is.&#160; Once you find the object you are looking for, you can then quickly identify related objects since they are grouped together (such as <font face="Courier New">RadiusX</font> and RadiusY, Width and Height, and HorizontalAlignment and VerticalAlignment).</p>
<p>The biggest complaint about Figure-2 is “wasted space”.&#160; 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.</p>
<p><font face="Times New Roman" color="#c0c0c0" size="2">Figure-1</font>    <br /><a href="http://www.85turns.com/blog/wp-content/uploads/2009/02/image1.png"><img title="image" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="143" alt="image" src="http://www.85turns.com/blog/wp-content/uploads/2009/02/image-thumb.png" width="596" border="0" /></a>&#160;</p>
<p><font face="Times New Roman" color="#c0c0c0" size="2">Figure-2</font>    <br /><a href="http://www.85turns.com/blog/wp-content/uploads/2009/02/image2.png"><img title="image" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="527" alt="image" src="http://www.85turns.com/blog/wp-content/uploads/2009/02/image-thumb1.png" width="363" border="0" /></a> </p>
<h2>Naming conventions</h2>
<h3>Name everything</h3>
<p>Try to specify an x:Name attribute for every element in XAML.&#160; If nothing else, this will make you think if you really need the object.&#160; For example, at times it’s tempting to embed a panel within a panel to achieve a specific layout.&#160; In many instances I have caught myself doing this, mainly out of laziness.&#160; 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.&#160; Forcing an x:Name on an object really makes you think of the purpose of that element.</p>
<h3>Describe elements</h3>
<p>Instead of pre or post-fixing element names, give them a descriptive name.&#160; Instead of:</p>
<ul>
<li>btnSubmit use SubmitButton,</li>
<li>grdContent use ContentPanel,</li>
<li>stkPnlNav use NavigationPanel.</li>
</ul>
<p>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.&#160; Admittedly, I come from this camp; however it’s time to change.&#160; A couple reasons for the change: 1.) designers will increasing be&#160; 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).</p>
<p>Lastly, give all panels the same post-fix because you never know if you will have to change the type of a panel.&#160; I assure you, it’s an enormous pain, and large risk, to change the x:Name in a project.&#160; For example imagine having to change “BufferGrid” to “BufferCanvas” to “BufferStackPanel”.&#160; Not only do you have to change the XAML, you have to ensure all instances in the code are changed too.&#160; Better to go with the generic “BufferPanel” and be done with it.</p>
<h2>Disclaimer</h2>
<p>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.&#160; Best practices are designed to eliminate maintenance time and promote consistency.&#160; I am certainly guilty of not following the above guidelines, however it’s my goal to follow these consistently as possible.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.85turns.com/2009/02/09/xaml-guidelines-and-best-practices/feed/</wfw:commentRss>
		<slash:comments>26</slash:comments>
		</item>
	</channel>
</rss>

