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 can see, someCondition is always the same, only the return value is different. Is there any way to simplify this yet?

+4
source share
10 answers

Let's see what you think about it?

 int someValue = 100; if (x == 5) return someCondition ? someValue : (someValue / 12); else if (x == 6) return someCondition ? (someValue * 12) : someValue; 
+7
source

It depends on what you mean by simplification ...

The following has fewer lines of code and does not sacrifice anything in terms of readability (IMO):

 var someValue = 100; switch (x) { case 5: return someCondition ? someValue : someValue / 12; case 6: return someCondition ? someValue * 12 : someValue; default: return someValue; } 
+4
source

You have different ways of processing the result of the same conditional statement. Because of this, it is probably more effective (and easier on the eyes) to maintain it as it is.

If your return expressions were the same for each conditional statement, then I would review the code, but each block of code has a different return value and because of this makes it unique.

No, there is no way to simplify what you have done.

+2
source

You have two variables ( x and someCondition ) and 3 separate results; so yes, you can do better than two pairs of two nested tests. The best you can do is:

 if (((x == 5) && someCondition)) || ((x == 6) && !someCondition))) { return someValue; } else if (x == 5) { return someValue / 12; } else if (x == 6) { return someValue * 12; } 
+2
source

This has no "nested" statements, so it looks cleaner to me:

 int someValue = 100; if ((x == 5 && someCondition) || (x == 6 && !someCondition)) return someValue; if (x == 5) return someValue / 12; if (x == 6) return someValue * 12; 
+2
source

You can switch if so that you only repeat the condition once. You can use switch to check the value of x :

 int someValue = 100; if (someCondition) { switch (x) { case 5: return someValue; case 6: return someValue * 12; } } else { switch (x) { case 5: return someValue / 12; case 6: return someValue; } } 
+1
source

Much of the fly really, how about

 Double scalar = 12; switch(x) { case 5 : scalar = 1f/12; break; case 6 : break; default : return 100; } if (someCondition) { return someValue; } return someValue * scalar; 
+1
source

One line of code:

 condition ? someValue * ((x == 6) ? 12 : 1) : someValue / ((x == 5) ? 12 : 1); 

It's simple? I think so.
Is it easy to read? In some ways.
It's good? I would not say that. (+ generated IL is slightly different)

+1
source
 switch(x) { case 5: return someCondition ? someValue : someValue / 12; case 6: return someCondition ? someValue * 12 : someValue; } 
+1
source

Personally, I would use a switch statement instead of two if statements. Or you can make a function in which you pass x * 12 or x / 12 ...

0
source

Source: https://habr.com/ru/post/1383428/


All Articles