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.

222 CHAPTER 8 Cutt<strong>in</strong>g fluff with a smart compilerSETTING UP UNIT TESTSWhen writ<strong>in</strong>g unit tests, I frequently want to populate an object just for one test, oftenpass<strong>in</strong>g it <strong>in</strong> as a parameter to the method I’m try<strong>in</strong>g to test at the time. This is particularlycommon with entity classes. Writ<strong>in</strong>g all of the <strong>in</strong>itialization “long-hand” can belongw<strong>in</strong>ded and also hides the essential structure of the object from the reader of thecode, just as XML creation code can often obscure what the document would look likeif you viewed it (appropriately formatted) <strong>in</strong> a text editor. With appropriate <strong>in</strong>dentationof object <strong>in</strong>itializers, the nested structure of the object hierarchy can becomeobvious <strong>in</strong> the very shape of the code, as well as make the values stand out more thanthey would otherwise.PARAMETER ENCAPSULATIONSometimes patterns occur <strong>in</strong> production code that can be aided by <strong>C#</strong> 3’s <strong>in</strong>itializationfeatures. For <strong>in</strong>stance, rather than specify<strong>in</strong>g several parameters to a s<strong>in</strong>gle method,you can sometimes make code more straightforward by collect<strong>in</strong>g the parameterstogether <strong>in</strong> an extra type. The framework ProcessStartInfo type is a good exampleof this—the designers could have overloaded Process.Start with many different setsof parameters, but us<strong>in</strong>g ProcessStartInfo makes everyth<strong>in</strong>g clearer. <strong>C#</strong> 3 allows youto create a ProcessStartInfo and fill <strong>in</strong> all the properties <strong>in</strong> a clearer manner—andyou could even specify it <strong>in</strong>l<strong>in</strong>e <strong>in</strong> a call to Process.Start. In some ways, the methodcall would then act as if it had a lot of default parameters, with the properties provid<strong>in</strong>gthe names of parameters you want to specify. It’s worth consider<strong>in</strong>g this patternwhen you f<strong>in</strong>d yourself us<strong>in</strong>g lots of parameters—it was always a useful technique toknow about, but <strong>C#</strong> 3 makes it that bit more elegant.Of course, there are uses beyond these three <strong>in</strong> ord<strong>in</strong>ary code, and I certa<strong>in</strong>ly don’twant to put you off us<strong>in</strong>g the new features elsewhere. There’s very little reason not touse them, other than possibly confus<strong>in</strong>g developers who aren’t familiar with <strong>C#</strong> 3 yet.You may decide that us<strong>in</strong>g an object <strong>in</strong>itializer just to set one property (as opposed tojust explicitly sett<strong>in</strong>g it <strong>in</strong> a separate statement) is over the top—that’s a matter of aesthetics,and I can’t give you much guidance there. As with implicit typ<strong>in</strong>g, it’s a goodidea to try the code both ways, and learn to predict your own (and your team’s) read<strong>in</strong>gpreferences.So far we’ve looked at a fairly diverse range of features: implement<strong>in</strong>g propertieseasily, simplify<strong>in</strong>g local variable declarations, and populat<strong>in</strong>g objects <strong>in</strong> s<strong>in</strong>gle expressions.In the rema<strong>in</strong>der of this chapter we’ll be gradually br<strong>in</strong>g<strong>in</strong>g these topicstogether, us<strong>in</strong>g more implicit typ<strong>in</strong>g and more object population, and creat<strong>in</strong>g wholetypes without giv<strong>in</strong>g any implementation details.Our next topic appears to be quite similar to collection <strong>in</strong>itializers when you lookat code us<strong>in</strong>g it. I mentioned earlier that array <strong>in</strong>itialization was a bit clumsy <strong>in</strong> <strong>C#</strong> 1and 2. I’m sure it won’t surprise you to learn that it’s been streaml<strong>in</strong>ed for <strong>C#</strong> 3. Let’stake a look.Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!