Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should conditions test for positive or negative results?

Sorry if the title is rather ambiguous, I was not sure how to word it.

Is it better to phrase a condition such that the outcome you don't want enters the if statement then you exit the function or should I test for the outcome I do want and follow the statement with my code.

Maybe some examples would help:

What I mean by testing for negative result:

if(myObject == null) {
    return;
}

//do whatever with myObject

What I mean by testing for positive result:

if(myObject != null) {
    //do whatever with myObject
}

Sorry, if someone can word it better than me please do.

like image 947
Lerp Avatar asked Oct 10 '12 17:10

Lerp


People also ask

How long does the virus that causes COVID-19 last on surfaces?

Recent research evaluated the survival of the COVID-19 virus on different surfaces and reported that the virus can remain viable for up to 72 hours on plastic and stainless steel, up to four hours on copper, and up to 24 hours on cardboard.

Can asymptomatic people transmit COVID-19?

Yes, infected people can transmit the virus both when they have symptoms and when they don't have symptoms. This is why it is important that all people who are infected are identified by testing, isolated, and, depending on the severity of their disease, receive medical care.

Can COVID-19 spread through water while swimming?

Fact: Water or swimming does not transmit the COVID-19 virusThe COVID-19 virus does not transmit through water while swimming. However, the virus spreads between people when someone has close contact with an infected person. WHAT YOU CAN DO: Avoid crowds and maintain at least a 1-metre distance from others, even when you are swimming or at swimming areas. Wear a mask when you’re not in the water and you can’t stay distant. Clean your hands frequently, cover a cough or sneeze with a tissue or bent elbow, and stay home if you’re unwell.

Can COVID-19 be transmitted through food?

There is currently no evidence that people can catch COVID-19 from food. The virus that causes COVID-19 can be killed at temperatures similar to that of other known viruses and bacteria found in food.


1 Answers

I personally prefer the first method of checking if the object is null then immediately returning. It allows the "real code" to stay unindented, linear, and can prevent many nested if statements, which I find to be more readable. Otherwise, both ways are valid and will have the same outcome. Choose whichever works best in your situation (which can depend on any else or else if statements).

Here's a good example:

if (object1 == null) {
    return;
}
// do some stuff
if (object2 == null) {
    return;
}
// do some stuff
if (object3 == null) {
    return;
}

Opposed to:

if (object1 != null) {
    // do some stuff
    if (object2 != null) {
        // do some stuff
        if (object3 != null) {
            // do some stuff
        }
    }
}

I find the first one to be much more readable.

like image 98
monoxygen Avatar answered Sep 25 '22 12:09

monoxygen