Given the following basic class:
public class MyClass
{
private object m_memberVariable;
public MyClass()
{
this.m_memberVariable = new object();
}
}
Which of the following implementations of the test
method is better programming practice?
private void test()
{
if(this.m_memberVariable == null)
{
return;
}
System.Diagnostics.Debug.Write(this.m_memberVariable.ToString());
}
Or
private void test()
{
if(this.m_memberVariable != null)
{
System.Diagnostics.Debug.Write(this.m_memberVariable.ToString());
}
}
In trying to recollect from a professor some 10 years ago, best practice for an if statement is to have the condition that is likely to be true more often than false (the latter). However, recently I have been using a trial of ReSharper, and their suggestion is the former. The m_memberVariable
member is initialized at instantiation of the class and will likely be non-null for the life of the class.