Best practice to eliminate switch statements?
I have a terrible feeling this is bad:
How would you go about refactoring it. My basic thinking is to create a class which contains the string, the
apiReturn
enum, the Object type (how would you do this?) and something to help any function to call the right method (one of two: SingleDataObjectFromXDoc
or MultipleDataObjectsFromXDoc
)8 Replies
You could easily use a switch expression here
Or, sure, you could extract some of it to a method, even to a local function like
Like the first one - a bit tidier!
I was wondering whether there's a way to store the 'variables' in each switch statement in a constant and then just do the call via two lines. E.g. in psuedo-code:
bloody hell - should have just coded it directly to see if it works
No, you can't store generic parameters in a variable
will not work
I did wonder - being
T ObjectType {get; set;}
in this case?
or the MultipleDataObjectsFromXDoc<requestType.ObjectType>
?No, generic parameters cannot take variables
ah - so whichever way you cut it - this would need some kind of switch statement?
Thanks zzzzzzz - you're always so helpful
one thing you could do is use a switch expression instead, the syntax is more ergonomic for this kind of code
ree, chat jumped a mile ignore me
hate it when the post is just big enough to hide that someone already responded :KEKW:
🤣 No worries - thanks for chipping in!