﻿<?xml version="1.0" encoding="utf-8"?><rss version="2.0"><channel><title>Ayende @ Rahien</title><link>http://ayende.com</link><description>Ayende @ Rahien</description><copyright>Copyright (C) Ayende Rahien  2004 - 2021 (c) 2026</copyright><ttl>60</ttl><item><title>norbi commented on Making code easier to read</title><description>Sorry for confusion, it's to late :/, execution is the same of course with expressions/operators. 
  
  
So still Ayende is it by mistake this order?   
  
  
regards!
</description><link>http://ayende.com/3697/making-code-easier-to-read#comment23</link><guid>http://ayende.com/3697/making-code-easier-to-read#comment23</guid><pubDate>Thu, 25 Dec 2008 23:42:45 GMT</pubDate></item><item><title>norbi commented on Making code easier to read</title><description>Hi*
  
Apart from readness, which for me second options creates more confusion, from my simple test it seems that these codes are not executing the same. 
  
First  is always evaluating the first statement, which is checking collection and that equality with parent.type is first true.
  
  
Second on the other hand applies filters as they are (hmm, it would be strange to apply pipeline in reverse order), and this is valid but with query expresions not operators. So if you would like to achive reverse execution you should write like this
  
  
var items = from x in AllItems 
  
where x.type == parent.type
  
where parent.Items.Constains(x) == false
  
select x;
  
  
Regards!
</description><link>http://ayende.com/3697/making-code-easier-to-read#comment22</link><guid>http://ayende.com/3697/making-code-easier-to-read#comment22</guid><pubDate>Thu, 25 Dec 2008 23:29:38 GMT</pubDate></item><item><title>configurator commented on Making code easier to read</title><description>What reverse order are you guys talking about? Could you elaborate a bit?
</description><link>http://ayende.com/3697/making-code-easier-to-read#comment21</link><guid>http://ayende.com/3697/making-code-easier-to-read#comment21</guid><pubDate>Sun, 16 Nov 2008 17:01:26 GMT</pubDate></item><item><title>PandaWood commented on Making code easier to read</title><description>@Ben Hall - I think the point you're missing is that the Ayende's version has less to read, less to think about.
  
  
I could understand it *and* re-read later, just as fast. The alternative you provided, always requires more time parsing in my brain.
  
  
I get a bit worried when people don't see this. I do believe that your brain would read it quicker as well. 
  
  
I am starting to think that the only people who prefer that type of unnecessarily verbose sort of statement, these days, are those whose are allowing aspects of their personality influence what should be a more scientific choice.
  
  
Assuming we're all in the business of programming for "business" Ayende's choice has got to be the right one. 
  
  
If you're programming something lower level, maybe I'll bend a little.
</description><link>http://ayende.com/3697/making-code-easier-to-read#comment20</link><guid>http://ayende.com/3697/making-code-easier-to-read#comment20</guid><pubDate>Sat, 15 Nov 2008 03:24:37 GMT</pubDate></item><item><title>Ray Booysen commented on Making code easier to read</title><description>And of course, this was done with LinqPad. ;)
</description><link>http://ayende.com/3697/making-code-easier-to-read#comment19</link><guid>http://ayende.com/3697/making-code-easier-to-read#comment19</guid><pubDate>Fri, 14 Nov 2008 16:55:54 GMT</pubDate></item><item><title>Ray Booysen commented on Making code easier to read</title><description>Based on the code below, you can see the where conditions are not done in reverse order:  
  
  
Random r = new Random();
  
List
&lt;double values = new List
&lt;double();
  
for    (int i =0; i &lt; 10000; i++)
  
{
  
    values.Add(r.NextDouble());
  
}
  
  
var result = values
  
    .Where(d =&gt; { "Inner".Dump(); return true; })
  
    .Where(d =&gt; { "Outer".Dump(); return true; });
  
  
  
result.Dump();
  
&gt;</description><link>http://ayende.com/3697/making-code-easier-to-read#comment18</link><guid>http://ayende.com/3697/making-code-easier-to-read#comment18</guid><pubDate>Fri, 14 Nov 2008 16:28:15 GMT</pubDate></item><item><title>Peter Morris commented on Making code easier to read</title><description>I do something similar when I perform OCL queries
  
  
Person.allInstances
  
-&gt;select(givenName = 'Peter')
  
-&gt;select(familyName = 'Morris')
  
  
OR conditions need to be in their own select.  I find it easier to read because a condition with lots of &amp; needs to be mentally processed as a single element when reading the source, whereas multiple WHERE or -&gt;SELECT can be read as smaller elements and I find them easier to read.
</description><link>http://ayende.com/3697/making-code-easier-to-read#comment17</link><guid>http://ayende.com/3697/making-code-easier-to-read#comment17</guid><pubDate>Fri, 14 Nov 2008 09:29:03 GMT</pubDate></item><item><title>Ben Hall commented on Making code easier to read</title><description>I don't agree, and I really don't think this adds much terms of readability. 
  
  
Personally, I find the code much more difficult to read due to the fact that we are implying the operator being used .  It also means that if we want to use a mixture of &amp;&amp; and || in the code, we will have two different approaches or having to flip conditions which sound people could easily misread. 
  
  
Given the two following examples, I would always bit the latter.
  
  
var items = AllItems
  
.Where(x=&gt; x.Type == parent.Type)
  
.Where(x=&gt; !parent.Items.Contains(x));
  
  
var items = AllItems
  
.Where(x=&gt; x.Type == parent.Type &amp;&amp; !parent.Items.Contains(x));
  
  
</description><link>http://ayende.com/3697/making-code-easier-to-read#comment16</link><guid>http://ayende.com/3697/making-code-easier-to-read#comment16</guid><pubDate>Fri, 14 Nov 2008 03:01:10 GMT</pubDate></item><item><title>Bunter commented on Making code easier to read</title><description>For me, first version is easier. I like the sound of "and" the &amp;&amp; makes in my brain. But I would use in this case
  
  
.Where ( x =&gt; ( x.Type == parent.Type &amp;&amp; !parent.Items.Contains(x)) )
  
  
I'm master of unnecessary parentheses.
  
  
</description><link>http://ayende.com/3697/making-code-easier-to-read#comment15</link><guid>http://ayende.com/3697/making-code-easier-to-read#comment15</guid><pubDate>Fri, 14 Nov 2008 02:46:09 GMT</pubDate></item><item><title>Benny Thomas commented on Making code easier to read</title><description>I like your thinking, Ayende!
  
  
Maybe you should make your own extension called And, that calls where, then it looks even better.
</description><link>http://ayende.com/3697/making-code-easier-to-read#comment14</link><guid>http://ayende.com/3697/making-code-easier-to-read#comment14</guid><pubDate>Thu, 13 Nov 2008 23:41:58 GMT</pubDate></item><item><title>Rik Hemsley commented on Making code easier to read</title><description>Gah it screwed up the formatting... of course.
</description><link>http://ayende.com/3697/making-code-easier-to-read#comment13</link><guid>http://ayende.com/3697/making-code-easier-to-read#comment13</guid><pubDate>Thu, 13 Nov 2008 22:40:31 GMT</pubDate></item><item><title>Rik Hemsley commented on Making code easier to read</title><description>I would probably format this as:
  
  
var items = AllItems.Where
  
  (
  
    x=&gt;
  
      x.Type == parent.Type
  
      &amp;&amp;
  
      ! parent.Items.Contains(x)
  
  );
  
  
Yes, I use a lot of vertical space, probably too much...
  
  
BTW I've said it before, but SubText doesn't seem very good for a developer's blog where code is posted.
  
  
  
  
</description><link>http://ayende.com/3697/making-code-easier-to-read#comment12</link><guid>http://ayende.com/3697/making-code-easier-to-read#comment12</guid><pubDate>Thu, 13 Nov 2008 22:39:50 GMT</pubDate></item><item><title>Duarte Nunes commented on Making code easier to read</title><description>@Justin Rudd
  
The calls are, indeed, applied in reverse order. But that means Where(x.Name != null)'s MoveNext() will call, before running its own code, Where(x.Name.Length == 10))'s MoveNext(), which could then throw a NullReferenceException. So, the code executes in the correct order (otherwise, as seen in your example, it would be really counterintuitive).
  
  
The only difference between Ayende's listings is that the latter, while much more readable, incurs one extra method call.    
  
  
@Ricky
  
I'm guessing, readability.
</description><link>http://ayende.com/3697/making-code-easier-to-read#comment11</link><guid>http://ayende.com/3697/making-code-easier-to-read#comment11</guid><pubDate>Thu, 13 Nov 2008 20:23:34 GMT</pubDate></item><item><title>Ricky commented on Making code easier to read</title><description>oops forgot last parens :)
  
  
var irems = AllItems
  
	.Where(x=&gt; x.Type == parent.Type)
  
	.Where(x=&gt; !parent.Items.Contains(x));
  
</description><link>http://ayende.com/3697/making-code-easier-to-read#comment10</link><guid>http://ayende.com/3697/making-code-easier-to-read#comment10</guid><pubDate>Thu, 13 Nov 2008 20:21:34 GMT</pubDate></item><item><title>Ricky commented on Making code easier to read</title><description>Why not
  
  
var irems = AllItems
  
	.Where(x=&gt; x.Type == parent.Type)
  
	.Where(x=&gt; !parent.Items.Contains(x);
  
</description><link>http://ayende.com/3697/making-code-easier-to-read#comment9</link><guid>http://ayende.com/3697/making-code-easier-to-read#comment9</guid><pubDate>Thu, 13 Nov 2008 20:21:06 GMT</pubDate></item><item><title>Justin Rudd commented on Making code easier to read</title><description>It's cleaner, but there is a subtle issue.  You have to remember that Where calls are applied in reverse order (last to first).
  
  
In the former code containment in "Items" is checked before the "Type" check.  In the latter code, the checks are the same order (Where is applied from last to first), but it reads differently.  In this particular case, it may not matter, but in something like this...
  
  
var nonNullNameOfLength10 = AllItems.Where(x.Name != null &amp;&amp; x.Name.Length == 10);
  
  
It reads well...oddly (IMO).
  
  
AllItems.Where(x.Name.Length == 10).Where(x.Name != null);
  
  
I've seen people stack the Where calls in the same order as the conditions.  And that causes the code to break (you could be checking the length of a null name).
</description><link>http://ayende.com/3697/making-code-easier-to-read#comment8</link><guid>http://ayende.com/3697/making-code-easier-to-read#comment8</guid><pubDate>Thu, 13 Nov 2008 19:42:36 GMT</pubDate></item><item><title>dru commented on Making code easier to read</title><description>+1 Ayende
</description><link>http://ayende.com/3697/making-code-easier-to-read#comment7</link><guid>http://ayende.com/3697/making-code-easier-to-read#comment7</guid><pubDate>Thu, 13 Nov 2008 19:15:19 GMT</pubDate></item><item><title>Ayende Rahien commented on Making code easier to read</title><description>No, it isn't.
  
Evaluation it lazy, ater all.
</description><link>http://ayende.com/3697/making-code-easier-to-read#comment6</link><guid>http://ayende.com/3697/making-code-easier-to-read#comment6</guid><pubDate>Thu, 13 Nov 2008 18:55:14 GMT</pubDate></item><item><title>Eric Hauser commented on Making code easier to read</title><description>The second one may be easier to read, but isn't it iterating over the collection twice?
</description><link>http://ayende.com/3697/making-code-easier-to-read#comment5</link><guid>http://ayende.com/3697/making-code-easier-to-read#comment5</guid><pubDate>Thu, 13 Nov 2008 18:50:20 GMT</pubDate></item><item><title>Ayende Rahien commented on Making code easier to read</title><description>No
</description><link>http://ayende.com/3697/making-code-easier-to-read#comment4</link><guid>http://ayende.com/3697/making-code-easier-to-read#comment4</guid><pubDate>Thu, 13 Nov 2008 18:28:38 GMT</pubDate></item><item><title>Peter Ritchie commented on Making code easier to read</title><description>So, the And is implied...  If you had to support both AND and OR conditions would you still do it that way?
</description><link>http://ayende.com/3697/making-code-easier-to-read#comment3</link><guid>http://ayende.com/3697/making-code-easier-to-read#comment3</guid><pubDate>Thu, 13 Nov 2008 18:28:04 GMT</pubDate></item><item><title>Ayende Rahien commented on Making code easier to read</title><description>It wouldn't
</description><link>http://ayende.com/3697/making-code-easier-to-read#comment2</link><guid>http://ayende.com/3697/making-code-easier-to-read#comment2</guid><pubDate>Thu, 13 Nov 2008 18:26:34 GMT</pubDate></item><item><title>Peter Ritchie commented on Making code easier to read</title><description>How would you differentate between or conditions and and conditions?
</description><link>http://ayende.com/3697/making-code-easier-to-read#comment1</link><guid>http://ayende.com/3697/making-code-easier-to-read#comment1</guid><pubDate>Thu, 13 Nov 2008 18:23:36 GMT</pubDate></item></channel></rss>