13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Novel uses of nullable types131particularly tax<strong>in</strong>g with the if/else version, but I know I’d have to stop and th<strong>in</strong>ktwice, and verify the code mentally. I’d also be rely<strong>in</strong>g on unit tests, so there’d be relativelylittle chance of me actually gett<strong>in</strong>g it wrong, but I’d prefer not to th<strong>in</strong>k aboutth<strong>in</strong>gs like this unless I absolutely have to.NOTEEveryth<strong>in</strong>g <strong>in</strong> moderation—Just <strong>in</strong> case you may be th<strong>in</strong>k<strong>in</strong>g that my code islittered with uses of the null coalesc<strong>in</strong>g operator, it’s really not. I tend toconsider it when I see default<strong>in</strong>g mechanisms <strong>in</strong>volv<strong>in</strong>g nulls and possiblythe conditional operator, but it doesn’t come up very often. When its useis natural, however, it can be a powerful tool <strong>in</strong> the battle for readability.We’ve seen how nullable types can be used for “ord<strong>in</strong>ary” properties of objects—caseswhere we just naturally might not have a value for some particular aspect that is stillbest expressed with a value type. Those are the more obvious uses for nullable typesand <strong>in</strong>deed the most common ones. However, a few patterns aren’t as obvious but canstill be powerful when you’re used to them. We’ll explore two of these patterns <strong>in</strong> ournext section. This is more for the sake of <strong>in</strong>terest than as part of learn<strong>in</strong>g about thebehavior of nullable types themselves—you now have all the tools you need to usethem <strong>in</strong> your own code. If you’re <strong>in</strong>terested <strong>in</strong> quirky ideas and perhaps try<strong>in</strong>g someth<strong>in</strong>gnew, however, read on…4.4 Novel uses of nullable typesBefore nullable types became a reality, I saw lots of people effectively ask<strong>in</strong>g for them,usually related to database access. That’s not the only use they can be put to, however.The patterns presented <strong>in</strong> this section are slightly unconventional but can make codesimpler. If you only ever stick to “normal” idioms of <strong>C#</strong>, that’s absolutely f<strong>in</strong>e—this sectionmight not be for you, and I have a lot of sympathy with that po<strong>in</strong>t of view. I usuallyprefer simple code over code that is “clever”—but if a whole pattern provides benefitswhen it’s known, that sometimes makes the pattern worth learn<strong>in</strong>g. Whether or notyou use these techniques is of course entirely up to you—but you may f<strong>in</strong>d that theysuggest other ideas to use elsewhere <strong>in</strong> your code. Without further ado, let’s start withan alternative to the TryXXX pattern mentioned <strong>in</strong> section 3.2.6.4.4.1 Try<strong>in</strong>g an operation without us<strong>in</strong>g output parametersThe pattern of us<strong>in</strong>g a return value to say whether or not an operation worked, andan output parameter to return the real result, is becom<strong>in</strong>g an <strong>in</strong>creas<strong>in</strong>gly commonone <strong>in</strong> the .NET Framework. I have no issues with the aims—the idea that somemethods are likely to fail to perform their primary purpose <strong>in</strong> nonexceptional circumstancesis common sense. My one problem with it is that I’m just not a huge fanof output parameters. There’s someth<strong>in</strong>g slightly clumsy about the syntax of declar<strong>in</strong>ga variable on one l<strong>in</strong>e, then immediately us<strong>in</strong>g it as an output parameter.Methods return<strong>in</strong>g reference types have often used a pattern of return<strong>in</strong>g nullon failure and non-null on success. It doesn’t work so well when null is a valid returnvalue <strong>in</strong> the success case. Hashtable is an example of both of these statements, <strong>in</strong> aLicensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!