Unity Basics: Switch Statements to the Rescue
Code can always be optimized to be more efficient. Refactoring code is normal and even very necessary. As a beginner, the most important thing is to write code that solves a problem, and the next step is to optimize the code to make it readable and modular.
Take a look at this example of the powerup system in Space Shooter:
The -if- and -else if- statement does the job here, but with more elements, it will start to be quite difficult to read and maintain. If we use a switch statement here, our code will be much nicer ;)
It’s important to make constant code refactoring a habit, go back to your code and refactor it every time you develop, learn new things or just when looking at your code again you have an idea how to improve it.