Adrian Sud, Web Developer
I am primarily a Java developer. Like many Java developers, when I think about .NET, I’m inclined to think of it as Microsoft’s rip-off of Java; that Java is pretty much exactly the same, but more readily cross-platform. While that may have been true of at its inception, I’ve been working with C# recently and have noticed several pieces of syntactic sugar that are, well, just nice. Here are a few of the things that I have already grown to love, and that I wish were back in Java.
Null Logic and the Null Coalescing Operator
In C#, the primitive types can, with the annotation of a question mark, be wrapped in a Nullable type, to allow for the object to be null. It’s similar to using Object wrappers in Java with automatic unboxing, until you add Null to Boolean logic. The Nullable Types allow for this by effectively treating a Null value as “Maybe”, so that “null || true” returns true, and “null && true” returns null. While such cases can easily be programmed around, the concept is powerful, and allows you to think more clearly about what you’re code is modeling. If a method is asked a yes or no question, and it says “I don’t know”, we have learned something wholly new about the state of the program.
Just to complete their thorough treatment of “null” as a legitimate value, C# provides the Null Coalescing Operator, “??”. This is really just shorthand for a terniary form, selecting the left-hand value if it is non-null, or else the right-hand value. Still, it is an extremely common pattern, and produces complex assignments and tests without sacrificing conciseness or readability. Not a game changer, but it is a useful time-saver.
Jeremy Hilts, Web Developer
As a developer, I’m often presented with a number of problems in need of solving. Is this code reusable? Does this object do too much? Will my use of a ternary operator here over-complicate this section of code? Is this Paul, John or George? (I kid. I can tell them apart.)
Problem solving has always been my greatest ability. I not know for the spoken language good; however, if you give me a paperclip, a piece of chewed up bubblegum (classic flavor only, not your sour apple), and a rubber band, I could make you a CMS … or CRM … or POS. Perhaps, I’ll use a little XML with a some PHP on the back-end. Maybe toss in some SQL. These abbreviations doing anything for ya?