Deeply Nested Null Checking in C# verses Assert with no nesting

One of the code smells that particularly bothers me (though I often find myself doing it anyhow) is when I defensively program against nulls in C# (though could be any other language).  That is, I do something like the following var rec = getRecord(..); if (rec != null) { var rec1 = getAnotherRecord(..); if (rec1 != null) { rec2 = getAThridRecord(..); if (rec2 != null)... The code gets ugly quick and the nesting does not help the readability, and if anything, hurts it. Today, while using a daily build of Resharper7, I noticed that when I asked resharper to do the null check for me, instead of doing the above, it did the following: var rec = getRecord(..); Debug.Assert(rec != null,"rec != … Continue Reading

A Cool / Unexpected Refactoring around .Net C# Locking Issue

I’m constantly amazed by the insightfulness of ReSharper’s suggested refactorings (ReSharper is a Visual Studio Addin from JetBrains I use with C#). Today, I’ve been working on a threading problem where I’m getting crashes based on what seems like not proper locking across threads (they usually show up as some type of ugly update object or enum error). My code starts like this:   public static List<DbProgressReport> DbProgressReportProperty { get; set; }   … Continue Reading

ReSharper 5.0’s LINQ Refactoring Continues to be Amazing!

  In this post, we’ll take a straight forward procedure based set of code and convert it to LINQ using a ReSharper from JetBrains suggestion.   I’ve found that in general, when I do things with foreach syntax, there is often a better way in Linq to do this.  The better does not jump out at me sometimes, however with ReSharper, it is often a button click away. … Continue Reading

Another Cool Resharper Refactoring

So, if you are like me, it’s easy to make mistakes on whether to use && or || when combining variables on if statements.  I hate to just start nesting if statements because the code gets bulky, but if not, I have to think three times about whether I got it right. So, Here comes Resharper for c# from Jetbrains.  Simply write the if statement nested as follows: … Continue Reading

Follow

Get every new post delivered to your Inbox

Join other followers: