Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

6
  • Maybe try a UML of your code to see if it's really accidental Commented Jun 13, 2020 at 11:47
  • 1
    If they're all events, why aren't you just having a single dispatch or run function on each of them and doing away with the switch statement altogether? Commented Jun 13, 2020 at 19:16
  • @corsiKa Do you mean to have a dispatch or run method on the event? To me that doesn't always make sense, the thing that creates the event shouldn't need to know how to handle the event, if you mean to add the run method on later, then still the events have to be inspected and switched on to determined what run method to add, which just moves the switch somewhere else. But in any case this was just supposed to be an example of the sort of situation I meant, nothing precise. Commented Jun 13, 2020 at 22:58
  • No there's no switch necessary. Consider Java's Runnable class. Commented Jun 14, 2020 at 1:12
  • 2
    Ah, I see what you mean - I interpretted you saying "you receive an event" to mean "you receive an event object" not "you receive an event string" - that being said, in Java I do use annotations and reflection to make direct calls in such a way, and I am sure you could do so in Javascript, although you would clearly need proper security on such things. For sure, a mapping object would be superior to a switch, and more maintainable, but I suppose such things do digress from the original question (slightly) Commented Jun 14, 2020 at 14:57