Take the 2-minute tour ×
Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. It's 100% free, no registration required.

I have a class "Data" and a subclass "SubData". I want to access a method in SubData.

In order to do something like:

value = Data.SubData.getValue();

I need to make sure that Data!=null and also that SubData!=null.

I would usually do this:

if (Data!=null)
{
   if (Data.SubData!=null)
   {
       value=Data.SubData.Value();
   }
}

However when I want to print a message in case of Data or SubData being null, I need to put the message in two places:

if (Data!=null)
{
   if (Data.SubData!=null)
   {
       value=Data.SubData.Value();
   }
   else
   {
       // MESSAGE
   }  
}
else
{
    // MESSAGE
}

Is there a way of having //Message only in one place? Of course I could set a "showMessage flag" to true and then later have the message appear only in one place, but then I would still set the flag in two places.

share|improve this question

closed as off topic by dzieciou, user246, Sam Woods, bytebuster, Joe Strazzere Mar 11 '13 at 21:06

Questions on Software Quality Assurance & Testing Stack Exchange are expected to relate to software quality assurance or testing within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

2  
More like a standard programming question than a testing related one ? –  Phil Kirkham Dec 5 '12 at 14:57
    
Well, it is about good style, I think. Which stackexchange forum would you move it to? –  Sebastian Langer Dec 5 '12 at 14:59
1  
@Sebastian, try at codereview.stackexchange.com –  dzieciou Dec 6 '12 at 18:11
    
@dzieciou, thank you! Seems like the perfect place for a question like the one above. –  Sebastian Langer Dec 7 '12 at 12:20
add comment

1 Answer 1

up vote 4 down vote accepted

You could combine the not null tests into one if statement

if (Data != null || Data.SubData != null)
{
    //Do Whatever
}
else
{
    //Message Evaluation Example
    return Data = null ? "Data is null!" : "Data.SubData is null!";
}

Above I have made a check on both Data and Data.SubData to ensure they are not null, if they are however I use a Ternary Operator to figure out which is null and return the result as a string (don't know what data type you would like to return).

This assumes that both properties will never be null, if they are simply expand the evaluation code.

share|improve this answer
    
Can I be sure that Data!=null is tested for before Data.SubData!=null? It seems ambiguous to me. –  Sebastian Langer Dec 5 '12 at 15:25
    
Depends what you are after, but in my sample code yes you can be, as it would evaluate left to right. Short-circuiting In If Statement Evaluations, Post From MSDN Forums On If Statement Evaluation Order –  Tom Dec 5 '12 at 15:30
    
Thanks, this is basically what I was after. I just wasn't aware of the term "short circuiting". I'll accept your answer. –  Sebastian Langer Dec 5 '12 at 15:40
add comment

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