<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>NewItUp()</title>
        <link>http://newitup.com/Default.aspx</link>
        <description>Another Alt.Net blog - Dan Donahue</description>
        <language>en-US</language>
        <copyright>Dan Donahue</copyright>
        <managingEditor>dan.donahue@comcast.net</managingEditor>
        <generator>Subtext Version 1.9.5.176</generator>
        <image>
            <title>NewItUp()</title>
            <url>http://newitup.com/images/RSS2Image.gif</url>
            <link>http://newitup.com/Default.aspx</link>
            <width>77</width>
            <height>60</height>
        </image>
        <item>
            <title>Book Review:  Code Complete</title>
            <category>book-review</category>
            <link>http://newitup.com/archive/2010/05/12/book-review--code-complete.aspx</link>
            <description>&lt;p&gt;I've been trying to be constantly reading a book for about the past year and a half now.  And in addition, I started a dev book group where I work, where we pick a book and read it a certain amount each week and then meet for an hour to discuss that section - whether it be making decisions on whether this is something we want to do as a department, or how we can apply the topic to our projects, etc.&lt;/p&gt;
&lt;p&gt;The latest book we just finished was &lt;span style="font-weight: bold;"&gt;Code Complete&lt;/span&gt;.  I feel like I was long overdue reading this one as I've heard a few times in the past that this was one of those required reading books on programming.  Actually, on more than one occasion, I've been asking during a job interview what books I've read and liked the most and after giving my list, the interviewer has said "all great books... the only one I'd add is Code Complete".  So I was pretty excited to finally read through this one.&lt;/p&gt;
&lt;p&gt;I've broken up my review into a section of positives, negatives and then a summary.  I find that helpful because sometimes reviews get too verbose and will be filled with double speak such as "the paragraph on *whatever* was great, except ..." .  So I'm going to try to avoid that and just talk about what I liked, what I didn't.&lt;/p&gt;
&lt;p&gt;&lt;span style="font-weight: bold;"&gt;POSITIVE:&lt;/span&gt;&lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;I can't stress enough how much good information  this book is packed with.  There's a ton of information to digest about a  wide range of topics - from how to organize and comment your code to team dynamics, estimation tipis and even programmer human characteristics - this book does a great job of taking a good  1000ft.view of a lot of aspects of software development and also gives a  lot of "read this next" resources for topics that you might want to  delve deeper into.  McConnell (the author) does a pretty good of using  case studies to back up his points.  And while some of the information  is a little out of date in the book (happens often in these types of  books... the software industry just moves too fast), a lot of the  information is plenty applicable no matter what language you develop in,  type of development you do, etc.&lt;/p&gt;
&lt;span style="font-weight: bold;"&gt;NEGATIVE:&lt;/span&gt;&lt;br /&gt;
&lt;p&gt;First off - this book is long... REALLY long.  We actually  skipped a few chapters early on during the book group and it still felt like the book would  never end.  That's a bit nitpicky though as the Lord Of The Rings  trilogy is long as hell, but it's a great read &lt;img class="jive_macro&amp;#xD;&amp;#xA;jive_emote" jivemacro="emoticon" ___jive_emoticon_name="happy" src="http://tspace.totalattorneys.com/4.0.4/images/emoticons/happy.gif" mce_src="/4.0.4/images/emoticons/happy.gif" alt="" /&gt;  I think the biggest disappointment to me was that a lot of  the information put forth was stuff that you learn your first few years  in professional software development and it just becomes common sense.   More often than not, I found myself (and I believe some of the others  in the group) saying "these are great points - but I already knew this"  or "these are great points - but this is common sense".  And then, of  course, the problem with a 1000ft. view that I mentioned earlier is that  if you find yourself interested in one of the ideas that he presents,  you would love to learn more, but it's just not there.  That's not a  problem with this book in particular... but I guess my preference is  with more directed learning books.  Books that take a smaller array of topics  and really delve into them in greater detail.&lt;/p&gt;
&lt;p style="font-weight: bold;"&gt;SUMMARY:&lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;&lt;span style="font-weight: bold;" /&gt;I'm  not sure I'd ever read this one again.  Like I said, it just contains  too much information that I already knew.. and I don't even pretend to be a software guru by any stretch of the imagination.&lt;/p&gt;
However, this  book is far from worthless.  I think this book should be required  reading in a college course on programming, or the first book a young  developer reads before ever working on their first project.  This is  type of stuff that you can either learn organically by being in the  industry for a while and seeing it all come to life in both good and bad  ways in your projects, or you can just read this book and enter the  industry with this knowledge in hand.  It'll put you on level playing  ground with people who take the long path.
&lt;p&gt;I also think  this is one of those books that would be a checkmark in the thumbs up  column if a prospective new employee had mentioned reading it.  It would  be a sign that the developer was equipped with knowledge of good  programming practices and ideas (of course - whether they apply them is a  different story, but you'd rarely get that out of an interview anyway).&lt;/p&gt;
&lt;p&gt;Bottom  line... new developers should be forced to read this book.  Older  developers probably already know most of what it contains.  But it was nice to get some of this knowledge reiterated (much like how I re-read The Pragmatic Programmer every year or two just to get rejuvenated) and also have the case studies to back up the information with statistics.  It's one thing to do something a certain way because it works for you, and even better when it turns out that it works for the entire industry :)&lt;/p&gt;&lt;img src="http://newitup.com/aggbug/127.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Dan Donahue</dc:creator>
            <guid>http://newitup.com/archive/2010/05/12/book-review--code-complete.aspx</guid>
            <pubDate>Wed, 12 May 2010 14:15:28 GMT</pubDate>
            <wfw:comment>http://newitup.com/comments/127.aspx</wfw:comment>
            <comments>http://newitup.com/archive/2010/05/12/book-review--code-complete.aspx#feedback</comments>
            <wfw:commentRss>http://newitup.com/comments/commentRss/127.aspx</wfw:commentRss>
            <trackback:ping>http://newitup.com/services/trackbacks/127.aspx</trackback:ping>
        </item>
        <item>
            <title>Linq To Sql Web App Lifetime Management - Take 1</title>
            <category>.net</category>
            <category>linq</category>
            <link>http://newitup.com/archive/2009/12/02/linq-to-sql-web-app-lifetime-management---take-1.aspx</link>
            <description>&lt;style type="text/css"&gt;&lt;![CDATA[csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: Consolas, &amp;quot;Courier New&amp;quot;, Courier, Monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ }  .csharpcode pre { margin: 0em; }  .csharpcode .rem { color: #008000; }  .csharpcode .kwrd { color: #0000ff; }  .csharpcode .str { color: #006080; }  .csharpcode .op { color: #0000c0; }  .csharpcode .preproc { color: #cc6633; }  .csharpcode .asp { background-color: #ffff00; }  .csharpcode .html { color: #800000; }  .csharpcode .attr { color: #ff0000; }  .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; }  .csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt; I was asked to help another project team at work to come up with a good way to manage Linq To Sql data contexts on their project.  Their current situation is that they open and use a datacontext wherever needed, but often run into problems where they're trying to save entities with expired contexts or opening too many connections to the database to grab the same record multiple times.  They're not fully utilizing the datacontext's features such as the &lt;a href="javascript:void(0);/*1259763267838*/"&gt;Identity Map&lt;/a&gt; or &lt;a href="javascript:void(0);/*1259763303898*/"&gt;Unit Of Work&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
I haven't had much experience with Linq prior to being asked to help, but I knew the basics.  For the rest, I decided to do a bunch of blog reading on the subject to help out with my implementation.  My criteria for a good solution was the following:&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;Ease Of Use / Clean Syntax&lt;/span&gt;&lt;br /&gt;
I figured this one was self-explanatory.  First, I didn't want to get into a huge architecture if I didn't have to and furthermore, I figured that if it wasn't easy to implement and use, no one would want to do the work to implement it.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;Unit Testable:&lt;/span&gt;&lt;br /&gt;
I like to do unit testing and wanted to be able to write tests that didn't actually hit the database.  I wanted to be able to write mock versions of the implementation instead for tests so I could control them during my tests.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;Unit Of Work/Lifetime Management:&lt;/span&gt;&lt;br /&gt;
This was to solve the problem that they came to with me in the first place.  Microsoft, as well as common sense, indicate that you should really only have one datacontext around per request for web applications.  This seems to make sense since the datacontext already is built like a unit of work (it records all changes to entities, inserts and deletes and waits until SubmitChanges() to actually persist them to the database).&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;Validation Of Entities:&lt;/span&gt;&lt;br /&gt;
Another concern the team had was how to validate entities before saving them.  Their current implementation was to create partial classes for each entity and create custom attributes to perform validation.  I did not include my solution as part of the prototype I came up, but I actually suggested they pull the validation out into separate Specification objects - this way, they can keep the entity classes to solely what Microsoft provides them and the Specification objects can be reused, as well as making more explicit the business rules around a valid entity - something I learned from reading Eric Evans' DDD book.&lt;br /&gt;
&lt;br /&gt;
What I came up with was pretty simple. I mostly just took a lot of what other bloggers were suggesting and stripped out a lot of stuff I didn't think was necessary at the moment.  Not that I felt others were over-complicating the matter, but they were showing examples of things they needed for their projects which may not necessarily be needed in this one.  And taking the YAGNI approach, I felt like I should keep it as simple as possible, knowing that I could add in the desired features later if necessary.&lt;br /&gt;
&lt;br /&gt;
So, finally, here is the design I came up with...&lt;br /&gt;
&lt;br /&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; UnitOfWork : IUnitOfWork&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;{&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;     &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;readonly&lt;/span&gt; EntityClassesDataContext context;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;     &lt;span class="kwrd"&gt;public&lt;/span&gt; UnitOfWork()&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;     {&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;          context = &lt;span class="kwrd"&gt;new&lt;/span&gt; EntityClassesDataContext();&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;     }&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;     &lt;span class="kwrd"&gt;public&lt;/span&gt; Table&amp;lt;T&amp;gt; GetTable&amp;lt;T&amp;gt;() &lt;span class="kwrd"&gt;where&lt;/span&gt; T : &lt;span class="kwrd"&gt;class&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;     {&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;          &lt;span class="kwrd"&gt;return&lt;/span&gt; context.GetTable&amp;lt;T&amp;gt;();&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;     }&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt;     &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; SubmitChanges()&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  16:  &lt;/span&gt;     {&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  17:  &lt;/span&gt;          context.SubmitChanges();&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  18:  &lt;/span&gt;     }&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  19:  &lt;/span&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
This UnitOfWork class just wraps the DataContext and implements an IUnitOfWork interface which can then be used in the rest of the system, so that we can mock it during tests.  It just provides the ability to get a table (which can then be queried, or inserted to/deleted from) and run SubmitChanges().  I considered having SubmitChanges() be run automatically at the end of each request, but I felt like that was dangerous as you may not want all changes submitted - I want to make it more explicit and require programmers to call that function when they wanted their changes persisted.&lt;br /&gt;
&lt;br /&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; UnitOfWorkFactory&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;{&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;     &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;const&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; HTTP_CONTEXT_KEY = &lt;span class="str"&gt;"SFT.UnitOfWork.Factory.Context.Key"&lt;/span&gt;;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;     &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; IUnitOfWork GetUnitOfWork()&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;     {&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;          &lt;span class="kwrd"&gt;if&lt;/span&gt; (HttpContext.Current != &lt;span class="kwrd"&gt;null&lt;/span&gt;)&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;          {&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;               &lt;span class="kwrd"&gt;if&lt;/span&gt; (!HttpContext.Current.Items.Contains(HTTP_CONTEXT_KEY))&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;               {&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;                    HttpContext.Current.Items.Add(HTTP_CONTEXT_KEY, Activator.CreateInstance(&lt;span class="kwrd"&gt;typeof&lt;/span&gt;(UnitOfWork)));&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;               }&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;               &lt;span class="kwrd"&gt;return&lt;/span&gt; (IUnitOfWork)HttpContext.Current.Items[HTTP_CONTEXT_KEY];&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt;          }&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt;          &lt;span class="kwrd"&gt;throw&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; Exception(&lt;span class="str"&gt;"No current HttpContext found."&lt;/span&gt;);&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  16:  &lt;/span&gt;     }&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  17:  &lt;/span&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
This factory is used to get the current UnitOfWork which is stored in HttpContext's Items collection.  It's pretty simple.  If you ask for the current UnitOfWork and it hasn't been created for the current request yet, it creates it and puts into the Items collection.  Then, it returns it.  Otherwise, if it already exists, it returns the one that's in the Item's collection already.  From what I read, this should guarantee that you always use the single, per-request DataContext which means you take advantage of the Identity Map (instead of loading the same entity from the db multiple times) and have all your changes in a request tracked.&lt;br /&gt;
&lt;br /&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; DefaultPresenter(IDefaultView view) : &lt;span class="kwrd"&gt;this&lt;/span&gt;(view, UnitOfWorkFactory.GetUnitOfWork(), &lt;span class="kwrd"&gt;new&lt;/span&gt; DebtorRepository())&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;{&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;}&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; DefaultPresenter(IDefaultView view, IUnitOfWork unitOfWork, IDebtorRepository repository)&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;{&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;     &lt;span class="kwrd"&gt;this&lt;/span&gt;.view = view;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;     &lt;span class="kwrd"&gt;this&lt;/span&gt;.unitOfWork = unitOfWork;&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;     &lt;span class="kwrd"&gt;this&lt;/span&gt;.repository = repository;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
And this is how I used the factory in my prototype.  Dependency injection allows for unit testing because in the unit tests, I can use the 2nd constructor and pass in a mock of the IUnitOfWork, but in production, I can use the above constructor which calls on the UnitOfWork factory to get the concrete UnitOfWork class that wraps the Linq datacontext.&lt;br /&gt;
&lt;br /&gt;
I know that IoC containers would get me down to one constructor, but for the purposes of the prototype, I didn't want to do the extra work.  However, itt's probably something I would suggest if this is used in an actual project. &lt;br /&gt;
&lt;br /&gt;
The last thing which I won't post is that I use Repositories for getting data.  I do that cut down on the duplication of queries across the codebase and also to abstract out the Table classes that come with the datacontext since they're not easily mocked for testing.  I read about ways that you could mock them out and provide data from an in-memory data store during testing, but when I'm testing the presenters, I don't want to have to worry about all the overhead of setting that up in my test, so I'd rather just put it behind an IRepository interface.  When it comes time to test the Repository classes, I can focus on how to write the tables with an in-memory data collection.&lt;br /&gt;
&lt;br /&gt;
And that's it.  I would love some feedback as this is my first time taking up something like this.  Is there any obvious, or not-so-obvious flaw in this design that I'm missing?  Are there any ways it could be improved?  Your comments would be greatly appreciated.&lt;img src="http://newitup.com/aggbug/124.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Dan Donahue</dc:creator>
            <guid>http://newitup.com/archive/2009/12/02/linq-to-sql-web-app-lifetime-management---take-1.aspx</guid>
            <pubDate>Wed, 02 Dec 2009 14:45:07 GMT</pubDate>
            <wfw:comment>http://newitup.com/comments/124.aspx</wfw:comment>
            <comments>http://newitup.com/archive/2009/12/02/linq-to-sql-web-app-lifetime-management---take-1.aspx#feedback</comments>
            <wfw:commentRss>http://newitup.com/comments/commentRss/124.aspx</wfw:commentRss>
            <trackback:ping>http://newitup.com/services/trackbacks/124.aspx</trackback:ping>
        </item>
        <item>
            <title>Delegates =&gt; Lambdas, Pt. 4:  .NET Helpers (The Final Episode)</title>
            <category>.net</category>
            <category>delegates</category>
            <category>lambda-expression</category>
            <link>http://newitup.com/archive/2009/08/13/delegates--lambdas-pt.-4--.net-helpers-the-final.aspx</link>
            <description>&lt;style type="text/css"&gt;&lt;![CDATA[
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: Consolas, "Courier New", Courier, Monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #006080; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}

.csharpcode .lnum { color: #606060; }
]]&gt;&lt;/style&gt;
&lt;p&gt;&lt;span style="font-weight: bold;"&gt;&lt;span style="font-style: italic;"&gt;This is a series I created on my company's internal intranet for a few young developers.  However, I thought it might be useful to anyone, so I wanted to make it public.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;     &lt;a href="../../../../archive/2009/08/13/delegates--lambdas-pt.-1-whats-a-delegate.aspx" title="Title of this entry." id="viewpost_ascx_TitleUrl"&gt;Delegates =&amp;gt; Lambdas, Pt. 1: What's A Delegate?&lt;/a&gt;     &lt;/li&gt;
    &lt;li&gt;     &lt;a href="../../../../archive/2009/08/13/delegates--lambdas-pt.-2-anonymous-methods--inline-delegates.aspx" title="Title of this entry." id="viewpost_ascx_TitleUrl"&gt;Delegates =&amp;gt; Lambdas, Pt. 2: Anonymous Methods == Inline Delegates&lt;/a&gt;     &lt;/li&gt;
    &lt;li&gt;     &lt;a href="../../../../archive/2009/08/13/delegates--lambdas-pt.-3-lambda-expressions.aspx" title="Title of this entry." id="viewpost_ascx_TitleUrl"&gt;Delegates =&amp;gt; Lambdas, Pt. 3: Lambda Expressions&lt;/a&gt;     &lt;/li&gt;
    &lt;li&gt;     &lt;a href="../../../../archive/2009/08/13/delegates--lambdas-pt.-4--.net-helpers-the-final.aspx" title="Title of this entry." id="viewpost_ascx_TitleUrl"&gt;Delegates =&amp;gt; Lambdas, Pt. 4:  .NET Helpers (The Final Episode)&lt;/a&gt;     &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;Ok - this is the last part of my series about delegates and lambdas.  Finally.  But I wanted to mention a few helpers that .NET provides to make it easier to use delegates and lambdas in your code.&lt;/p&gt;
&lt;p&gt;If you remember back to the &lt;a href="http://newitup.com/archive/2009/08/13/delegates--lambdas-pt.-1-whats-a-delegate.aspx"&gt;first post&lt;/a&gt; of the series, I mentioned that to declare a delegate to be used as a parameter or anything else, you had to do this:&lt;/p&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;delegate&lt;/span&gt; &lt;span class="kwrd"&gt;bool&lt;/span&gt; Filter(TeamDTO team);&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Well - .NET makes it slightly less tedious with the use of three framework classes, namely Action&amp;lt;T&amp;gt;, Predicate&amp;lt;T&amp;gt; and Func&amp;lt;T&amp;gt;.  Let's look at an example of each.&lt;/p&gt;
&lt;h3&gt;Action&amp;lt;T&amp;gt;: &lt;/h3&gt;
&lt;p&gt;This class encapsulates a delegate that takes one parameter and does not return a value.  Meaning, it is a contract that specifies one input parameter, the type of which is specified as a generic, and a void return type.  This is useful for a variety of scenarios, but let me show you just one.&lt;/p&gt;
&lt;p&gt;If you've noticed, when you create a List&amp;lt;T&amp;gt;, one of the methods on that list that is built-in for .NET is ForEach(Action&lt;t&gt; action).  Using that, you can build for-each loops using delegates/lambdas.  See below:&lt;/t&gt;&lt;/p&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;readonly&lt;/span&gt; List&amp;lt;TeamDTO&amp;gt; allTeams = &lt;span class="kwrd"&gt;new&lt;/span&gt; List&amp;lt;TeamDTO&amp;gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;                                          {&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;                                              &lt;span class="kwrd"&gt;new&lt;/span&gt; TeamDTO(&lt;span class="str"&gt;"Flyers"&lt;/span&gt;, &lt;span class="str"&gt;"East"&lt;/span&gt;),&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;                                              &lt;span class="kwrd"&gt;new&lt;/span&gt; TeamDTO(&lt;span class="str"&gt;"Blackhawks"&lt;/span&gt;, &lt;span class="str"&gt;"West"&lt;/span&gt;),&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;                                              &lt;span class="kwrd"&gt;new&lt;/span&gt; TeamDTO(&lt;span class="str"&gt;"Blue Jackets"&lt;/span&gt;, &lt;span class="str"&gt;"West"&lt;/span&gt;),&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;                                              &lt;span class="kwrd"&gt;new&lt;/span&gt; TeamDTO(&lt;span class="str"&gt;"Capitals"&lt;/span&gt;, &lt;span class="str"&gt;"East"&lt;/span&gt;)&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;                                          };&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;Action&amp;lt;TeamDTO&amp;gt; printTeam = x =&amp;gt; Response.Write(x.TeamName);&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;allTeams.ForEach(printTeam);&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Or, you can just inline it all with a lambda expression like this:&lt;/p&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;allTeams.ForEach(x =&amp;gt; Response.Write(x.TeamName));&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;By the way, &lt;strong&gt;Action&amp;lt;T&amp;gt;&lt;/strong&gt; also comes in a few additional flavors, those being &lt;strong&gt;Action&amp;lt;T1, T2&amp;gt;&lt;/strong&gt;, &lt;strong&gt;Action&amp;lt;T1, T2, T3&amp;gt;&lt;/strong&gt; and &lt;strong&gt;Action&amp;lt;T1, T2, T3, T4&amp;gt;&lt;/strong&gt;.  As you can probably guess, this just allows you to use them to define up to 4 input parameter types - the return value in all situations is still void.&lt;/p&gt;
&lt;h3&gt;Predicate&amp;lt;T&amp;gt;:&lt;/h3&gt;
&lt;p&gt;This is another .NET class representing another standard delegate situation.  This one takes one parameter and returns a boolean value.  This is actually PERFECT for our filter situation.  Instead of having to create a delegate like the one at the top of this post, we can use this in it's place, since it does the same thing (takes in one argument, it's generic, but we can specify we want it to be of type TeamDTO and returns a boolean value).  So let's see it in action:&lt;/p&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; IList&amp;lt;TeamDTO&amp;gt; SelectTeamsByFilter(IEnumerable&amp;lt;TeamDTO&amp;gt; teams, Predicate&amp;lt;TeamDTO&amp;gt; filter)&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;{&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;     var filteredTeams = &lt;span class="kwrd"&gt;new&lt;/span&gt; List&amp;lt;TeamDTO&amp;gt;();&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;     &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (TeamDTO team &lt;span class="kwrd"&gt;in&lt;/span&gt; teams)&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;     {&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;          &lt;span class="kwrd"&gt;if&lt;/span&gt; (filter(team))&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;          {&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;               filteredTeams.Add(team);&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;          }&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;     }&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;     &lt;span class="kwrd"&gt;return&lt;/span&gt; filteredTeams;&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Compare this piece of code to the one in the first post of the series and you see that I just replaced the delegate object I created with the Predicate - why re-invent what .NET already has?&lt;/p&gt;
&lt;h3&gt;Func&amp;lt;T&amp;gt;:&lt;/h3&gt;
&lt;p&gt;This one actually comes in a bunch of different flavors and is arguably the most powerful.  And actually, if you look at the MSDN documentation, &lt;strong&gt;Func&amp;lt;T&amp;gt; &lt;/strong&gt;is actually not even the correct form of the delegate - but this is how it's used in conversation.  The actual name is &lt;strong&gt;Func&amp;lt;TResult&amp;gt;&lt;/strong&gt;.  And hopefully by now you're catching on and can guess what that means.  &lt;strong&gt;Func&amp;lt;TResult&amp;gt; &lt;/strong&gt;allows you to specify a delegate with no input parameters and a return value of &lt;strong&gt;TResult.&lt;/strong&gt;  Like &lt;strong&gt;Action&amp;lt;T&amp;gt;&lt;/strong&gt;, &lt;strong&gt;Func&amp;lt;TResult&amp;gt; &lt;/strong&gt;has variations that allow you to specify up to 4 input parameters, in addition to the return type.  So you have &lt;strong&gt;Func&amp;lt;TResult&amp;gt;, Func&amp;lt;T, TResult&amp;gt;, Func&amp;lt;T1, T2, TResult&amp;gt;, Func&amp;lt;T1, T2, T3, TResult&amp;gt; &lt;/strong&gt;and &lt;strong&gt;Func&amp;lt;T1, T2, T3, T4, TResult&amp;gt;.   &lt;/strong&gt;Keep in mind if you use these that the last value will be the return value in any situation.&lt;/p&gt;
&lt;p&gt;As a really simple example, imagine I wanted a function that printed all of the team names in a list.  Here it is:&lt;/p&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;Func&amp;lt;TeamDTO, &lt;span class="kwrd"&gt;string&lt;/span&gt;&amp;gt; printTeamName = x =&amp;gt; x.TeamName;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;&lt;span class="kwrd"&gt;foreach&lt;/span&gt; (TeamDTO team &lt;span class="kwrd"&gt;in&lt;/span&gt; allTeams)&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;{&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;     printTeamName(team);&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;The first line defines a &lt;strong&gt;Func&amp;lt;T, TResult&amp;gt; &lt;/strong&gt;that accepts a TeamDTO object and returns a string.  Then the lambda shows that &lt;em&gt;x&lt;/em&gt; is the input parameter and on the other side of the =&amp;gt; is the return statement &lt;em&gt;x.TeamName&lt;/em&gt;.  The other part is just a for-loop which invokes the delegate.&lt;/p&gt;
&lt;p&gt;So that's it for my series on delegates and lambda expressions.  But it won't be the last time you hear about them.  A lot of the examples in these posts were scholastic in nature, but they probably left you thinking that aside from being neat, where was the benefit?  Where all of these helpers, and delegates in general, become really powerful is when you combine them with other features of .NET.  So expect to see more of these in future posts.  But hopefully these gave you a few ideas about where you could benefit from the use of delegates, or at least help your understanding about what exactly is going on when you type code into your IDE of choice.&lt;/p&gt;
&lt;p&gt;Any questions/comments/feedback? Feel free to comment any of the posts.&lt;/p&gt;&lt;img src="http://newitup.com/aggbug/123.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Dan Donahue</dc:creator>
            <guid>http://newitup.com/archive/2009/08/13/delegates--lambdas-pt.-4--.net-helpers-the-final.aspx</guid>
            <pubDate>Thu, 13 Aug 2009 18:32:00 GMT</pubDate>
            <wfw:comment>http://newitup.com/comments/123.aspx</wfw:comment>
            <comments>http://newitup.com/archive/2009/08/13/delegates--lambdas-pt.-4--.net-helpers-the-final.aspx#feedback</comments>
            <wfw:commentRss>http://newitup.com/comments/commentRss/123.aspx</wfw:commentRss>
            <trackback:ping>http://newitup.com/services/trackbacks/123.aspx</trackback:ping>
        </item>
        <item>
            <title>Delegates =&gt; Lambdas, Pt. 3: Lambda Expressions</title>
            <category>.net</category>
            <category>delegates</category>
            <category>lambda-expression</category>
            <link>http://newitup.com/archive/2009/08/13/delegates--lambdas-pt.-3-lambda-expressions.aspx</link>
            <description>&lt;style type="text/css"&gt;&lt;![CDATA[
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: Consolas, "Courier New", Courier, Monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #006080; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}

.csharpcode .lnum { color: #606060; }
]]&gt;&lt;/style&gt;
&lt;p&gt;&lt;span style="font-weight: bold;"&gt;&lt;span style="font-style: italic;"&gt;This is a series I created on my company's internal intranet for a few young developers.  However, I thought it might be useful to anyone, so I wanted to make it public.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;     &lt;a href="../../../../archive/2009/08/13/delegates--lambdas-pt.-1-whats-a-delegate.aspx" title="Title of this entry." id="viewpost_ascx_TitleUrl"&gt;Delegates =&amp;gt; Lambdas, Pt. 1: What's A Delegate?&lt;/a&gt;     &lt;/li&gt;
    &lt;li&gt;     &lt;a href="../../../../archive/2009/08/13/delegates--lambdas-pt.-2-anonymous-methods--inline-delegates.aspx" title="Title of this entry." id="viewpost_ascx_TitleUrl"&gt;Delegates =&amp;gt; Lambdas, Pt. 2: Anonymous Methods == Inline Delegates&lt;/a&gt;     &lt;/li&gt;
    &lt;li&gt;     &lt;a href="../../../../archive/2009/08/13/delegates--lambdas-pt.-3-lambda-expressions.aspx" title="Title of this entry." id="viewpost_ascx_TitleUrl"&gt;Delegates =&amp;gt; Lambdas, Pt. 3: Lambda Expressions&lt;/a&gt;     &lt;/li&gt;
    &lt;li&gt;     &lt;a href="../../../../archive/2009/08/13/delegates--lambdas-pt.-4--.net-helpers-the-final.aspx" title="Title of this entry." id="viewpost_ascx_TitleUrl"&gt;Delegates =&amp;gt; Lambdas, Pt. 4:  .NET Helpers (The Final Episode)&lt;/a&gt;     &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;&lt;a href="http://newitup.com/archive/2009/08/13/delegates--lambdas-pt.-2-anonymous-methods--inline-delegates.aspx"&gt;In my last post&lt;/a&gt;, I explained how to use anonymous methods, or inline delegates, to remove a bunch of unnecessary code from your class.  This post is going to explain how lambda expressions are really just a syntatic helper to aid in the removal of more 'extra' code and to make anonymous methods a little more readable.&lt;/p&gt;
&lt;p&gt;And that's really what lambda expressions are - cleaner ways to write anonymous methods/inline delegates.&lt;/p&gt;
&lt;p&gt;Let's look at our example from last time.  To handle two different filtering scenarios, we had this:&lt;/p&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;IList&amp;lt;TeamDTO&amp;gt; allTeams = &lt;span class="kwrd"&gt;new&lt;/span&gt; List&amp;lt;TeamDTO&amp;gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;                                          {&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;                                              &lt;span class="kwrd"&gt;new&lt;/span&gt; TeamDTO(&lt;span class="str"&gt;"Flyers"&lt;/span&gt;, &lt;span class="str"&gt;"East"&lt;/span&gt;),&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;                                              &lt;span class="kwrd"&gt;new&lt;/span&gt; TeamDTO(&lt;span class="str"&gt;"Blackhawks"&lt;/span&gt;, &lt;span class="str"&gt;"West"&lt;/span&gt;),&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;                                              &lt;span class="kwrd"&gt;new&lt;/span&gt; TeamDTO(&lt;span class="str"&gt;"Blue Jackets"&lt;/span&gt;, &lt;span class="str"&gt;"West"&lt;/span&gt;),&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;                                              &lt;span class="kwrd"&gt;new&lt;/span&gt; TeamDTO(&lt;span class="str"&gt;"Capitals"&lt;/span&gt;, &lt;span class="str"&gt;"East"&lt;/span&gt;)&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;                                          };&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;IList&amp;lt;TeamDTO&amp;gt; easternTeams = SelectTeamsByFilter(allTeams, &lt;span class="kwrd"&gt;delegate&lt;/span&gt;(TeamDTO team) { &lt;span class="kwrd"&gt;return&lt;/span&gt; team.Conference == &lt;span class="str"&gt;"East"&lt;/span&gt;; });&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;IList&amp;lt;TeamDTO&amp;gt; teamsThatStartWithLetterB = SelectTeamsByFilter(allTeams, &lt;span class="kwrd"&gt;delegate&lt;/span&gt;(TeamDTO team) { &lt;span class="kwrd"&gt;return&lt;/span&gt; team.TeamName.StartsWith(&lt;span class="str"&gt;"B"&lt;/span&gt;); });&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;This isn't bad, but you still have to use the delegate keyword and declare a function, complete with parentheses and curly braces inline. That can get hard to match up pretty quickly.  So here are these same lines using lambda expressions:&lt;/p&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;IList&amp;lt;TeamDTO&amp;gt; allTeams = &lt;span class="kwrd"&gt;new&lt;/span&gt; List&amp;lt;TeamDTO&amp;gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;                                          {&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;                                              &lt;span class="kwrd"&gt;new&lt;/span&gt; TeamDTO(&lt;span class="str"&gt;"Flyers"&lt;/span&gt;, &lt;span class="str"&gt;"East"&lt;/span&gt;),&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;                                              &lt;span class="kwrd"&gt;new&lt;/span&gt; TeamDTO(&lt;span class="str"&gt;"Blackhawks"&lt;/span&gt;, &lt;span class="str"&gt;"West"&lt;/span&gt;),&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;                                              &lt;span class="kwrd"&gt;new&lt;/span&gt; TeamDTO(&lt;span class="str"&gt;"Blue Jackets"&lt;/span&gt;, &lt;span class="str"&gt;"West"&lt;/span&gt;),&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;                                              &lt;span class="kwrd"&gt;new&lt;/span&gt; TeamDTO(&lt;span class="str"&gt;"Capitals"&lt;/span&gt;, &lt;span class="str"&gt;"East"&lt;/span&gt;)&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;                                          };&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;IList&amp;lt;TeamDTO&amp;gt; easternTeams = SelectTeamsByFilter(allTeams, team =&amp;gt; team.Conference == &lt;span class="str"&gt;"East"&lt;/span&gt;);&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;IList&amp;lt;TeamDTO&amp;gt; teamsThatStartWithLetterB = SelectTeamsByFilter(allTeams, team =&amp;gt; team.TeamName.StartsWith(&lt;span class="str"&gt;"B"&lt;/span&gt;));&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;As I said earlier, this is really just a cleaner way to write the delegate.  The left side of the lambda expression declares the inputs.  You will often see these as x.  That's just a convention.  I chose team because I think it's more descriptive.  The right side of the lambda expression holds the statement block, or function body.  Pretty simple - lambda expressions are delegates.  Nothing to be afraid of.&lt;/p&gt;
&lt;p&gt;A few notes about lambda expressions before this post is over.&lt;/p&gt;
&lt;p&gt;When a lambda expression's body contains only one line of code, you don't need a return - the result of that line of code is assumed to be the return value.  So above, the line evaluates to a boolean, so that is considered the return value.  That said - you CAN write lambda expressions containing multiple lines of code, but you then have to include the return keyword and wrap your expression body in curly braces.  Here's a quick example:&lt;/p&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;return&lt;/span&gt; SelectTeamsByFilter(allTeams, team =&amp;gt; {&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;                                                         &lt;span class="kwrd"&gt;int&lt;/span&gt; count = team.TeamName.Length;&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;                                                         &lt;span class="kwrd"&gt;return&lt;/span&gt; count == 10;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;                                                     });&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Not super realistic, it filters by teams whose name is ten characters long.&lt;/p&gt;
&lt;p&gt;Also - lambda expressions can take zero parameters.  In that case, you use an empty set of parentheses on the left side like this:&lt;/p&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;() =&amp;gt; SomeMethod()&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;And they can also take multiple inputs.  For example, if you wanted to compare two teams:&lt;/p&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;(team1, team2) =&amp;gt; team1.TeamName == team2.TeamName&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;So there you go.  Lambda expressions are nothing more than clean ways to write delegates.  And delegates are a really powerful and useful feature to allow you to separate a method from its implementation.&lt;/p&gt;
&lt;p&gt;There's going to be one more post in this series.  It's a bit of a tangent rather than a continuation, but I wanted to introduce a few classes that .NET provides to help facilitate the use of delegates in your code.  So look for that in the next couple of days.&lt;/p&gt;&lt;img src="http://newitup.com/aggbug/122.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Dan Donahue</dc:creator>
            <guid>http://newitup.com/archive/2009/08/13/delegates--lambdas-pt.-3-lambda-expressions.aspx</guid>
            <pubDate>Thu, 13 Aug 2009 18:18:14 GMT</pubDate>
            <wfw:comment>http://newitup.com/comments/122.aspx</wfw:comment>
            <comments>http://newitup.com/archive/2009/08/13/delegates--lambdas-pt.-3-lambda-expressions.aspx#feedback</comments>
            <wfw:commentRss>http://newitup.com/comments/commentRss/122.aspx</wfw:commentRss>
            <trackback:ping>http://newitup.com/services/trackbacks/122.aspx</trackback:ping>
        </item>
        <item>
            <title>Delegates =&gt; Lambdas, Pt. 2: Anonymous Methods == Inline Delegates</title>
            <category>.net</category>
            <category>delegates</category>
            <category>lambda-expression</category>
            <link>http://newitup.com/archive/2009/08/13/delegates--lambdas-pt.-2-anonymous-methods--inline-delegates.aspx</link>
            <description>&lt;style type="text/css"&gt;&lt;![CDATA[csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: Consolas, "Courier New", Courier, Monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #006080; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}

.csharpcode .lnum { color: #606060; }
]]&gt;&lt;/style&gt;
&lt;p&gt;&lt;span style="font-weight: bold;"&gt;&lt;span style="font-style: italic;"&gt;This is a series I created on my company's internal intranet for a few young developers.  However, I thought it might be useful to anyone, so I wanted to make it public.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;     &lt;a id="viewpost_ascx_TitleUrl" title="Title of this entry." href="../../../../archive/2009/08/13/delegates--lambdas-pt.-1-whats-a-delegate.aspx"&gt;Delegates =&amp;gt; Lambdas, Pt. 1: What's A Delegate?&lt;/a&gt;     &lt;/li&gt;
    &lt;li&gt;     &lt;a id="viewpost_ascx_TitleUrl" title="Title of this entry." href="../../../../archive/2009/08/13/delegates--lambdas-pt.-2-anonymous-methods--inline-delegates.aspx"&gt;Delegates =&amp;gt; Lambdas, Pt. 2: Anonymous Methods == Inline Delegates&lt;/a&gt;     &lt;/li&gt;
    &lt;li&gt;     &lt;a id="viewpost_ascx_TitleUrl" title="Title of this entry." href="../../../../archive/2009/08/13/delegates--lambdas-pt.-3-lambda-expressions.aspx"&gt;Delegates =&amp;gt; Lambdas, Pt. 3: Lambda Expressions&lt;/a&gt;     &lt;/li&gt;
    &lt;li&gt;     &lt;a id="viewpost_ascx_TitleUrl" title="Title of this entry." href="../../../../archive/2009/08/13/delegates--lambdas-pt.-4--.net-helpers-the-final.aspx"&gt;Delegates =&amp;gt; Lambdas, Pt. 4:  .NET Helpers (The Final Episode)&lt;/a&gt;     &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;&lt;a href="http://newitup.com/archive/2009/08/13/delegates--lambdas-pt.-1-whats-a-delegate.aspx"&gt;In my last post&lt;/a&gt;, I introduced delegates and briefly showed you how to use them, along with explaining a few of the benefits.  At the same time, there were some drawbacks as well that I probably didn't mention, but may have hinted at.&lt;/p&gt;
&lt;p&gt;I suggest you look at that post again to refresh your memory about how delegates are declared and used.  The first thing you might notice that is a little less than ideal about the last example is that you still need to declare a function for each of your filters - &lt;em&gt;IsEasternConferenceTeam, &lt;/em&gt;&lt;em&gt;TeamNameStartsWithB, &lt;/em&gt;etc.  At that point, why don't you just call them directly in your code instead of passing them into another function?&lt;/p&gt;
&lt;p&gt;This is where &lt;strong&gt;anonymous methods&lt;/strong&gt; come in.  Anonymous methods allow you to declare methods inline using the &lt;em&gt;delegate &lt;/em&gt;keyword.  This cuts down on unnecessary private functions bulking up your code.&lt;/p&gt;
&lt;p&gt;Let's take a look at an example.  As a quick refresher, in the last post, we had this for filtering:&lt;/p&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;bool&lt;/span&gt; IsEasternConferenceTeam(TeamDTO team)&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;{&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;     &lt;span class="kwrd"&gt;return&lt;/span&gt; team.Conference == &lt;span class="str"&gt;"East"&lt;/span&gt;;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;}&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;bool&lt;/span&gt; TeamNameStartsWithB(TeamDTO team)&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;{&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;     &lt;span class="kwrd"&gt;return&lt;/span&gt; team.TeamName.StartsWith(&lt;span class="str"&gt;"B"&lt;/span&gt;);&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;}&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;IList&amp;lt;TeamDTO&amp;gt; allTeams = &lt;span class="kwrd"&gt;new&lt;/span&gt; List&amp;lt;TeamDTO&amp;gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;                                          {&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;                                              &lt;span class="kwrd"&gt;new&lt;/span&gt; TeamDTO(&lt;span class="str"&gt;"Flyers"&lt;/span&gt;, &lt;span class="str"&gt;"East"&lt;/span&gt;),&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt;                                              &lt;span class="kwrd"&gt;new&lt;/span&gt; TeamDTO(&lt;span class="str"&gt;"Blackhawks"&lt;/span&gt;, &lt;span class="str"&gt;"West"&lt;/span&gt;),&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt;                                              &lt;span class="kwrd"&gt;new&lt;/span&gt; TeamDTO(&lt;span class="str"&gt;"Blue Jackets"&lt;/span&gt;, &lt;span class="str"&gt;"West"&lt;/span&gt;),&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  16:  &lt;/span&gt;                                              &lt;span class="kwrd"&gt;new&lt;/span&gt; TeamDTO(&lt;span class="str"&gt;"Capitals"&lt;/span&gt;, &lt;span class="str"&gt;"East"&lt;/span&gt;)&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  17:  &lt;/span&gt;                                          };&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  18:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  19:  &lt;/span&gt;IList&amp;lt;TeamDTO&amp;gt; easternTeams = SelectTeamsByFilter(allTeams, IsEasternConferenceTeam); &lt;span class="rem"&gt;// will return a list wtih Flyers and Capitals&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  20:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  21:  &lt;/span&gt;IList&amp;lt;TeamDTO&amp;gt; teamsThatStartWithLetterB = SelectTeamsByFilter(allTeams, TeamNameStartsWithB); // will &lt;span class="kwrd"&gt;return&lt;/span&gt; a list with Blackhawks and Blue Jackets&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Those two functions at the top are really just different filters.  Seems a little bulky.  So with anonymous methods we can do this instead:&lt;/p&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;IList&amp;lt;TeamDTO&amp;gt; allTeams = &lt;span class="kwrd"&gt;new&lt;/span&gt; List&amp;lt;TeamDTO&amp;gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;                                          {&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;                                              &lt;span class="kwrd"&gt;new&lt;/span&gt; TeamDTO(&lt;span class="str"&gt;"Flyers"&lt;/span&gt;, &lt;span class="str"&gt;"East"&lt;/span&gt;),&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;                                              &lt;span class="kwrd"&gt;new&lt;/span&gt; TeamDTO(&lt;span class="str"&gt;"Blackhawks"&lt;/span&gt;, &lt;span class="str"&gt;"West"&lt;/span&gt;),&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;                                              &lt;span class="kwrd"&gt;new&lt;/span&gt; TeamDTO(&lt;span class="str"&gt;"Blue Jackets"&lt;/span&gt;, &lt;span class="str"&gt;"West"&lt;/span&gt;),&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;                                              &lt;span class="kwrd"&gt;new&lt;/span&gt; TeamDTO(&lt;span class="str"&gt;"Capitals"&lt;/span&gt;, &lt;span class="str"&gt;"East"&lt;/span&gt;)&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;                                          };&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;IList&amp;lt;TeamDTO&amp;gt; easternTeams = SelectTeamsByFilter(allTeams, &lt;span class="kwrd"&gt;delegate&lt;/span&gt;(TeamDTO team) { &lt;span class="kwrd"&gt;return&lt;/span&gt; team.Conference == &lt;span class="str"&gt;"East"&lt;/span&gt;; });&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;IList&amp;lt;TeamDTO&amp;gt; teamsThatStartWithLetterB = SelectTeamsByFilter(allTeams, &lt;span class="kwrd"&gt;delegate&lt;/span&gt;(TeamDTO team) { &lt;span class="kwrd"&gt;return&lt;/span&gt; team.TeamName.StartsWith(&lt;span class="str"&gt;"B"&lt;/span&gt;); });&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Now - we can get rid of those functions.  And we can write whatever types of filters we want inline in code wherever we need to filter things.&lt;/p&gt;
&lt;p&gt;Again - notice that as long as long as the contract is fulfilled for this delegate (TeamDTO input, boolean output), there will be no complaints from the compiler.  And just like I mentioned before, these functions can actually do as much as work as you'd like them to - they don't need to be just one line.  Of course, when you get into writing them inline like this, big functions that do a lot of heavy lifting quickly become hard to format.  But for quick one-liners, I think this is awesome.  And imagine, like I mentioned in my last post, how easy it is now for consumers of this code to write new filters?&lt;/p&gt;
&lt;p&gt;The next step is from anonymous methods to lambdas.&lt;/p&gt;&lt;img src="http://newitup.com/aggbug/121.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Dan Donahue</dc:creator>
            <guid>http://newitup.com/archive/2009/08/13/delegates--lambdas-pt.-2-anonymous-methods--inline-delegates.aspx</guid>
            <pubDate>Thu, 13 Aug 2009 18:12:07 GMT</pubDate>
            <wfw:comment>http://newitup.com/comments/121.aspx</wfw:comment>
            <comments>http://newitup.com/archive/2009/08/13/delegates--lambdas-pt.-2-anonymous-methods--inline-delegates.aspx#feedback</comments>
            <wfw:commentRss>http://newitup.com/comments/commentRss/121.aspx</wfw:commentRss>
            <trackback:ping>http://newitup.com/services/trackbacks/121.aspx</trackback:ping>
        </item>
        <item>
            <title>Fixing Broken Windows</title>
            <link>http://newitup.com/archive/2009/06/29/fixing-broken-windows.aspx</link>
            <description>&lt;p&gt;I started re-reading &lt;a href="http://www.amazon.com/Pragmatic-Programmer-Journeyman-Master/dp/020161622X"&gt;The Pragmatic Programmer&lt;/a&gt; over the weekend and one of the early chapters mentioned the broken window theory.&lt;/p&gt;
&lt;p&gt;Google search the term, but the quick version is basically that studies were done by experts in crime and urban decay that something as small as one broken window in a building, left unfixed, will make people occupying a neighborhood feel as though no one cares about the neighborhood and things will go downhill from there.  One study was done where a car was left in the same spot for weeks and nothing happpened to it.  Then one day, the person conducting the study smashed the window and by the next day, the entire car had been stripped.&lt;/p&gt;
&lt;p&gt;Ok, so how does this apply to software?  Well - when a codebase contains a lot of un-clean code and generally looks as though it's been abandoned, the developers working on it are often more likely to use it as an excuse to do things the quick and dirty way.  Why should I worry about making my code maintainable, when this codebase has duplication all over the place and functions/pages that haven't worked correctly in months.&lt;/p&gt;
&lt;p&gt;On the flipside, if a codebase is nice, clean and easy to work with... developers will be LESS likely to go for the quick and dirty change because they won't want to be responsible for being the one to start the downfall.&lt;/p&gt;
&lt;p&gt;The reason this hit me so hard this weekend while reading was on account of something that occurred during work on Friday.  I came across an old file in my application that had a few validation functions in it.  The only problem was, the entire body of the functions were commented out and all that was left was a "return true". &lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;Now, this is hardly the biggest problem I've come across in this codebase since I started my job, but it was just another case where I could've seen it, felt hopeless and moved on.  But what I decided to go was... fix them.  Remove the functions that have been commented out for years and then clean up the code was calling on them only to set their values to true.&lt;/p&gt;
&lt;p&gt;And that's basically what the chapter in The Pragmatic Programmer suggests.  Fix Broken Windows.  Because just like seeing decay will sway a fellow developer to continue the decay, seeing repair will push fellow developers to continue that effort towards a cleaner codebase.&lt;/p&gt;&lt;img src="http://newitup.com/aggbug/117.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Dan Donahue</dc:creator>
            <guid>http://newitup.com/archive/2009/06/29/fixing-broken-windows.aspx</guid>
            <pubDate>Mon, 29 Jun 2009 20:09:31 GMT</pubDate>
            <wfw:comment>http://newitup.com/comments/117.aspx</wfw:comment>
            <comments>http://newitup.com/archive/2009/06/29/fixing-broken-windows.aspx#feedback</comments>
            <wfw:commentRss>http://newitup.com/comments/commentRss/117.aspx</wfw:commentRss>
            <trackback:ping>http://newitup.com/services/trackbacks/117.aspx</trackback:ping>
        </item>
        <item>
            <title>Book Review: Working Effectively With Legacy Code</title>
            <category>book-review</category>
            <link>http://newitup.com/archive/2009/06/19/book-review-working-effectively-with-legacy-code.aspx</link>
            <description>This is a very important book on a topic that hasn't been covered thoroughly prior to its release.  For all the text out there about Agile software development, XP programming practices, unit testing, etc., there seems to be an unwritten understanding that you're dealing with a fresh, new project space.  And for me, specifically, that was the case for the past few years.  As a independent contractor, I was often brought in to projects at their beginning to see them through to completion.  But having recently re-joined the full-time employee ranks to maintain and improve an existing product, I now understand how overwhelming it can be to look at a giant codebase and think to yourself "ok, where do I even start?"&lt;br /&gt;
&lt;br /&gt;
That's where this book comes in.&lt;br /&gt;
&lt;br /&gt;
First, it's definition of legacy code is simple.  Any piece of code that is not under test.  I think that's a good starting point.  Too many people think of legacy code as having to have been written years ago and allowed to degrade.  Not true.  By the author's definition, legacy code is still being written by developers every single day.&lt;br /&gt;
&lt;br /&gt;
So obviously, the point of the book is refactoring existing code to make it testable.  And what's provided are chapters describing different situations, such as "I'm Changing The Same Code All Over The Place" and "I Can't Run This Method In A Test Harness".  In that way, the book can act like a reference material for programmers working in legacy code.  If you see yourself running up against one of these problems, simply flip to the chapter list, find your problem and read the chapter about it.  Most chapters are pretty short and get right to the point.&lt;br /&gt;
&lt;br /&gt;
After the situational chapters, the book ends with a list of refactoring techniques, written step-by-step, that you can apply to get pieces of your code under test.  It's very much like Fowler's Refactoring book, except with some new techniques explicitly for getting your code into a test harness.&lt;br /&gt;
&lt;br /&gt;
I had heard that this book was useful for years now, but never bothered to pick it up.  But now facing a codebase that seems unwieldy, I think this may be one of the most important books I've ever read.  We don't all code in green pastures, but this book certainly helps you not lose focus of the techniques you hold dear (unit testing, separation of concerns, D-R-Y, single responsibility, etc.) when you encounter a situation that seems overwhelming.&lt;br /&gt;
&lt;br /&gt;
http://www.amazon.com/Working-Effectively-Legacy-Robert-Martin/dp/0131177052&lt;img src="http://newitup.com/aggbug/116.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Dan Donahue</dc:creator>
            <guid>http://newitup.com/archive/2009/06/19/book-review-working-effectively-with-legacy-code.aspx</guid>
            <pubDate>Fri, 19 Jun 2009 18:13:19 GMT</pubDate>
            <wfw:comment>http://newitup.com/comments/116.aspx</wfw:comment>
            <comments>http://newitup.com/archive/2009/06/19/book-review-working-effectively-with-legacy-code.aspx#feedback</comments>
            <wfw:commentRss>http://newitup.com/comments/commentRss/116.aspx</wfw:commentRss>
            <trackback:ping>http://newitup.com/services/trackbacks/116.aspx</trackback:ping>
        </item>
        <item>
            <title>Code Contracts In .Net 4.0 - My second thoughts</title>
            <category>.net</category>
            <category>4.0</category>
            <link>http://newitup.com/archive/2009/06/18/code-contracts-in-.net-4.0---my-second-thoughts.aspx</link>
            <description>&lt;p&gt;Well - I just watched this screencast which served as my intro to Code Contracts and now I'm not quite as siked on them:&lt;/p&gt;
&lt;p&gt;http://www.dimecasts.net/Casts/CastFeedDetails/118&lt;/p&gt;
&lt;p&gt;They're still pretty useful and I think the syntax beats having all the IF statements checking for null and other expectations as far as readability goes, but I really thought they would throw a full fledged compiler error in the case that a contract was violated, rather than just a warning.  I mean, it's better than nothing, but a lot of programmers ignore warnings (unless you've subscribed to the &lt;a href="http://media.pragprog.com/titles/pad/CodeAndDebug.pdf" class=""&gt;pragmatic programmers &lt;/a&gt;philosophy of treating warnings as errors).&lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;I also don't particularly like the code you have to write to handle the contract check (it's in the SetUp of the test harness in the video).  It just seems like an added step, where you could just assert that the exception was thrown in the old piece of code, without having to write in that extra stuff in SetUp.&lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;I'm still looking forward to hearing more about this as VS2010 and .NET 4.0 come closer to being released.  For me, the jury is still out on this one.&lt;/p&gt;
&lt;p&gt;But the video did show me one cool thing, completely unrelated to code contracts.  In his test, he used NUnit syntax helpers which I thought made the test code more readable.  I'm going to have to look into those and once again renew my ongoing battle between MbUnit and NUnit.&lt;/p&gt;&lt;img src="http://newitup.com/aggbug/114.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Dan Donahue</dc:creator>
            <guid>http://newitup.com/archive/2009/06/18/code-contracts-in-.net-4.0---my-second-thoughts.aspx</guid>
            <pubDate>Thu, 18 Jun 2009 13:30:48 GMT</pubDate>
            <wfw:comment>http://newitup.com/comments/114.aspx</wfw:comment>
            <comments>http://newitup.com/archive/2009/06/18/code-contracts-in-.net-4.0---my-second-thoughts.aspx#feedback</comments>
            <wfw:commentRss>http://newitup.com/comments/commentRss/114.aspx</wfw:commentRss>
            <trackback:ping>http://newitup.com/services/trackbacks/114.aspx</trackback:ping>
        </item>
        <item>
            <title>Code Contracts In .Net 4.0 - My initial thoughts</title>
            <category>.net</category>
            <category>4.0</category>
            <link>http://newitup.com/archive/2009/06/16/my-thoughts-on-code-contracts-new-.net-4.0-feature.aspx</link>
            <description>I just read about this new feature coming with .NET 4.0 this morning at work, linked from some blog in my reader.  After following a few links, I came to this one:&lt;br /&gt;
&lt;br /&gt;
&lt;a class="linkification-ext" href="http://codebetter.com/blogs/matthew.podwysocki/archive/2008/11/08/code-contracts-and-net-4-0-spec-comes-alive.aspx" title="Linkification: http://codebetter.com/blogs/matthew.podwysocki/archive/2008/11/08/code-contracts-and-net-4-0-spec-comes-alive.aspx"&gt;http://codebetter.com/blogs/matthew.podwysocki/archive/2008/11/08/code-contracts-and-net-4-0-spec-comes-alive.aspx&lt;/a&gt;&lt;br /&gt;
&lt;a href="javascript:void(0);/*1245162399274*/"&gt;http://research.microsoft.com/en-us/projects/contracts/&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
In a nutshell, Code Contracts is Microsoft's Design By Contract (DbC) implementation. And it looks pretty cool.&lt;br /&gt;
&lt;br /&gt;
Admittedly having only looked at it for about 20 minutes this morning, my initial thoughts are:&lt;br /&gt;
&lt;br /&gt;
&lt;p&gt;+ It's AWESOME that this library supports runtime checking.  That means if you have a piece of code like:&lt;/p&gt;
&lt;p&gt;if (someObject == null) throw someException else { // do stuff};&lt;/p&gt;
&lt;p&gt;you can replace it with:&lt;/p&gt;
&lt;p&gt;CodeContract.RequiresAlways(someObject != &lt;span style="color: blue;"&gt;null&lt;/span&gt;);&lt;/p&gt;
&lt;p&gt;// do stuff&lt;/p&gt;
&lt;p&gt;And if during compilation, the compiler notices a spot where null is being passed into the function, it will notify you with an error at compile time, rather than throwing an error when you run your code.  That's ESPECIALLY cool if you build them into a library that you release and let others use.  If they try to pass invalid objects into your library, they'll be notified when they compile.&lt;/p&gt;
&lt;p&gt;+ The syntax is certainly cleaner than wrapping things in IF statements to check their state.  The code also communicates more clearly, in that regard.  It's easy to read what is expected in a function the way these contracts are written.&lt;/p&gt;
&lt;p&gt;Now, the negatives:&lt;/p&gt;
&lt;p&gt;- I still wish there was a better way to add these conditions to your code.  It's better than IF statements, but I still wish I could just write a clean function without having that in there so that ONLY the actions performed within the functon stood out.  Oh well, it's a step in the right direction.&lt;/p&gt;
&lt;p&gt;- I think it could lull people into a false sense of security and away from unit testing, which is a slippery slope.  But a few articles I read made the case for using both complementary, as opposed to trying to tout the benefits of one over the other.&lt;/p&gt;
&lt;p&gt;I'm going to look into it a bit more in my free time (I'm posting on my company's dime right now), but if anyone even knows my blog exists, I'd like your thoughts on this.  Is it worth using or not?&lt;/p&gt;&lt;img src="http://newitup.com/aggbug/113.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Dan Donahue</dc:creator>
            <guid>http://newitup.com/archive/2009/06/16/my-thoughts-on-code-contracts-new-.net-4.0-feature.aspx</guid>
            <pubDate>Tue, 16 Jun 2009 15:25:44 GMT</pubDate>
            <wfw:comment>http://newitup.com/comments/113.aspx</wfw:comment>
            <comments>http://newitup.com/archive/2009/06/16/my-thoughts-on-code-contracts-new-.net-4.0-feature.aspx#feedback</comments>
            <wfw:commentRss>http://newitup.com/comments/commentRss/113.aspx</wfw:commentRss>
            <trackback:ping>http://newitup.com/services/trackbacks/113.aspx</trackback:ping>
        </item>
        <item>
            <title>How will MS MVC affect the greater developer community?</title>
            <category>.net</category>
            <link>http://newitup.com/archive/2007/12/05/65.aspx</link>
            <description>5 months to make my first post?  Yikes!  I need to get on the ball.&lt;br /&gt;
&lt;br /&gt;
But now seems like the perfect time to jump into things with the release of the &lt;a href="http://weblogs.asp.net/scottgu/archive/2007/11/29/net-web-product-roadmap-asp-net-silverlight-iis7.aspx&amp;#xD;&amp;#xA;target=" _blank=""&gt;ASP.NET 3.5 Extensions Preview&lt;/a&gt; coming soon (supposedly this week, but I haven't been able to find it yet) which features the MS MVC Framework that the Alt.Net community has been so excited for.&lt;br /&gt;
&lt;br /&gt;
WIth the release imminent, I've been thinking about a few things.  Not things like predicting syntax, interoperability or how MS MVC will ease TDD. &lt;br /&gt;
&lt;br /&gt;
I'm wondering how this release will affect the community.  Not just the Alt.Net community - those whom have already bought into the idea of &lt;a href="http://laribee.com/blog/2007/04/10/altnet/"&gt;reaching outside of the mainstream&lt;/a&gt;.  I'm wondering if the backing of Microsoft will lead those developers whom are perfectly content with doing things the "Microsoft way" with giving MVC a shot, now that it, in a way, &lt;span style="font-style: italic;"&gt;is &lt;/span&gt;the Microsoft way.  Or at least it could be.&lt;br /&gt;
&lt;br /&gt;
Furthermore, will Microsoft promote their MVC architecture as the new way to do things?  With Ruby On Rails getting a lot of attention lately, Microsoft may make a big deal about their implementation of a similar framework.  Can we expect to see code samples and tutorials in MSDN, featured prominently?&lt;br /&gt;
&lt;br /&gt;
As someone who has taken a strong interest in MVC through working with MonoRail, I'd be overjoyed if Microsoft would push this framework as the new standard.  Imagine removing the pressure points of trying to sell this technique to management and other team members?  No more fighting the uphill battle trying to explain that this separation of concerns simplifies code, makes things more testable, maintainable, etc.  No more hitting walls of resistance because developers would rather not leave their comfort zone.  Instead of trying to sell MVC as a better way, we'll be able to say "this is the Microsoft way!"&lt;br /&gt;
&lt;br /&gt;
I obviously don't have the answers, only the questions.  So what does everyone else think?  Will MS make a big deal out of this release?  And what would you LIKE to see them do, and why?  (I bet you didn't expect a pop quiz from my first blog post!)&lt;img src="http://newitup.com/aggbug/65.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Dan Donahue</dc:creator>
            <guid>http://newitup.com/archive/2007/12/05/65.aspx</guid>
            <pubDate>Thu, 06 Dec 2007 03:58:59 GMT</pubDate>
            <wfw:comment>http://newitup.com/comments/65.aspx</wfw:comment>
            <comments>http://newitup.com/archive/2007/12/05/65.aspx#feedback</comments>
            <slash:comments>3</slash:comments>
            <wfw:commentRss>http://newitup.com/comments/commentRss/65.aspx</wfw:commentRss>
            <trackback:ping>http://newitup.com/services/trackbacks/65.aspx</trackback:ping>
        </item>
    </channel>
</rss>