13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

356 CHAPTER 13 Elegant code <strong>in</strong> the new eraThis works well when a s<strong>in</strong>gle, isolated aspect of the component needs to be specialized.It’s certa<strong>in</strong>ly not a complete replacement of <strong>in</strong>heritance, nor would I want itto be (the title of this section notwithstand<strong>in</strong>g)—but it allows a more direct approachto be used <strong>in</strong> many situations. Us<strong>in</strong>g <strong>in</strong>terfaces with a small set of methods has oftenbeen another way of provid<strong>in</strong>g custom behavior, and delegates can be regarded as anextreme case of this approach.Of course, this is similar to the po<strong>in</strong>t made earlier about a more functional bias, butit’s applied to the specific area of <strong>in</strong>heritance and <strong>in</strong>terface implementation. It’s notentirely new to <strong>C#</strong> 3, either: List made a start <strong>in</strong> .NET 2.0 even when only <strong>C#</strong> 2 wasavailable, with methods such as Sort and F<strong>in</strong>dAll. Sort allows both an <strong>in</strong>terface-basedcomparison (with IComparer) and a delegate-based comparison (with Comparison),whereas F<strong>in</strong>dAll is purely delegate based. Anonymous methods made these calls relativelysimple and lambda expressions add even more readability.In short, when a type or method needs a s<strong>in</strong>gle aspect of specialized behavior, it’sworth at least consider<strong>in</strong>g the ability to specify that behavior <strong>in</strong> terms of a delegate<strong>in</strong>stead of via <strong>in</strong>heritance or an <strong>in</strong>terface.All of this contributes to our next big goal: readable code.13.3 Readability of results over implementationThe word readability is bandied around quite casually as if it can only mean one th<strong>in</strong>gand can somehow be measured objectively. In real life, different developers f<strong>in</strong>d differentth<strong>in</strong>gs readable, and <strong>in</strong> different ways. There are two k<strong>in</strong>ds of readability I’dlike to separate—while acknowledg<strong>in</strong>g that many more categorizations are possible.First, there is the ease with which a reader can understand exactly what your codeis do<strong>in</strong>g at every step. For <strong>in</strong>stance, mak<strong>in</strong>g every conversion explicit even if there’s animplicit one available makes it clear that a conversion is <strong>in</strong>deed tak<strong>in</strong>g place. This sortof detail can be useful if you’re ma<strong>in</strong>ta<strong>in</strong><strong>in</strong>g code and have already isolated the problemto a few l<strong>in</strong>es of code. However, it tends to be longw<strong>in</strong>ded, mak<strong>in</strong>g it harder tobrowse large sections of source. I th<strong>in</strong>k of this as “readability of implementation.”When it comes to gett<strong>in</strong>g the broad sweep of code, what is required is “readabilityof results”—I want to know what the code does, but I don’t care how it does it rightnow. Much of this has traditionally been down to refactor<strong>in</strong>g, careful nam<strong>in</strong>g, andother best practices. For example, a method that needs to perform several steps canoften be refactored <strong>in</strong>to a method that simply calls other (reasonably short) methodsto do the actual work. Declarative languages tend to emphasize readability of results.<strong>C#</strong> 3 and LINQ comb<strong>in</strong>e to improve readability of results quite significantly—at thecost of readability of implementation. Almost all the cleverness shown by the <strong>C#</strong> 3compiler adds to this: extension methods make the <strong>in</strong>tention of the code clearer, butat the cost of the visibility of the extra static class <strong>in</strong>volved, for example.This isn’t just a language issue, though; it’s also part of the framework support.Consider how you might have implemented our earlier user query <strong>in</strong> .NET 1.1. Theessential <strong>in</strong>gredients are filter<strong>in</strong>g, sort<strong>in</strong>g, and project<strong>in</strong>g:Licensed to Rhona Hadida

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!