Coalescing with ReSharper 3.0

The ReSharper 3.0 beta is out and I'm really digging it. It's little things that make my day a better place.

For example I had a piece of code that looked like this:

   14 public ErrorReportProxy(IWebProxy proxy, bool needProcessEvents)

   15 {

   16     _errorReport.Proxy = proxy;

   17     if (proxy.Credentials != null) _errorReport.Proxy.Credentials = proxy.Credentials;

   18     else _errorReport.Proxy.Credentials = CredentialCache.DefaultCredentials;

   19     _needProcessEvents = needProcessEvents;

   20 }

"if" statements are so 1990s so you can rewrite it like this using the conditional operator "?"  

   14 public ErrorReportProxy(IWebProxy proxy, bool needProcessEvents)

   15 {

   16     _errorReport.Proxy = proxy;

   17     _errorReport.Proxy.Credentials = (proxy.Credentials != null)

   18                                         ? proxy.Credentials

   19                                         : CredentialCache.DefaultCredentials;

   20     _needProcessEvents = needProcessEvents;

   21 }

However with nullable types in C# 2.0 you can use the language feature of using the "??" assignment (called the null coalesce operator). The ?? operator defines the default value to be returned when a nullable type is assigned to a non-nullable type. This shortens the code but makes it more readable (at least to me).

ReShaper to the rescue as it showed me squiggly lines in my code where I had the "?" operator and said (in a nice ReSharper way not an evil Clippy way):

'?:' expression could be re-written as '??' expression.

So a quick ALT+ENTER and the code becomes this:

   14 public ErrorReportProxy(IWebProxy proxy, bool needProcessEvents)

   15 {

   16     _errorReport.Proxy = proxy;

   17     _errorReport.Proxy.Credentials = proxy.Credentials ?? CredentialCache.DefaultCredentials;

   18     _needProcessEvents = needProcessEvents;

   19 }

Okay, so maybe I'm easily amused but it's the little things that make my day (and maybe yours) a little brighter. The coding assistance feature of ReSharper 3.0 is shaping up to be very useful indeed.

Published Wednesday, June 13, 2007 12:56 PM by Bil Simser
Filed under: ,

Comments

# re: Coalescing with ReSharper 3.0

Wednesday, June 13, 2007 6:50 PM by Ryan Ternier

Resharper has been impressing me a lot lateley... but be sides that...

GAHHH MY EYES MY EYES!

# re: Coalescing with ReSharper 3.0

Wednesday, June 13, 2007 8:40 PM by Tom Dean

Actually, the coalesce operator means return the first non-null value.

string t1 = null, t2 = null, t3 = "data", t4 = null;

string t5 = t1 ?? t2 ?? t3 ?? t4;

It's a very useful operator, but it's not always a replacement for a ternary.

Another really great feature of 3.0 is that you can do code formatting on an entire project / solution, instead of just one file at a time. And you can customize the rules for how it formats code!

# re: Coalescing with ReSharper 3.0

Wednesday, June 13, 2007 10:51 PM by Will Asrari
Downloading now! Thanks! It's all about ALT + ENTER and CTRL + ALT + O.

# re: Coalescing with ReSharper 3.0

Thursday, June 14, 2007 1:25 AM by Ilya Ryzhenkov

Bil,

Did you use context action on "if" to convert to ternary? It is another little helper :)

# re: Coalescing with ReSharper 3.0

Thursday, June 14, 2007 4:12 AM by Simone Busoli

"The ?? operator defines the default value to be returned when a nullable type is assigned to a non-nullable type."

It's not very clear in your words, but the ?? operator is not about nullable types actually, it just checks the left operand for nullness and returns it in case it's not null, otherwise it returns the right operand.

# re: Coalescing with ReSharper 3.0

Friday, June 15, 2007 12:25 AM by Bil Simser

@Simone: It's funny you quote that one line in my blog. That's actually from the MSDN documentation (I was looking for words to describe it).

# re: Coalescing with ReSharper 3.0

Friday, June 15, 2007 10:27 AM by Donn Felker

I've been using 3.0 for about a week now and I LOVE IT. I cant live without it ever again. I'm definately picking up a copy when it drops.

# re: Coalescing with ReSharper 3.0

Friday, June 15, 2007 2:14 PM by Simone Busoli
Maybe that's why so many people just don't get the meaning of the operator then ;)

Leave a Comment

(required) 
(required) 
(optional)
(required)