Dictionary instead of switch or if statements
Conditions are a basic concept of programming but best practices suggest to avoid excessive usage of if statements. They say that if conditions make more complex and could buggy our applications because too many if-else can cause large numbers of condition branches. We cannot fully remove if statements, but we can reduce their usage.
Do you need to map datas from A to B without more logic? Is it a simple key/value mapping? Set up a dictionary data structure instead of switch or if-else ladder.
Suppose we need to manage the enum of Pokemon types and map for some reason each type to a base hit point value. Let's write using switch, if-else and dictionary structure, with some semplifications:
As you can see, without any kind of explanation, in term of readability and compactness, dictionaries are the first choice, I wrote less lines, code is clean, easy to refactor. Could be the data structure to consider in these type of simple mappings, remember the KISS principle, you need to avoid a large number of cases in switch/if statements because they are less maintainable.
[EDIT] I have created a repository on this topic, Github repo: https://github.com/apulito/dictionary-instead-statements-v1
Hope this gives some inspiration!