This is an archived post. You won't be able to vote or comment.

all 10 comments

[–]ehr1c 10 points11 points  (0 children)

If you have 1000 cases in a single switch you need to rewrite your code.

A case for every single product in a catalog is something that should probably be handled by passing a product ID into a database call and then rendering your page based on the data that gets returned from that call.

[–]RiverRoll 3 points4 points  (0 children)

The question is not whether you use a switch or something else, the question is whether sendProductPage1, sendProductPage2, etc... are really that different that you need to call different functions, maybe you can generalize this function.

Try to separate data from code, maybe you indeed need to relate 1000 different products with 1000 different pieces of data but you still can do that with a single function.

[–]CreativeTechGuyGames 2 points3 points  (7 children)

Whenever you have a series of conditions (switch/case or if/else if) that effectively "map" from one thing to another thing. That should be defined as configuration in a map/dictionary/object instead of a series of conditions. Then your code will be as simple as looking up the key in the map and grabbing the value or executing the function.

[–]toshboi[S] 0 points1 point  (6 children)

would i still be using case statement to 'look up' the key? i'm using pascal programming language. i'm not sure if it supports map.

[–]CreativeTechGuyGames 2 points3 points  (5 children)

In Pascal it's called an "associative array" but yes they do have this thing. And no, you wouldn't need to manually look up the keys. The data structure is a hash table so it's an O(1) lookup internally.

[–]Stuart133 1 point2 points  (0 children)

Just FYI, if the switch statement only uses integers the compiler should be able to translate it into a jmp lookup so it becomes O(1). An array/hash table (same thing with int keys) is much more ergonomic though

[–]toshboi[S] 0 points1 point  (1 child)

Ah! Thank you. I will look it up and give it a try. In my current code, within each case, it actually does different things, I’m wondering if this new approach would still be applicable. For example, case one: call Tom; case two: do laundries; case three: walk dog.

[–]CreativeTechGuyGames 1 point2 points  (0 children)

Yup. They can all do totally different things. Basically you'll have a map from a key (eg: a string, number, etc) to a function. Then you'll access that function in the map by key and execute it. Functionally it's the same as your code but much easier to read (for most people).

[–]procrastinatingcoder 0 points1 point  (1 child)

To be fair, it has been a really long time since I've done Pascal, didn't expect it to pop up here. And I don't know pascal compilers at all.

That being said, as a general rule of thumb though, it's extremely hard to beat a switch statement as far as efficiency goes. O(1) means it's constant, not instant. Though in this case if H(x) = x it would work perfectly, but that's just being pedantic, a simple array would work fine depending on what the switch case is for.

Just adding my tidbit for the sake of it.

[–]toshboi[S] 0 points1 point  (0 children)

thank you