4 ms·
I use the "other" technique when it's necessary for the user to be able to mix in their own: enum WidgetFlavor { Vanilla, Chocolate,
by kstenerud 2y ago
I use the "other" technique when it's necessary for the user to be able to mix in their own:
enum WidgetFlavor
{
Vanilla,
Chocolate,
Strawberry,
Other=10000,
};
Now users can add their own (and are also responsible for making sure it works in all APIs):
enum CustomWidgetFlavor
{
RockyRoad=Other,
GroovyGrape,
Cola,
};
And now you can amend the enum without breaking the client:
enum WidgetFlavor
{
Vanilla,
Chocolate,
Strawberry,
Mint,
Other=10000,
};
- qingcharles 2y agoIt's code like this that ends in a terminal choco-banana shake hang: http://www.technofileonline.com/texts/chocobanana.gif http://www.technofileonline.com/texts/chocobanana.gif
- fingerlocks 2y agoWhat is the context here? Is this just a silly nonsense tech support page/meme or an actual product from the late 90s?
- sandblast 2y agoIs the context not clear for you from the information that the article applies to "DreamWorks Interactive, Someone's in the Kitchen, version 1.0" and other clues that it's a game?
- qingcharles 2y agoIt's a real Knowledge Base article from Microsoft. I can't find the original, but it is on archive.org somewhere if you dig around.
- dominicrose 2y ago"Other" doesn't mean the same thing before and after Cola has been added. "Unknown" would be more accurate. My personal opinion would be to make the enum nullable and not add a fake value.
- mewpmewp2 2y agoBut null value seems to have the intent that it doesn't have category at all. So, let's say there's a function createFruit(fruitType: FruitTypeEnum); If it's null, it seems wrong since it seems to mean that you have a fruit without type. If it's unknown, then it might also be incorrect, since you very well know the type, it just isn't handled there. So I'm wondering if best might be something like Unhandled, Unspecified or Unlisted.
- dominicrose 2y agoI guess if you know the type but it's not in the enum, then you can save it somewhere else. If the enum changes afterwards, that data can be used to update the enum value.
- kstenerud 2y agoIt's not about "other" having a meaning unto itself; rather it's about having a placeholder in the enum "space" (for older languages that implement it as an integer) so that customers can introduce their own variants. My library would then happily accept any value passed in, but only act upon the enum values that the library itself specifies. The customer would be entirely responsible for making sure their own variants behave well. This is not a common thing to do, of course, but when your customers are clamoring for what would actually be a useful feature for your product, even this somewhat ugly hack is a lot better than saying "no".