Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I was just writing an if statement with fairly long property names and came occross this problem.

Let say we have an if statement like this:

if(_someViewModelNameThatIsLong.AnotherPropertyINeedToCheck == someValue &&
   !_someViewModelNameThatIsLong.ThisIsABooleanPropertyThatIsImportant)
{
    //Do something
}

The second property is of a boolean type and it makes no sense to have the stetement like

if(boleanValue == true)

Is there a better way to emphasize the negation then to put the ! in front. To me it seems like this can easily be overseen when reading the code and may potentialy cause problems with debuging

share|improve this question
7  
Extract the condition into a method with a meaningful name. –  Joachim Sauer Jan 21 '13 at 11:20
2  
...or assign the negated value to a variable with a meaningful name and use it in the if condition instead. –  scrwtp Jan 21 '13 at 11:22
    
+1 what @JoachimSauer said, if possible put that method on the object that is being queried.. In this case the whole condition could be encapsulated with a method on _someViewModelNameThatIsLong –  MattDavey Jan 21 '13 at 11:51
2  
A general thing I often do is to surround the negation with a space on each side to make it more visible. if( ! something) vs if(!something) –  Svish Jan 21 '13 at 14:44
    
If you're emphasizing negation, why not use ... && model.Prop == false)? Personally I very rarely use !, it's too easy to overlook. –  Jon of All Trades Jan 21 '13 at 17:49
add comment

4 Answers

up vote 21 down vote accepted
if(_someViewModelNameThatIsLong.NeedsMeToDoSomething(someValue))
{
    //Do something
}

And then, in the view model object

public bool NeedsMeToDoSomething(string someValue)
{
    return AnotherPropertyINeedToCheck == someValue &&
        !ThisIsABooleanPropertyThatIsImportant;
}

(assuming someValue is a string and isn't known by the model object)

This not only emphasises the ! operator, but it makes it more readable generally. Now, in the calling method, I can see one condition, which should be well-named to describe the condition in the context of the calling object. And in the model object, I can see what that means in the context of the model object.

share|improve this answer
1  
+1, this is a good application of the information expert principle. –  MattDavey Jan 21 '13 at 11:53
1  
Worth noting, someValue may need to be a parameter to the NeedsMeToDoSomething method, depending on your situation. –  MattDavey Jan 21 '13 at 11:55
    
@MattDavey: Oh, good point. –  pdr Jan 21 '13 at 12:06
    
@pdr Maybe you should make a property to that view model with the same boolean and reuse that flag in more places. I'm sure you will find it handy. –  Floradu88 Jan 22 '13 at 6:33
add comment

Put it in its own if block prior to evaluating the less important conditions. Not only would it be easier to read without the clutter of the other conditions, but it's also the first condition a programmer will read. Combine this with the idea already mentioned by @scrwtp to assign to a variable with a meaningful name and you get:

var isValid = !_someViewModelNameThatIsLong.ThisIsABooleanPropertyThatIsImportant;
if( isValid ) 
{
    if( _someViewModelNameThatIsLong.AnotherPropertyINeedToCheck == someValue ) 
    {
        //Do something
    }
}

If you're programming in a compiler language, most times these nested if blocks get combined in the end anyway, so long as you don't insert code between the outer if and the inner if, so it shouldn't affect performance in these cases.

share|improve this answer
add comment

If you are using C/C++ then the preprocessor could provide readability.

#define NOT !

if(_someViewModelNameThatIsLong.AnotherPropertyINeedToCheck == someValue &&
    NOT _someViewModelNameThatIsLong.ThisIsABooleanPropertyThatIsImportant)
{
    //Do something
}
share|improve this answer
    
or perhaps... function not( x ) { return !x; } // javascript –  Alex Jan 21 '13 at 21:06
2  
Operators in C and C++ There are already standard synonyms for various operators. –  Joel Jan 21 '13 at 21:44
add comment

I'd just extract

`!_someViewModelNameThatIsLong.ThisIsABooleanPropertyThatIsImportant`

In a Method that returns this. If you name this method NotThisIsABooleanPropertyThatIsImportant you should be fine.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.