<?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>Style follows semantics</title><link>http://blogs.msdn.com/b/ericlippert/archive/2010/02/01/style-follows-semantics.aspx</link><description>Which is better style? 
 bool abc; if (Foo()) abc = Bar(); else abc = false; 
 vs 
 bool abc = Foo() &amp;amp;&amp;amp; Bar(); 
 ? 
 To me, this comes down to the question “is Bar useful solely for obtaining its value, or also for its side effects?” The</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>re: Style follows semantics</title><link>http://blogs.msdn.com/b/ericlippert/archive/2010/02/01/style-follows-semantics.aspx#10085444</link><pubDate>Wed, 03 Nov 2010 14:22:27 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10085444</guid><dc:creator>Not Oliver</dc:creator><description>&lt;p&gt;Oliver: &amp;quot;I used to think &amp;quot;shorter is always better&amp;quot;, but using breakpoints in VS made me change my mind.&amp;quot;&lt;/p&gt;
&lt;p&gt;I still think that in general &amp;quot;shorter is always better&amp;quot; (at least for simplicity in terms of semantic elements, not characters). &amp;nbsp;Using breakpoints in VS made me convinced that VS doesn&amp;#39;t do breakpoints very well. &amp;nbsp;:-)&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10085444" width="1" height="1"&gt;</description></item><item><title>re: Style follows semantics</title><link>http://blogs.msdn.com/b/ericlippert/archive/2010/02/01/style-follows-semantics.aspx#9991428</link><pubDate>Tue, 06 Apr 2010 22:35:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9991428</guid><dc:creator>Pete Wilson</dc:creator><description>&lt;p&gt;Personally I don't like seeing tests or assignments of literal true/false values when it can be avoided.&lt;/p&gt;
&lt;p&gt;I'd probably do&lt;/p&gt;
&lt;p&gt;bool success = NetworkAvailable();&lt;/p&gt;
&lt;p&gt;if (success)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; success = LogUserOn();&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9991428" width="1" height="1"&gt;</description></item><item><title>re: Style follows semantics</title><link>http://blogs.msdn.com/b/ericlippert/archive/2010/02/01/style-follows-semantics.aspx#9974672</link><pubDate>Mon, 08 Mar 2010 09:41:49 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9974672</guid><dc:creator>Bent Rasmussen</dc:creator><description>&lt;p&gt;I observed a colleague doing a side-effecting expression a week or so ago and tried to explain the command-query separation principle and how he could make more explicit that there was a side-effect by not chaining side-effecting calls together in an expression as if the expression really was pure. It was a pretty tough sell...&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9974672" width="1" height="1"&gt;</description></item><item><title>re: Style follows semantics</title><link>http://blogs.msdn.com/b/ericlippert/archive/2010/02/01/style-follows-semantics.aspx#9965270</link><pubDate>Wed, 17 Feb 2010 19:34:27 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9965270</guid><dc:creator>RobS</dc:creator><description>&lt;p&gt;C# is a high-level language and should be about readability rather than brevity.&lt;/p&gt;
&lt;p&gt;Conversely, being overly verbose is just as bad a writing cryptic code.&lt;/p&gt;
&lt;p&gt;In this case, virtually nothing is gained by combining these lines and you lose a little bit of readability.&lt;/p&gt;
&lt;p&gt;In these cases, I would always keep them as separate cases or, as mentioned, assign the results to separate variables then compare in an IF statement&lt;/p&gt;
&lt;p&gt;BTW I like the variable names in helping to make self-documenting code. &amp;nbsp;When lines get too cryptic, you tend to lose that too.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9965270" width="1" height="1"&gt;</description></item><item><title>re: Style follows semantics</title><link>http://blogs.msdn.com/b/ericlippert/archive/2010/02/01/style-follows-semantics.aspx#9964533</link><pubDate>Tue, 16 Feb 2010 19:02:04 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9964533</guid><dc:creator>Allon Guralnek</dc:creator><description>&lt;p&gt;Kris really nailed it in my opinion, but I don't agree that both approaches make the original question pointless, rather that they make the LogUserOn example pointless.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9964533" width="1" height="1"&gt;</description></item><item><title>re: Style follows semantics</title><link>http://blogs.msdn.com/b/ericlippert/archive/2010/02/01/style-follows-semantics.aspx#9964144</link><pubDate>Tue, 16 Feb 2010 09:43:04 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9964144</guid><dc:creator>rousso</dc:creator><description>&lt;p&gt;I always prefer the shortest way to express something in code. So I tend to prefer skipping the control flow statements if I can do the exact same thing with a direct assignment. &lt;/p&gt;
&lt;p&gt;Nevertheless I see other semantic issues in the examples you are presenting here, which would make the argument quite different had they been addressed:&lt;/p&gt;
&lt;p&gt;I would implement NetworkAvailable() method most likely as a NetworkAvailable property (because availability is a property of a network that indicates its status). I also would test the property inside LogOnUser() method which would return false if network was unavailable or even better it should throw an exception (because lack of network availability is an exceptional situation).&lt;/p&gt;
&lt;p&gt;To be more precise if I needed to express these things in code I would structure the whole thing to end up looking like:&lt;/p&gt;
&lt;p&gt;if (!Network.IsAvailable)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; throw new Network.UnavailableException();&lt;/p&gt;
&lt;p&gt;return User.Logon();&lt;/p&gt;
&lt;p&gt;or (if i didn't want to throw an exception)&lt;/p&gt;
&lt;p&gt;return Network.IsAvailable ? User.TryLogOn() : false;&lt;/p&gt;
&lt;p&gt;which is shorter than the control flow statement but still expresses the notion that attempting a login while network is unavailable is pointless. Also naming the LogOn method as TryLogOn() denotes that the method is not going to throw an exception.&lt;/p&gt;
&lt;p&gt;So bottom line i would go for the shortest expressive statement foobar = foo() &amp;amp;&amp;amp; bar(); but I would also take under consideration the semantics of foo() and bar() and and go for the shortest &amp;amp;&amp;amp; least cryptic code by utilizing programming constructs that suite best the nature of the expressed elements.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9964144" width="1" height="1"&gt;</description></item><item><title>re: Style follows semantics</title><link>http://blogs.msdn.com/b/ericlippert/archive/2010/02/01/style-follows-semantics.aspx#9958187</link><pubDate>Thu, 04 Feb 2010 14:23:53 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9958187</guid><dc:creator>Filini</dc:creator><description>&lt;p&gt;I tend to agree with the idea that communicating the semantics of the program fragment is important, and trying to &amp;quot;compress&amp;quot; 2 lines into 1, just because you prefer that style, risks to hide the flow you are writing.&lt;/p&gt;
&lt;p&gt;If we talk about the &amp;quot;real-word&amp;quot; scenario of login, then I suppose the requirements of that code might be something like: &amp;quot;create a method which logs a user in the system, if the network is available, and returns the status of the login&amp;quot;. &lt;/p&gt;
&lt;p&gt;What if some other action needs to be taken, when the connection is not available?&lt;/p&gt;
&lt;p&gt;Let's change the requirements to: &amp;quot;create a method which logs a user in the system, if the network is available. If the network is not available, check your client for a cached profile, to work off-line. the method returns the status of the login (real or cached)&amp;quot;.&lt;/p&gt;
&lt;p&gt;Then our code would become:&lt;/p&gt;
&lt;p&gt;bool result;&lt;/p&gt;
&lt;p&gt;if( NetworkAvailable() )&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; result = LogInUser();&lt;/p&gt;
&lt;p&gt;else&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; result = ExistsCachedProfile();&lt;/p&gt;
&lt;p&gt;How would you change it?&lt;/p&gt;
&lt;p&gt;bool result = ( NetworkAvailable() ) ? LogInUser() : ExistsCachedProfile() ;&lt;/p&gt;
&lt;p&gt;or&lt;/p&gt;
&lt;p&gt;bool result = ( NetworkAvailable() &amp;amp;&amp;amp; LogInUser() ) || ExistsCachedProfile() ;&lt;/p&gt;
&lt;p&gt;For me, the second &amp;quot;style&amp;quot; is not so nice/readable anymore.&lt;/p&gt;
&lt;p&gt;It all depends on what your methods do (check a value / modify the system), as Eric states in the first paragraph. The two fragments:&lt;/p&gt;
&lt;p&gt;bool result = NetworkAvailable() &amp;amp;&amp;amp; LogInUser();&lt;/p&gt;
&lt;p&gt;bool result = NetworkAvailable() &amp;amp;&amp;amp; IsLoggedUser();&lt;/p&gt;
&lt;p&gt;are the same code fragment, but they clearly perform different algorithms (the first one modifies the system). This might become misleading, for the code maintenance.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9958187" width="1" height="1"&gt;</description></item><item><title>re: Style follows semantics</title><link>http://blogs.msdn.com/b/ericlippert/archive/2010/02/01/style-follows-semantics.aspx#9957494</link><pubDate>Wed, 03 Feb 2010 11:33:16 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9957494</guid><dc:creator>Kris Vandermotten</dc:creator><description>&lt;p&gt;I mostly agree with James Hart. A method should do what its name implies, and return its result (if any). If it can't do what its name implies, it should report it as an exception.&lt;/p&gt;
&lt;p&gt;So that leaves two options (for this example):&lt;/p&gt;
&lt;p&gt;1) LonOnUser(...) always returns an IPrincipal. You can call .Identy.IsAuthenticated to test whether the principal was authenticated.&lt;/p&gt;
&lt;p&gt;2) LogOnUser() returns an IPrincipal where .Identity.IsAuthenticated is always thrue, and throws if it can't do that.&lt;/p&gt;
&lt;p&gt;Note that the method would need a rename in both cases.&lt;/p&gt;
&lt;p&gt;Both approaches make the original question pointless.&lt;/p&gt;
&lt;p&gt;Summarry: a method that returns a boolean is a function meant to compute the boolean. A method meant to do something else (&amp;quot;called for its side effect&amp;quot;) should not return a boolean to indicate success or failure.&lt;/p&gt;
&lt;p&gt;(The only exception to this is bool methods with out parameters named TryX, such as TryParse methods.) Consequently, if functions return boolean, they can be combined with the &amp;amp;&amp;amp; operator with no maintainability problem at all.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9957494" width="1" height="1"&gt;</description></item><item><title>re: Style follows semantics</title><link>http://blogs.msdn.com/b/ericlippert/archive/2010/02/01/style-follows-semantics.aspx#9957410</link><pubDate>Wed, 03 Feb 2010 07:10:20 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9957410</guid><dc:creator>Wes</dc:creator><description>&lt;p&gt;Upon reading this I immediately searched for CQS in the comments and am glad to see it show up. &amp;nbsp;Seeing as the state of being &amp;quot;logged in&amp;quot; to a system is something that can change and is what we are really modeling here, it totally makes sense to call a logon procedure and then check if the login was sucessful via a query routine. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;CQS leads to reuse as I imagine we wouldn't want to attempt logging in a second time just to check if the user is authenticated somewhere else. &amp;nbsp;Also, I think it would be beneficial to know if a user is no longer logged on without logging them in, so we would have to add another query routine anyways, so we might as well make LogOn void to avoid duplication.&lt;/p&gt;
&lt;p&gt;I guess what we come back to the semantics of separating commands from queries, in which case the separated semantics lead to a more _readable_ style(syntax) where Foo() &amp;amp;&amp;amp; Bar() can always be compacted to one line, thanks to CQS.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9957410" width="1" height="1"&gt;</description></item><item><title>re: Style follows semantics</title><link>http://blogs.msdn.com/b/ericlippert/archive/2010/02/01/style-follows-semantics.aspx#9957395</link><pubDate>Wed, 03 Feb 2010 06:04:19 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9957395</guid><dc:creator>nog</dc:creator><description>&lt;p&gt;In terms of code readability a method named LogUserOn() shouldn't return a bool. &amp;nbsp;&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9957395" width="1" height="1"&gt;</description></item></channel></rss>