I want a better performing code. I have used so many if-else in my code. I want to reduce the lines of code and increase the performance. Please suggest.
if-else case or switch case or ternary operator which one to use if i am having 10 string conditions
Asked
Active
Viewed 81 times
-6
-
6Well we'd have to see the code to be able to give any advice. Although if you have ten if else cases, I would highly recommend not using a nested ternary operator for that – GBlodgett Jan 26 '19 at 18:05
-
"better performing code" and "reduce the lines of code" are not the same – Sharon Ben Asher Jan 26 '19 at 18:13
-
Possible duplicate of [Is there any significant difference between using if/else and switch-case in C#?](https://stackoverflow.com/questions/395618/is-there-any-significant-difference-between-using-if-else-and-switch-case-in-c) – Aniket Kariya Jan 26 '19 at 18:14
-
1If "reduce the lines of code" is your goal, you're doing it wrong. Reducing the number of lines of code just to reduce the number of lines of code should **NEVER** be a goal. – Andrew Henle Jan 26 '19 at 18:15
-
You seem to know about `switch`, so why not give that a try? – VGR Jan 26 '19 at 20:54
-
@AniketKariya Closing a Java question as a duplicate of a C# question is not appropriate. – Mark Rotteveel Jan 28 '19 at 10:49
1 Answers
0
The performance it's almost the same in all cases. What you have to look it's for legibility, and with so many conditions you will be better with an switch statement.

Agus Achille
- 48
- 4