0

take a look at the following code :

if( down == null || down.GetFace() != Face.None || down.GetFace() != Face.Partial )
{  
    // I called GetFace() two times
    // How can i avoid to call it two times, and still putting inside that if
}  

Thank you!

1
  • It better to ask the question as part of the question body and not as a comment within your code sample. People could miss it. Commented Oct 24, 2011 at 9:16

2 Answers 2

2

To be more maintainable and expressive first separate the null check as exceptional case

then get the result to a variable and check it.

if(down == null)
  // Some exceptional case .. return or throw exception

var result =  down.GetFace();
if(result == Face.None || result != Face.Parial)
  // Do your code here
Sign up to request clarification or add additional context in comments.

Comments

1

Refactor to a method:

private bool IsFaceNoneOrPartial(Down down)
{
   var face = down.GetFace();

   return face != Face.None || face != Face.Partial;
}

// Your code is now:
if( down == null || IsFaceNoneOrPartial(down))
{

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.