+ 2

Is it a good practice to use chained switch statements in c# ? If not, what alternative can there be ?

In the following code I have used chained switch statements and I wonder if it's a good practice to do so? https://code.sololearn.com/cMHkN062xa6H/?ref=app

15th Jun 2023, 1:08 PM
Lamron
Lamron - avatar
16 ответов
+ 4
In terms of rock-paper-scissors, there is a simple arithmetic solution. You might enjoy this one: https://therenegadecoder.com/code/rock-paper-scissors-using-modular-arithmetic/
15th Jun 2023, 3:58 PM
Lisa
Lisa - avatar
+ 3
Interisting solution, thanks Lisa . Haven't read through it but by the looks it's quite straightforward. Thanks for clarificationRuntime Terror . I haven't learned about enums in c# yet. Bob_Li what do you mean by lookup table? Although logical flow is the same in each programming language , I haven't seen many data structures like lists & dictionaries, appart from arrays. This is something that pulls me back at the moment and something for me to look up in the near future. I have a simple solution in python: https://code.sololearn.com/cGBLSH8vKrkx/?ref=app
15th Jun 2023, 4:46 PM
Lamron
Lamron - avatar
+ 3
Example in C# using an enum, and a two-dimensional array as decision table. string[] outcomes = {"Tie", "Player 1 wins", "Player 2 wins"}; int[,] judge = {{0, 2, 1}, {1, 0, 2}, {2, 1, 0}}; string result = outcomes[judge[(int)player1, (int)player2]]; https://code.sololearn.com/cOO4pVH1tKIn/?ref=app
15th Jun 2023, 5:53 PM
Tibor Santa
Tibor Santa - avatar
+ 3
modifying Tibor Santa's code to use the modulo math method. It turned out to be more complex than I expected, because C# % operator in not the same as Python % for negative values. https://code.sololearn.com/cin5iOhNCFCo/?ref=app
16th Jun 2023, 5:19 AM
Bob_Li
Bob_Li - avatar
+ 2
Lamron The way you used it here leads to lots of code repetition, so no, not good practice. Maybe a single switch block in a function which returns an int, so you could call it for both inputs, then add some logic to compare the ints.
15th Jun 2023, 2:51 PM
Emerson Prado
Emerson Prado - avatar
+ 2
No no, I don't think it's a good practice to use switch statement, I only use it with enums. An if-else or if-else if would be a good option and good practice, if you are working with number or any data types. A switch statement would be (probably) useful when you are working with enums.. So, I believe using switch statement in this code is not necessary.Try with some if-else (if) statement (or make the switch statement in your code a bit shorter then prevent repetitions), then add some logic.
15th Jun 2023, 3:07 PM
Dragon RB
Dragon RB - avatar
+ 2
Emerson Prado I thought about creating a function which returns win/lose/tie to a user based on argument. But then I would have to write something like: If((ComputerInput == UserInput) or (condition) or (condition)) I assumed that I would need to list all combinations 🤔 But yes, using a single switch statement and if statements is what I thought of. However, it would still cause code duplication. That's how I concluded to write a function and return a value... Dragon RB didn't get to enums yet but will do soon.
15th Jun 2023, 3:13 PM
Lamron
Lamron - avatar
+ 2
It's indeed a good practise and ive seen many team including mine using long chain of switch for professional softwares. But hold, Emerson is right, this actual code you show has redundancy and switch isn't the best case. The numbers ranges from 1-3 inclusive and I would suggest creating an enum, then a single function that writes to the console based on the chosen. Seems to be the job for an if-else if- else without chaining
15th Jun 2023, 4:14 PM
RuntimeTerror
RuntimeTerror - avatar
+ 2
a lookup table to determine the winner and a parametized output message would probably be simpler. then there is the case when the user inputs invalid values or no value at all.
15th Jun 2023, 4:39 PM
Bob_Li
Bob_Li - avatar
+ 2
Lamron your python code can easily be converted to c#
15th Jun 2023, 4:50 PM
RuntimeTerror
RuntimeTerror - avatar
+ 2
Lamron something like this: you just have to parse the input into the form of the key. ("1 1", "1 2", etc) https://code.sololearn.com/cFnvd498DQwL/?ref=app
15th Jun 2023, 4:54 PM
Bob_Li
Bob_Li - avatar
+ 2
Bob_Li , oh right, I see. As I said I just need to learn how to implement data structures in c#
15th Jun 2023, 4:56 PM
Lamron
Lamron - avatar
+ 2
they're worth the effort to study.
15th Jun 2023, 4:58 PM
Bob_Li
Bob_Li - avatar
+ 2
from Lisa's modulo method link, here is my Python version: https://code.sololearn.com/cDVdOU93Vjsl/?ref=app
16th Jun 2023, 3:36 AM
Bob_Li
Bob_Li - avatar
+ 2
Tibor Santa Bob_Li Thanks for examples
18th Jun 2023, 2:13 PM
Lamron
Lamron - avatar
+ 1
Runtime Terror yes, it can. I've been following an example on the internet which has used if statements and a single switch statement. Guess I don't need to follow bad examples and do what I think is right 🤣
15th Jun 2023, 4:53 PM
Lamron
Lamron - avatar