❔ alternative to switch statement or if statements?
is there something better I can use in this situation?
21 Replies
no not really
sad. It's kinda a gross and obnoxiously long statement. Maybe I'll just extract it to its own function to reduce the size of the method it's in
yeah definitely do that
@mcacutt maybe a tenary statement?
Not really if you assign multiple values and three cases can get messy
Also not the best because of the size assignment but you could do
(address, int size) = HLevel.CurrentLevelId switch
works by the way
just in case you wanna inline the size declarationOr just turn the addresses into an array so no need for a switch
A dictionary?
address = HOpal.adresses[HLevel.CurrentLevelId]
Splitting "separate" bits of logic into methods with meaningful names should be done anyway, btw 😛
just an array, the keys, in this case being member names already follow a number sequence
and add an if special case for size on 0
Arrays "because they're numbers" is not automatically a good idea, it depends on what the keys are
Unless there are cases there some levels use the same address
Or there's a huge number of levels with a lot of overlap to not justify an huge array
and if one of them is like int.maxvalue?
Then a dictionary of ids and addresses
If the ids aren't sequential
So what I said, then.
But only if
Dictionary works but might be overkill
No, the dictionary is not "overkill", what does that even mean
Might end up with duplicate sequential ids
The dictionary is the data structure of choice when you're mapping stuff from one thing to another and there's a simple equivalence. It's an ID.
Anyway, amio out
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.