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 types133else{Console.WriteL<strong>in</strong>e ("Couldn't parse");}You may well th<strong>in</strong>k there’s very little to dist<strong>in</strong>guish the two versions here—they’re thesame number of l<strong>in</strong>es, after all. However, I believe there’s a difference <strong>in</strong> emphasis. Thenullable version encapsulates the natural return value and the success or failure <strong>in</strong>to as<strong>in</strong>gle variable. It also separates the “do<strong>in</strong>g” from the “test<strong>in</strong>g,” which puts the emphasis<strong>in</strong> the right place <strong>in</strong> my op<strong>in</strong>ion. Usually, if I call a method <strong>in</strong> the condition part of anif statement, that method’s primary purpose is to return a Boolean value. Here, thereturn value is <strong>in</strong> some ways less important than the output parameter. When you’reread<strong>in</strong>g code, it’s easy to miss an output parameter <strong>in</strong> a method call and be left wonder<strong>in</strong>gwhat’s actually do<strong>in</strong>g all the work and magically giv<strong>in</strong>g the answer. With the nullableversion, this is more explicit—the result of the method has all the <strong>in</strong>formationwe’re <strong>in</strong>terested <strong>in</strong>. I’ve used this technique <strong>in</strong> a number of places (often with rathermore method parameters, at which po<strong>in</strong>t output parameters become even harder tospot) and believe it has improved the general feel of the code. Of course, this only worksfor value types.Another advantage of this pattern is that it can be used <strong>in</strong> conjunction with thenull coalesc<strong>in</strong>g operator—you can try to understand several pieces of <strong>in</strong>put, stopp<strong>in</strong>gat the first valid one. The normal TryXXX pattern allows this us<strong>in</strong>g the short-circuit<strong>in</strong>goperators, but the mean<strong>in</strong>g isn’t nearly as clear when you use the same variable fortwo different output parameters <strong>in</strong> the same statement.The next pattern is an answer to a specific pa<strong>in</strong> po<strong>in</strong>t—the irritation and fluff thatcan be present when writ<strong>in</strong>g multitiered comparisons.4.4.2 Pa<strong>in</strong>less comparisons with the null coalesc<strong>in</strong>g operatorI suspect you dislike writ<strong>in</strong>g the same code over and over aga<strong>in</strong> as much as I do. Refactor<strong>in</strong>gcan often get rid of duplication, but there are some cases that resist refactor<strong>in</strong>gsurpris<strong>in</strong>gly effectively. Code for Equals and Compare often falls firmly <strong>in</strong>to this category<strong>in</strong> my experience.Suppose you are writ<strong>in</strong>g an e-commerce site and have a list of products. You maywish to sort them by popularity (descend<strong>in</strong>g), then price, then name—so that the fivestar-ratedproducts come first, but the cheapest with<strong>in</strong> those come before the moreexpensive ones. If there are multiple products with the same price, products beg<strong>in</strong>n<strong>in</strong>gwith A are listed before products beg<strong>in</strong>n<strong>in</strong>g with B. This isn’t a problem specificto e-commerce sites—sort<strong>in</strong>g data by multiple criteria is a fairly common requirement<strong>in</strong> comput<strong>in</strong>g.Assum<strong>in</strong>g we have a suitable Product type, we can write the comparison with codelike this <strong>in</strong> <strong>C#</strong> 1:public <strong>in</strong>t Compare(Product first, Product second){// Reverse comparison of popularity to sort descend<strong>in</strong>g<strong>in</strong>t ret = second.Popularity.CompareTo(first.Popularity);Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!