3 ms·
I mean, they're both useful for different purposes. When you're dealing with 4 possible values, each of which will result in wildly different code (e.g. evalua
by crazygringo 8y ago
I mean, they're both useful for different purposes.
When you're dealing with 4 possible values, each of which will result in wildly different code (e.g. evaluating the value of a options variable, or handling error codes that mean very different things), then switch is clearly the way to go.
When you're dealing with 20 or 200 different values, all of which map to a few similar variations, then defining an object or array lookup is clearly preferable.
"Preferring" objects over switch statements is like saying you prefer bitmaps over vector drawings -- it's nonsensical. Different tools are better for different jobs.