Tuesday, November 25, 2014

Sure, throw a null-coalescing operator into an intro tutorial on MVC EF


"
int pageSize = 3;
int pageNumber = (page ?? 1);
return View(students.ToPagedList(pageNumber, pageSize));
The ToPagedList method takes a page number. The two question marks represent the null-coalescing operator. The null-coalescing operator defines a default value for a nullable type; the expression (page ?? 1) means return the value of page if it has a value, or return 1 if page is null.
"

Can't be surprised since he loves the Ternary Operator.



Some links on Ternary Operators:

http://stackoverflow.com/questions/694814/ternary-operator-bad-or-best-practice

http://programmers.stackexchange.com/questions/28314/ternary-operator-considered-harmful

http://thedailywtf.com/articles/One-Bad-Ternary-Operator-Deserves-Another

https://agiletribe.wordpress.com/2011/11/01/21-avoid-ternary-conditional-operator/

http://discuss.fogcreek.com/joelonsoftware/default.asp?cmd=show&ixpost=142517

http://en.wikipedia.org/wiki/Talk%3A%3F%3A
"I don't know how to do so, but this page should be flagged as being a very biased viewpoint. The text runs counter to my experience of 20+ years as a professional software programmer. The only people who prefer the ternary conditional operator to the more readable if-then-else format (when both are allowed by the language syntax) are those people who have never had to maintain other people's code bases for any significant period of time. This opinion is found in industry standard texts, such as "Code Complete" by Steve McConnell. I would love to see a valid citation supporting the viewpoint presented here, i.e. that the ternary operator is NOT considered to be antiquated, harder to debug, and harder to maintain."

No comments: