Best way to set all applicable flags?
I have a string that represent possible flags. How can I set all the flags that are in the string?
This works but is there a better way?
9 Replies
can you change the format of the string
Enum.Parse<Classifiers>("CTR,HI,LAG")
I supposed I could change the format of the string, It might also include other information not needed by the classifier though
what's the input look like?
This is horrible, why would you do this?
If you want to set all the flags, just apply them normally.
Classifiers c = LO | HI | LFT | RIT | CTR | LAG;
People also usually add a flag to the enum that specifies groups of flags, or all flags.
Doing weird string parsing is unnecessary complication and just straight up confusing. This is also a very poor and slow solution.
Also, this is your solution regardless if you remove this parsing system. You can then pass "ALL" to the string and parse all of them.I suspect they are trying to parse it out in some larger piece of text
string.Contains is, 90% of a code smell to me. It's almost never correct, since a lot of text "cotains" substrings unexpectedly. Like in this example, "LO" and "HI" are going to show up in a LOT of strings. Like is "CTRIT" supposed to be both "CTR" an "RIT"? I strongly suspect no.
And I've seen it used for things like ".Contains(googl.com)", or the like in URL's. But "my.site.evil/#google.com" contains it.
You should already know what piece of string you are looking at is supposed to contain, then it either "is" that or not. No contains.
I assume that the whole point is to have a concatenated string of the flags, meaning that it doesn't contain bloat like other text
Not that it matters, it's still horrible and inefficient. I doubt they are familiar with how to use flags properly
You should already know what piece of string you are looking at is supposed to contain, then it either "is" that or not. No contains.Also this, it doesn't really matter. They should not use a string, period.
This is how I get the json
I didn't chose to put it all in a string with a bunch of other random crap
What is this JSON? Can you change how it returns the data? Then just return an integer that represents the flag
This still is excessive overcomplication. If you get this JSON from somebody else then they really need to improve it.