Round 1:
The smelly method has entered the ring and you can see Resharper sizing up his opponent. The bell rings, and the contenders meet in the center of the ring. Resharper has found a weakness in the code and throws a punch knocking unused variable 'i', and it's assignments out of the method. Ouch, that had to hurt.
public Models.MemberRecord FindMemberWithDetail(string uri)
{
MemberRecord memberRec = null;
int i;
if (string.IsNullOrEmpty(uri))
memberRec = null;
else
{
using (new SessionScope())
{
memberRec = Models.MemberRecord.FindUri(uri);
if (memberRec != null)
{
i = memberRec.ProfessionalInformationRecords.Count;
i = memberRec.EducationInformationRecords.Count;
i = memberRec.MemberWebSiteRecords.Count;
}
}
}
return memberRec;
}
The smelly method is looking battered and bruised. Resharper takes another swing and sends the newly empty if block flying, and follows up by removing the unnecessary else clause. What a one-two combo that was. The round is coming to a close, but Resharper manages to get in one more jab converting the null assignment to a guard statement.
public Models.MemberRecord FindMemberWithDetail(string uri)
{
MemberRecord memberRec = null;
if (string.IsNullOrEmpty(uri))
memberRec = null;
else
{
using (new SessionScope())
{
memberRec = Models.MemberRecord.FindUri(uri);
if (memberRec != null)
{
}
}
}
return memberRec;
}
DING DING DING
That wasn't even a close one. Resharper has pulvurized that smelly method. Looks like coach is going to remove the SessionScope() call to get rid of unnecessary weight.
Round 2:
Now that the SessionScope has been removed, the memberRec variable is no longer protected. Resharper quickly converts to return result of a method call making memberRec useless.
public Models.MemberRecord FindMemberWithDetail(string uri)
{
MemberRecord memberRec = null;
if (string.IsNullOrEmpty(uri))
return memberRec = null;
using (new SessionScope())
{
memberRec = Models.MemberRecord.FindUri(uri);
}
return memberRec;
}
Resharper sends all references to memberRec flying. The smelly code can't go on much longer, there are only a few lines left! Resharper reaches in deep and uses everything he has left to convert the if statement to a return statement. That's it, the smelly code falls to the ring floor.
public Models.MemberRecord FindMemberWithDetail(string uri)
{
MemberRecord memberRec = null;
if (string.IsNullOrEmpty(uri))
return memberRec = null;
}
Knock Out!
public Models.MemberRecord FindMemberWithDetail(string uri)
{
return string.IsNullOrEmpty(uri)
? null
: MemberRecord.FindUri(uri);
}
Resharper is a fantastic developer tool. The architecture of the system changed, allowing for the removal of the new SessionScope(), all other enhancements were due to Resharpers suggestions. That's a pretty fantastic reduction of code. Certainly, this is an extreme case, but that's why I chose to share ;)<\p>