<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: To Brace or not to Brace in C# ifs and other Constructs</title>
	<atom:link href="http://peterkellner.net/2009/10/19/codingstandards-csharp-braces/feed/" rel="self" type="application/rss+xml" />
	<link>http://peterkellner.net/2009/10/19/codingstandards-csharp-braces/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=codingstandards-csharp-braces</link>
	<description>Microsoft Focused, JavaScript,HTML5 (ExtJS, SenchaTouch &#38; Windows 8 Metro)</description>
	<lastBuildDate>Tue, 15 May 2012 21:53:02 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
	<item>
		<title>By: Axel</title>
		<link>http://peterkellner.net/2009/10/19/codingstandards-csharp-braces/#comment-35456</link>
		<dc:creator>Axel</dc:creator>
		<pubDate>Wed, 14 Mar 2012 09:46:03 +0000</pubDate>
		<guid isPermaLink="false">http://peterkellner.net/2009/10/19/codingstandards-csharp-braces/#comment-35456</guid>
		<description>In terms of readability and cleanliness I tend to use braces always. With current screen sizes the space used by those 2 extra lines really doesn&#039;t matter.

However, code should be as easy to understand as possible. That being a fact, you should not override the value of &#039;err&#039; this often.

Your example uses an &#039;inverse importance&#039; of conditions, which makes the code harder to read.

if (myObject1==null) err = &quot;bad1&quot;;
if (myObject2==null) err = &quot;bad2&quot;;
if (myObject3==null) err = &quot;bad3&quot;;
if (myObject4==null) err = &quot;bad4&quot;;
if (myObject5==null) err = &quot;bad5&quot;;

The programs behaviour would be more obvious (and quicker) if you put the most relevant line on top and return right after it or skip the following conditions by returning.

if (myObject5==null)
{
  err = &quot;bad5&quot;;
  return;
}
if (myObject4==null)
{
  err = &quot;bad4&quot;;
  return;
}
if (myObject3==null)
{
  err = &quot;bad3&quot;;
  return;
}
if (myObject2==null)
{
  err = &quot;bad2&quot;;
  return;
}
if (myObject1==null)
{
  err = &quot;bad1&quot;;
  return;
}


or by using else:

if (myObject5==null) err = &quot;bad5&quot;
else if (myObject4==null) err = &quot;bad4&quot;;
else if (myObject3==null) err = &quot;bad3&quot;;
else if (myObject2==null) err = &quot;bad2&quot;;
else if (myObject1==null) err = &quot;bad1&quot;;</description>
		<content:encoded><![CDATA[<p>In terms of readability and cleanliness I tend to use braces always. With current screen sizes the space used by those 2 extra lines really doesn&#8217;t matter.</p>
<p>However, code should be as easy to understand as possible. That being a fact, you should not override the value of &#8216;err&#8217; this often.</p>
<p>Your example uses an &#8216;inverse importance&#8217; of conditions, which makes the code harder to read.</p>
<p>if (myObject1==null) err = &#8220;bad1&#8243;;<br />
if (myObject2==null) err = &#8220;bad2&#8243;;<br />
if (myObject3==null) err = &#8220;bad3&#8243;;<br />
if (myObject4==null) err = &#8220;bad4&#8243;;<br />
if (myObject5==null) err = &#8220;bad5&#8243;;</p>
<p>The programs behaviour would be more obvious (and quicker) if you put the most relevant line on top and return right after it or skip the following conditions by returning.</p>
<p>if (myObject5==null)<br />
{<br />
  err = &#8220;bad5&#8243;;<br />
  return;<br />
}<br />
if (myObject4==null)<br />
{<br />
  err = &#8220;bad4&#8243;;<br />
  return;<br />
}<br />
if (myObject3==null)<br />
{<br />
  err = &#8220;bad3&#8243;;<br />
  return;<br />
}<br />
if (myObject2==null)<br />
{<br />
  err = &#8220;bad2&#8243;;<br />
  return;<br />
}<br />
if (myObject1==null)<br />
{<br />
  err = &#8220;bad1&#8243;;<br />
  return;<br />
}</p>
<p>or by using else:</p>
<p>if (myObject5==null) err = &#8220;bad5&#8243;<br />
else if (myObject4==null) err = &#8220;bad4&#8243;;<br />
else if (myObject3==null) err = &#8220;bad3&#8243;;<br />
else if (myObject2==null) err = &#8220;bad2&#8243;;<br />
else if (myObject1==null) err = &#8220;bad1&#8243;;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dance</title>
		<link>http://peterkellner.net/2009/10/19/codingstandards-csharp-braces/#comment-29957</link>
		<dc:creator>Dance</dc:creator>
		<pubDate>Mon, 16 May 2011 04:34:16 +0000</pubDate>
		<guid isPermaLink="false">http://peterkellner.net/2009/10/19/codingstandards-csharp-braces/#comment-29957</guid>
		<description>great share</description>
		<content:encoded><![CDATA[<p>great share</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Karlyn Covert</title>
		<link>http://peterkellner.net/2009/10/19/codingstandards-csharp-braces/#comment-29204</link>
		<dc:creator>Karlyn Covert</dc:creator>
		<pubDate>Mon, 17 Jan 2011 21:55:39 +0000</pubDate>
		<guid isPermaLink="false">http://peterkellner.net/2009/10/19/codingstandards-csharp-braces/#comment-29204</guid>
		<description>it&#039;s good to see this information in your submit, i used to be wanting the same but there was not any correct resource, thanx now i&#039;ve the link which i used to be looking for my research.</description>
		<content:encoded><![CDATA[<p>it&#8217;s good to see this information in your submit, i used to be wanting the same but there was not any correct resource, thanx now i&#8217;ve the link which i used to be looking for my research.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Janeen Minot</title>
		<link>http://peterkellner.net/2009/10/19/codingstandards-csharp-braces/#comment-27542</link>
		<dc:creator>Janeen Minot</dc:creator>
		<pubDate>Fri, 07 May 2010 21:06:59 +0000</pubDate>
		<guid isPermaLink="false">http://peterkellner.net/2009/10/19/codingstandards-csharp-braces/#comment-27542</guid>
		<description>I must say that your entire blog is incredibly informative.  I have been spending loads of spare time within the last couple months on the lookout at precisely what is on the market based on the very fact that I am planning to launch a blog site.  The important information you have put on here is essentially to the point. It just is likely so mystifying in regards to all the technology that are around, but I enjoy the way your is visually.  Gotta find it irresistible where technologies has come over the last 20 yrs.</description>
		<content:encoded><![CDATA[<p>I must say that your entire blog is incredibly informative.  I have been spending loads of spare time within the last couple months on the lookout at precisely what is on the market based on the very fact that I am planning to launch a blog site.  The important information you have put on here is essentially to the point. It just is likely so mystifying in regards to all the technology that are around, but I enjoy the way your is visually.  Gotta find it irresistible where technologies has come over the last 20 yrs.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Why I Love the Semicolon! &#124; PeterKellner.net</title>
		<link>http://peterkellner.net/2009/10/19/codingstandards-csharp-braces/#comment-25516</link>
		<dc:creator>Why I Love the Semicolon! &#124; PeterKellner.net</dc:creator>
		<pubDate>Sat, 24 Oct 2009 15:23:59 +0000</pubDate>
		<guid isPermaLink="false">http://peterkellner.net/2009/10/19/codingstandards-csharp-braces/#comment-25516</guid>
		<description>[...] so we all want to be somewhat popular, and it seems that my last article, “To Brace Or Not to Brace”, seems to have gotten a lot of attention, so, here goes my opinion on the popular semi-colon [...]</description>
		<content:encoded><![CDATA[<p>[...] so we all want to be somewhat popular, and it seems that my last article, “To Brace Or Not to Brace”, seems to have gotten a lot of attention, so, here goes my opinion on the popular semi-colon [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: mongo lloyd</title>
		<link>http://peterkellner.net/2009/10/19/codingstandards-csharp-braces/#comment-25509</link>
		<dc:creator>mongo lloyd</dc:creator>
		<pubDate>Fri, 23 Oct 2009 09:17:55 +0000</pubDate>
		<guid isPermaLink="false">http://peterkellner.net/2009/10/19/codingstandards-csharp-braces/#comment-25509</guid>
		<description>I agree with always using braces*, mostly because if I don&#039;t, whenever I realise I have to do more than just one statement after an if (or within a for/while, etc), I have to actually reformat the code. Inserting brackets is a bit annoying to have to do. It&#039;s not just a one-keystroke move (unless you have a really intelligent editor). 

Should the code actually have to be reformatted for such a simple and common code change? I think not.


*: Except for in case blocks, where I think it is really daft and pointless. One of my pet peeves actually.</description>
		<content:encoded><![CDATA[<p>I agree with always using braces*, mostly because if I don&#8217;t, whenever I realise I have to do more than just one statement after an if (or within a for/while, etc), I have to actually reformat the code. Inserting brackets is a bit annoying to have to do. It&#8217;s not just a one-keystroke move (unless you have a really intelligent editor). </p>
<p>Should the code actually have to be reformatted for such a simple and common code change? I think not.</p>
<p>*: Except for in case blocks, where I think it is really daft and pointless. One of my pet peeves actually.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: andyclap</title>
		<link>http://peterkellner.net/2009/10/19/codingstandards-csharp-braces/#comment-25498</link>
		<dc:creator>andyclap</dc:creator>
		<pubDate>Thu, 22 Oct 2009 10:31:40 +0000</pubDate>
		<guid isPermaLink="false">http://peterkellner.net/2009/10/19/codingstandards-csharp-braces/#comment-25498</guid>
		<description>Most of the problems solved by consistently bracing single lines are also solved with a code beautifier.

The additional benefits of beautification (consistency, SCM diff) are enough to warrant including beautification as part of the dev process.

Now ... does anybody know a beautifier as good as R# that can be invoked through nAnt?</description>
		<content:encoded><![CDATA[<p>Most of the problems solved by consistently bracing single lines are also solved with a code beautifier.</p>
<p>The additional benefits of beautification (consistency, SCM diff) are enough to warrant including beautification as part of the dev process.</p>
<p>Now &#8230; does anybody know a beautifier as good as R# that can be invoked through nAnt?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ian</title>
		<link>http://peterkellner.net/2009/10/19/codingstandards-csharp-braces/#comment-25495</link>
		<dc:creator>Ian</dc:creator>
		<pubDate>Wed, 21 Oct 2009 14:19:17 +0000</pubDate>
		<guid isPermaLink="false">http://peterkellner.net/2009/10/19/codingstandards-csharp-braces/#comment-25495</guid>
		<description>I use resharper to force {} everywhere possible. I have been bitten too many times by people that write multi-line statements, but forget to add the braces. It can be quite difficult to track down these kinds of bug. I also use CodeKana which gives me a mental image of my code structure without needing to read the code (works very nicely with the {} in the code).</description>
		<content:encoded><![CDATA[<p>I use resharper to force {} everywhere possible. I have been bitten too many times by people that write multi-line statements, but forget to add the braces. It can be quite difficult to track down these kinds of bug. I also use CodeKana which gives me a mental image of my code structure without needing to read the code (works very nicely with the {} in the code).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Juozas</title>
		<link>http://peterkellner.net/2009/10/19/codingstandards-csharp-braces/#comment-25494</link>
		<dc:creator>Juozas</dc:creator>
		<pubDate>Wed, 21 Oct 2009 13:58:54 +0000</pubDate>
		<guid isPermaLink="false">http://peterkellner.net/2009/10/19/codingstandards-csharp-braces/#comment-25494</guid>
		<description>My rule of thumb is: always use braces, except when all you do is return, e.g:

if (param == null)
    return false;

This rule has exceptions - if condition is complex, I use braces even for simple return statement. And I NEVER write statements in condition line.</description>
		<content:encoded><![CDATA[<p>My rule of thumb is: always use braces, except when all you do is return, e.g:</p>
<p>if (param == null)<br />
    return false;</p>
<p>This rule has exceptions &#8211; if condition is complex, I use braces even for simple return statement. And I NEVER write statements in condition line.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Peter</title>
		<link>http://peterkellner.net/2009/10/19/codingstandards-csharp-braces/#comment-25493</link>
		<dc:creator>Peter</dc:creator>
		<pubDate>Wed, 21 Oct 2009 13:19:15 +0000</pubDate>
		<guid isPermaLink="false">http://peterkellner.net/2009/10/19/codingstandards-csharp-braces/#comment-25493</guid>
		<description>@Giraffe and @David:  I would write it the short way, but was just making a point about it does get uglier and is that worth it.</description>
		<content:encoded><![CDATA[<p>@Giraffe and @David:  I would write it the short way, but was just making a point about it does get uglier and is that worth it.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Giraffe</title>
		<link>http://peterkellner.net/2009/10/19/codingstandards-csharp-braces/#comment-25492</link>
		<dc:creator>Giraffe</dc:creator>
		<pubDate>Wed, 21 Oct 2009 09:17:36 +0000</pubDate>
		<guid isPermaLink="false">http://peterkellner.net/2009/10/19/codingstandards-csharp-braces/#comment-25492</guid>
		<description>@David,

I think rather that you should be comparing

if (myObject1==null) err = “bad1″;
if (myObject2==null) err = “bad2″;

with

if (myObject1==null) { err = “bad1″; }
if (myObject2==null) { err = “bad2″; }</description>
		<content:encoded><![CDATA[<p>@David,</p>
<p>I think rather that you should be comparing</p>
<p>if (myObject1==null) err = “bad1″;<br />
if (myObject2==null) err = “bad2″;</p>
<p>with</p>
<p>if (myObject1==null) { err = “bad1″; }<br />
if (myObject2==null) { err = “bad2″; }</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Garry Pilkington</title>
		<link>http://peterkellner.net/2009/10/19/codingstandards-csharp-braces/#comment-25491</link>
		<dc:creator>Garry Pilkington</dc:creator>
		<pubDate>Wed, 21 Oct 2009 08:31:48 +0000</pubDate>
		<guid isPermaLink="false">http://peterkellner.net/2009/10/19/codingstandards-csharp-braces/#comment-25491</guid>
		<description>I agree with you, it makes the code much easier to read and understand. However as soon as I put any else statements in there I judge whether it would look neater using a conditional operator. I always believe that code should be firstly understood by a human, the compiler doesn&#039;t really care either way.</description>
		<content:encoded><![CDATA[<p>I agree with you, it makes the code much easier to read and understand. However as soon as I put any else statements in there I judge whether it would look neater using a conditional operator. I always believe that code should be firstly understood by a human, the compiler doesn&#8217;t really care either way.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: David Kemp</title>
		<link>http://peterkellner.net/2009/10/19/codingstandards-csharp-braces/#comment-25489</link>
		<dc:creator>David Kemp</dc:creator>
		<pubDate>Wed, 21 Oct 2009 08:26:29 +0000</pubDate>
		<guid isPermaLink="false">http://peterkellner.net/2009/10/19/codingstandards-csharp-braces/#comment-25489</guid>
		<description>so you&#039;re saying

if (myObject1==null)
{
 err = &quot;bad1&quot;;
}
if (myObject2==null) 
{
 err = &quot;bad2&quot;;
}

is more readable than

if (myObject1==null) err = &quot;bad1&quot;;
if (myObject2==null) err = &quot;bad2&quot;;

Have you lost your mind?

You&#039;ll be telling me I can only have one &quot;return&quot; statement per function next!</description>
		<content:encoded><![CDATA[<p>so you&#8217;re saying</p>
<p>if (myObject1==null)<br />
{<br />
 err = &#8220;bad1&#8243;;<br />
}<br />
if (myObject2==null)<br />
{<br />
 err = &#8220;bad2&#8243;;<br />
}</p>
<p>is more readable than</p>
<p>if (myObject1==null) err = &#8220;bad1&#8243;;<br />
if (myObject2==null) err = &#8220;bad2&#8243;;</p>
<p>Have you lost your mind?</p>
<p>You&#8217;ll be telling me I can only have one &#8220;return&#8221; statement per function next!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Reflective Perspective - Chris Alcock &#187; The Morning Brew #459</title>
		<link>http://peterkellner.net/2009/10/19/codingstandards-csharp-braces/#comment-25488</link>
		<dc:creator>Reflective Perspective - Chris Alcock &#187; The Morning Brew #459</dc:creator>
		<pubDate>Wed, 21 Oct 2009 07:26:09 +0000</pubDate>
		<guid isPermaLink="false">http://peterkellner.net/2009/10/19/codingstandards-csharp-braces/#comment-25488</guid>
		<description>[...] To Brace or not to Brace in C# ifs and other Constructs - Peter Kellner asks one of the great coding style questions of all time, &#8216;do I have to use braces with that?&#8217; [...]</description>
		<content:encoded><![CDATA[<p>[...] To Brace or not to Brace in C# ifs and other Constructs &#8211; Peter Kellner asks one of the great coding style questions of all time, &#8216;do I have to use braces with that?&#8217; [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bart  Czernicki</title>
		<link>http://peterkellner.net/2009/10/19/codingstandards-csharp-braces/#comment-25487</link>
		<dc:creator>Bart  Czernicki</dc:creator>
		<pubDate>Tue, 20 Oct 2009 23:29:51 +0000</pubDate>
		<guid isPermaLink="false">http://peterkellner.net/2009/10/19/codingstandards-csharp-braces/#comment-25487</guid>
		<description>could be cleaner with the ternary operator:

myObject1 == null ? err = &quot;bad1&quot; : err = &quot;something else&quot;;

Many people code in multiple languages and even in SQL writing code like this can get you in trouble:

if (@someVariable is NULL)
   err = &quot;bad&quot;1
   err2 = &quot;bad also&quot;

err2 would not be under the condition and should have the begin/end (curly bracket equivelant in c#).  So sticking to general good practices is a good idea.</description>
		<content:encoded><![CDATA[<p>could be cleaner with the ternary operator:</p>
<p>myObject1 == null ? err = &#8220;bad1&#8243; : err = &#8220;something else&#8221;;</p>
<p>Many people code in multiple languages and even in SQL writing code like this can get you in trouble:</p>
<p>if (@someVariable is NULL)<br />
   err = &#8220;bad&#8221;1<br />
   err2 = &#8220;bad also&#8221;</p>
<p>err2 would not be under the condition and should have the begin/end (curly bracket equivelant in c#).  So sticking to general good practices is a good idea.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced (User agent is rejected)
Database Caching 27/58 queries in 0.055 seconds using disk: basic
Content Delivery Network via Amazon Web Services: S3: PetersBlogCDN.s3.amazonaws.com

Served from: peterkellner.net @ 2012-05-21 11:39:59 -->
