<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Design Guidelines Update: Enums vs Boolean Arguments </title><link>http://blogs.msdn.com/brada/archive/2004/01/12/57922.aspx</link><description>This was a pretty heavily debated guideline internally, I think we reached a good conclusion... Your feedback welcome as always! The full guidelines can be found here , we will be roll this (and other updates) into it for the whidbey release. Enums vs</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: Design Guidelines Update: Enums vs Boolean Arguments </title><link>http://blogs.msdn.com/brada/archive/2004/01/12/57922.aspx#57946</link><pubDate>Mon, 12 Jan 2004 19:19:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:57946</guid><dc:creator>nfactorial</dc:creator><description>I can see why it can become heated, my guideline for boolean parameters are to only use them if the method is a statement. ie.&lt;br&gt;&lt;br&gt;myObject.Enable( true );&lt;br&gt;&lt;br&gt;(Or somesuch, just an example). A pet-hate of mine in the current API is the boolean inside System.String.Compare. When reading code the line:&lt;br&gt;&lt;br&gt;System.String.Compare( stringA, stringB, true );&lt;br&gt;&lt;br&gt;never suggests to me 'Oh, that's case insensitive' (well, it does, but only 'cause I've used the method a few times :).&lt;br&gt;&lt;br&gt;However, I see the reasoning for the above guidelines and they seem pretty solid if adhered to :)&lt;br&gt;&lt;br&gt;n!</description></item><item><title>Design Guidelines Update: Enums vs Boolean Arguments </title><link>http://blogs.msdn.com/brada/archive/2004/01/12/57922.aspx#57976</link><pubDate>Mon, 12 Jan 2004 23:36:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:57976</guid><dc:creator>jeremy's wayward thoughts</dc:creator><description> This from Brad Abrams;&amp;amp;nbsp; This was a pretty heavily debated guideline internally, I think we reached a good conclusion... Your feedback welcome as always!&amp;amp;nbsp; The full guidelines can be found here, we will be roll this (and other updates) into it for the whidbey release. Enums vs Boolean Arguments A framework designer is often faced with the choice of when to use enums and when to use Booleans for method and constructor arguments.&amp;amp;nbsp; In general terms you should favor using enums where it makes the readability of source code more explicit and therefore easier to understand.&amp;amp;nbsp; Anywhere using enums would add unneeded complexity and actually hurt readability of source code Booleans should be preferred.&amp;amp;nbsp; &amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; Do use enums if you two or more arguments that are Booleans. Enums are much more readable (in books, documentation, code reviews, etc).&amp;amp;nbsp; Consider a method call that looks such as: FileStream f&amp;amp;nbsp; = File.Open (&amp;amp;ldquo;foo.txt&amp;amp;rdquo;, true, false); This call gives you no context whatsoever to understand the meaning behind true and false.&amp;amp;nbsp; Now consider if the call where: FileStream f&amp;amp;nbsp; = File.Open (&amp;amp;ldquo;foo.txt&amp;amp;rdquo;, CasingOptions.CaseSenstative, FileMode.Open); Annotation:&amp;amp;nbsp; Some have asked why we don&amp;amp;rsquo;t have a similar guideline for integers, doubles, etc, should we find a way to &amp;amp;ldquo;name&amp;amp;rdquo; them as well?&amp;amp;nbsp; There is a big difference between numeric types and Booleans: you almost always use constants and variables to pass numerics around, because it is good programming practice and you don&amp;amp;rsquo;t want to have &amp;amp;ldquo;magic numbers&amp;amp;rdquo;. However, if you take a look at any managed code-base, this is almost never true of Booleans. 80% of the time a Boolean argument is passed in as a constant, and its intention is to turn a piece of behavior on or off. We could alternatively try to establish a coding guideline that you should never pass a...</description></item><item><title>Design Guidelines Update: Enums vs Boolean Arguments</title><link>http://blogs.msdn.com/brada/archive/2004/01/12/57922.aspx#57979</link><pubDate>Mon, 12 Jan 2004 23:44:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:57979</guid><dc:creator>jeremy's wayward thoughts</dc:creator><description>On enums and Boleans Via Brad Abrams; A framework designer is often faced with the choice of when to use enums and when to use Booleans for method and constructor arguments. In general terms you should favor using enums where it makes the readability of source code more explicit and therefore easier to understand. Anywhere using enums would add unneeded complexity and actually hurt readability of source code Booleans should be preferred....</description></item><item><title>re: Design Guidelines Update: Enums vs Boolean Arguments </title><link>http://blogs.msdn.com/brada/archive/2004/01/12/57922.aspx#58654</link><pubDate>Wed, 14 Jan 2004 18:19:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:58654</guid><dc:creator>Mike Dimmick</dc:creator><description>I'd also suggest the guideline, 'Only use booleans when the two possible values are actually opposites.'&lt;br&gt;&lt;br&gt;Inspired by your use of Bold and NotBold above...</description></item><item><title>re: Design Guidelines Update: Enums vs Boolean Arguments </title><link>http://blogs.msdn.com/brada/archive/2004/01/12/57922.aspx#59371</link><pubDate>Fri, 16 Jan 2004 14:39:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:59371</guid><dc:creator>nfactorial</dc:creator><description>That's ok if the operation is a statement, font.SetBold( true ) [ignore the fact this would be a property, just an example :)]. If it's within a more complex method call true\false doesn't inform the user what its doing...&lt;br&gt;&lt;br&gt;font = CreateFont( &amp;quot;Times New Roman&amp;quot;, true );&lt;br&gt;&lt;br&gt;Does 'true' imply bold? underlined? strikeout?&lt;br&gt;&lt;br&gt;Whereas you can exactly what's happening with the line:&lt;br&gt;&lt;br&gt;font = CreateFont( &amp;quot;Time New Roman&amp;quot;, FontStyle.Bold );&lt;br&gt;&lt;br&gt;n!</description></item><item><title>Design Guideline Updates</title><link>http://blogs.msdn.com/brada/archive/2004/01/12/57922.aspx#60107</link><pubDate>Mon, 19 Jan 2004 13:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:60107</guid><dc:creator>Chris Burrows .NET Blog</dc:creator><description /></item><item><title>re: Design Guidelines Update: Enums vs Boolean Arguments </title><link>http://blogs.msdn.com/brada/archive/2004/01/12/57922.aspx#60611</link><pubDate>Tue, 20 Jan 2004 15:38:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:60611</guid><dc:creator>eriksalt</dc:creator><description>If I understand the above correctly, then the main trade-off here is (1) Increased Readability vs. (2) The increased effort &amp;amp; complexity of having to declare new enum types all the time.  I understand that there are some other side-cases relating to enum performance, but I think that this trade-off represents the main thrust of the guideline.  I had thought that a major factor in deciding which way to go would 'have been how often is the a bool is passed to a family of methods/properties to represent the same concept?'  In other words, if the bool is only used in one method, then it may not be worth it to create the enum.   I think this concept (number of declarations) plays in with the other factors you mentioned above.  I am sure you have thought about this already, and would be interested to hear those thoughts.</description></item><item><title>re: Design Guidelines Update: Enums vs Boolean Arguments </title><link>http://blogs.msdn.com/brada/archive/2004/01/12/57922.aspx#61107</link><pubDate>Wed, 21 Jan 2004 15:07:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:61107</guid><dc:creator>nfactorial</dc:creator><description>Yes, although &amp;quot;having to declare new enum types all the time&amp;quot; makes it sound like a frequent thing when it isn't really. :)&lt;br&gt;&lt;br&gt;Apart from increased readability, an enumeration ensures the programmer is explicit about his\her desire. 'true\false' is quite ambiguous and may lead to versioning problems in the future (note, this is all IMHO :).&lt;br&gt;&lt;br&gt;I'm not saying 'it will cause versioning problems', just that 'its possible'. And reducing 'possible' problems isn't such a bad idea (hopefully).&lt;br&gt;&lt;br&gt;Of course, if the boolean is used in just one method call then an enumeration may just be increasing the amount of work.&lt;br&gt;&lt;br&gt;For such things I would usually create two methods, with differing names. Something akin to 'strcmp' and 'strcmpi', from the C standard library for case sensitive and case insensitive comparisons. Except I'd try and choose more meaningful names :)&lt;br&gt;&lt;br&gt;n!</description></item><item><title>Design guidelines from Microsoft</title><link>http://blogs.msdn.com/brada/archive/2004/01/12/57922.aspx#64401</link><pubDate>Thu, 29 Jan 2004 18:30:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:64401</guid><dc:creator>James Geurts' Blog</dc:creator><description /></item><item><title>Microsoft Design Guidelines</title><link>http://blogs.msdn.com/brada/archive/2004/01/12/57922.aspx#82099</link><pubDate>Mon, 01 Mar 2004 20:34:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:82099</guid><dc:creator>James Geurts' Blog</dc:creator><description /></item><item><title>Write Readable Code By Making Its Intentions Clear</title><link>http://blogs.msdn.com/brada/archive/2004/01/12/57922.aspx#2204026</link><pubDate>Fri, 20 Apr 2007 10:59:02 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:2204026</guid><dc:creator>you've been HAACKED</dc:creator><description>&lt;p&gt;Write Readable Code By Making Its Intentions Clear&lt;/p&gt;
</description></item><item><title>Write Readable Code By Making Its Intentions Clear</title><link>http://blogs.msdn.com/brada/archive/2004/01/12/57922.aspx#2205437</link><pubDate>Fri, 20 Apr 2007 13:22:12 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:2205437</guid><dc:creator>Community Blogs</dc:creator><description>&lt;p&gt;I don’t think it’s too much of a stretch to say that the hardest part of coding is not writing code,&lt;/p&gt;
</description></item></channel></rss>