Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simplify if condition?

I have this code:

int someValue = 100;
if (x == 5)
{
    if (someCondition)
    {
        return someValue;
    }
    return someValue / 12;
}

if (x == 6)
{
     if (someCondition)
     {
         return someValue * 12;
     }
    return someValue;
}

As you see, someCondition is always the same, just the returned value differs. Is there a way to simplify this some more?

like image 602
grady Avatar asked Nov 29 '22 03:11

grady


1 Answers

Let's see, what do you think of this?

int someValue = 100;

if (x == 5)
 return someCondition ? someValue : (someValue / 12);
else if (x == 6)
 return someCondition ? (someValue * 12) : someValue;
like image 195
Bazzz Avatar answered Dec 09 '22 17:12

Bazzz