Fair warning, I'm a self-taught Python amateur, but I don't expect Python-only solutions and want to hear good strategies from any language. Just try to dumb it down for me.
I've seen 2 main suggestions for fixing the infamously long if-else statements in the code of the game Yandere Simulator (if you know, you know):
- enum & switch: Simple if-else comparisons can be changed to constant flags labeled by an enum. A switch statement checks constant values more efficiently than a sequential if-else chain.
- class polymorphism: Instead of 1 method with a giant if-else chain checking an object for what is essentially its class, make classes with their own methods.
I'm not entirely satisfied with these suggestions for the following reasons:
-
A) Just changing a possibly nested if-else to a switch statement can make it slightly more readable, but it's still a long conditional, possibly longer.
B) Calculating and storing every condition as a flag somewhere may not be feasible or readable, like a condition that is a combination of field values
-
Though polymorphism can actually structure the code, not every condition should be a class. For example, professions like
TeacherandStudentare reasonable classes, maybe subclasses ofPerson. However, aPerson's eye color and height should be fields, not their own classes.
So, assuming I've done all these suggestions as much as possible and am still stuck with long conditional statements checking combinations of field values, what else can be done without changing the code's behavior?
Aucun commentaire:
Enregistrer un commentaire