Enums are for wimps
-
Once upon a time, the concept of enumeration had not yet been invented. Instead, you had to use long lists of const definitions mapping some name to some int. So I found a colleague writing code like:
private int DecodeElementType(string elementName)
{
switch (elementName)
{
case "Blah": return 1;
case "Blub": return 2;
case "Hmpf": return 3;
case "Grml": return 4;
case "Tralala": return 5;
default: return 0; // unknown
}
}I asked him if he knew what an enum is, and how to use Enum.Parse / Enum.TryParse. He said he knew that. But his code is superior...
Implicit enums are ever worse.
public enum Implicit
AA_0
BB_1
CC_2
DD_3
EE_4
GG_6
HH_22
II_7
LL_17
...
end enumGuess what value did HH_22 have? And GG_6? BTW the numbers in the end are useful since this enum maps fixed alarm codes that come from a PLC. The only reason the code worked was because the same creator of this enum didn't use its values but used magic numbers all over the code. Half a day hunting for a bug because alarm 31 never matched with '== XX_31'. XX_31 equalled 30.
* CALL APOGEE, SAY AARDWOLF * GCS d--- s-/++ a- C++++ U+++ P- L- E-- W++ N++ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t++ 5? X R++ tv-- b+ DI+++ D++ G e++>+++ h--- ++>+++ y+++* Weapons extension: ma- k++ F+2 X * Never pay more than 20 bucks for a computer game. * I'm a puny punmaker.
-
Please use some other display method. As an engineer, I am "expression-blind" :sigh: (see [Engineers are cold and dead inside](https://www.codeproject.com/Messages/5364557/Engineers-are-cold-and-dead-inside.aspx))
If you have an important point to make, don't try to be subtle or clever. Use a pile driver. Hit the point once. Then come back and hit it again. Then hit it a third time - a tremendous whack. --Winston Churchill
:laugh:
Sudden Sun Death Syndrome (SSDS) is a very real concern which we should be raising awareness of. 156 billion suns die every year before they're just 1 billion years old. While the military are doing their part, it simply isn't enough to make the amount of nukes needed to save those poor stars. - TWI2T3D (Reddit)
-
Implicit enums are ever worse.
public enum Implicit
AA_0
BB_1
CC_2
DD_3
EE_4
GG_6
HH_22
II_7
LL_17
...
end enumGuess what value did HH_22 have? And GG_6? BTW the numbers in the end are useful since this enum maps fixed alarm codes that come from a PLC. The only reason the code worked was because the same creator of this enum didn't use its values but used magic numbers all over the code. Half a day hunting for a bug because alarm 31 never matched with '== XX_31'. XX_31 equalled 30.
* CALL APOGEE, SAY AARDWOLF * GCS d--- s-/++ a- C++++ U+++ P- L- E-- W++ N++ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t++ 5? X R++ tv-- b+ DI+++ D++ G e++>+++ h--- ++>+++ y+++* Weapons extension: ma- k++ F+2 X * Never pay more than 20 bucks for a computer game. * I'm a puny punmaker.
-
Sounds like a case of the wrong tool for the job. Implicit enums are great for representing a fixed set of ideas/behavior (like flags) that isn't mapped to specific values. If you need a fixed set mapped to specific values use explicit enums :doh:
Precisely. Even if I'm used to always explicit the enums - mainly because I work with a system that has a lot of marshalling and communications between different paradigms so more often than not I end up having to fix those values sooner or later.
* CALL APOGEE, SAY AARDWOLF * GCS d--- s-/++ a- C++++ U+++ P- L- E-- W++ N++ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t++ 5? X R++ tv-- b+ DI+++ D++ G e++>+++ h--- ++>+++ y+++* Weapons extension: ma- k++ F+2 X * Never pay more than 20 bucks for a computer game. * I'm a puny punmaker.
-
Sooooo, when I pass in TRalala to his code, it's still going to be superior. :rolleyes:
This space for rent
You're going to get 0, unless you mean "Tralala".
-
Once upon a time, the concept of enumeration had not yet been invented. Instead, you had to use long lists of const definitions mapping some name to some int. So I found a colleague writing code like:
private int DecodeElementType(string elementName)
{
switch (elementName)
{
case "Blah": return 1;
case "Blub": return 2;
case "Hmpf": return 3;
case "Grml": return 4;
case "Tralala": return 5;
default: return 0; // unknown
}
}I asked him if he knew what an enum is, and how to use Enum.Parse / Enum.TryParse. He said he knew that. But his code is superior...
-
My guess, he didn't know. Self-tough programming. What so superior about coding manually. The point of using enumeration is to allow the compiler to check for potential human errors. Tell him to go back to school.
-
Once upon a time, the concept of enumeration had not yet been invented. Instead, you had to use long lists of const definitions mapping some name to some int. So I found a colleague writing code like:
private int DecodeElementType(string elementName)
{
switch (elementName)
{
case "Blah": return 1;
case "Blub": return 2;
case "Hmpf": return 3;
case "Grml": return 4;
case "Tralala": return 5;
default: return 0; // unknown
}
}I asked him if he knew what an enum is, and how to use Enum.Parse / Enum.TryParse. He said he knew that. But his code is superior...
Whats the language that can use a string in a switch statement?
-
Whats the language that can use a string in a switch statement?
I think the snippet is in C#, which does allow that.
What do you get when you cross a joke with a rhetorical question? The metaphorical solid rear-end expulsions have impacted the metaphorical motorized bladed rotating air movement mechanism. Do questions with multiple question marks annoy you???
-
I think the snippet is in C#, which does allow that.
What do you get when you cross a joke with a rhetorical question? The metaphorical solid rear-end expulsions have impacted the metaphorical motorized bladed rotating air movement mechanism. Do questions with multiple question marks annoy you???
OK, makes sense. C++ allows for a lot of this kind of thing through operator overloading, though I tend to be a pure C programmer, and I like the level of clunky direct control you have of the code with C. :)