- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
No 42?
Admin
You blind, bruv?
Admin
Developer 745 was responsible for writing this snippet. He was birthed by alcoholic parents 623 and 679.
Admin
At least they are not using magic numbers, where nobody knows what they mean...
Admin
And that's what you get when some PHB insists that 'every line of code' should be commented
Admin
ZERO Overhead of using the Enum in code, compared to the integer, but protects against magic numbers and mitigates unapproved values. May make various serializations easier. Consider "EDI_810 = 810" as a topologically equivalent example.
Admin
And if referring to the XML comment templates... Run it through SandCastle (or equiv.), because that is what that format is for, not necessarily inline reading. [hint: look at nested indices and cross-reference capabilities.
Admin
No, the article isn't referring to the enum itself or the XML documentation. It's referring to the XML documentation being useless, in the sense that nothing in it couldn't be inferred from the names.
(That being said, I would disagree with using an enum here. To me, enums are appropriate when you have a well-defined set of values, and I don't think this qualifies... but hey, maybe it does; we don't know, because the documentation just restates the name.)
Tangent, but do .NET enums even protect against unexpected values? My understanding is that they're more like C++ enums than Java enums (not a criticism, just a question).
Admin
They do and they don't. You can convert an integer which doesn't have an option to the enum type; but you'd have to do it explicitly, and if you use them correctly, they do a good job preventing developers from making mistakes.
Unrelated, just wanted to post this article: https://www.theverge.com/2021/4/23/22399721/uk-post-office-software-bug-criminal-convictions-overturned
Admin
Not directly; as with C you could technically assign any value of the enum's base type. However, System.Enum does have several methods for working with enums that include validation, like TryParse and IsDefined. Also a lot of the tools, like Intellisense and Resharper, can tell you when you might not be using them correctly, for example checking to make sure you've included all the values in a switch statement.
Admin
No meaningful protection, no.
An enum in C# is basically just a wrapper for an integer. And whilst there is some sintaxic sugar to discourage you from assigning just any number to it the whole thing relies entirely on deterring you from doing something stupid rather than actually preventing you from doing so.
If you really want to do so you can just write "MyEnum foo = (MyEnum) some_int_not_in_the_enum" you absolutely can do that. And the program will both compile without error and run without exceptions at runtime.
Admin
This is what happens when you use some tool that verifies everything has a doc comment - and don't do anything to check that the comments are meaningful.
Admin
No, you can assign any value of the underlying enum type if you cast it to the enum. This is important for flag enums since you are going to be ORing flags together and getting total values not explicitly defined by the enum.
Of course there's FlagAttribute to mark enums like that.
There's also a case where you want to treat a value as a number but may have several special values. You could use consts to define these values, but using an enum ensures you can strongly type where these special values can be used.
Off the top of my head those are situations where it makes sense to allow that.
Admin
No, you can assign any value of the underlying enum type if you cast it to the enum. This is important for flag enums since you are going to be ORing flags together and getting total values not explicitly defined by the enum.
Of course there's FlagAttribute to mark enums like that.
There's also a case where you want to treat a value as a number but may have several special values. You could use consts to define these values, but using an enum ensures you can strongly type where these special values can be used.
Off the top of my head those are situations where it makes sense to allow that.
Admin
They do, unless you ask the compiler to let you shoot yourself in the foot by using an explicit cast. Forcing an undefined value into an enum will not cause a runtime error at assignment time. But maybe at use time. Contrast explicitly casting some object type to another. If the object cannot be cast you get a failed at assignment time. (Just before assignment.)
Admin
Wait until you get a PHP who demands that every line of comments should be commented. Including comments for comments and comments for comments for comments and so on.
Admin
In Swift, it is impossible to have an enum with a value that is not enumerated in the enum. Constructor returns nil. And in a switch statement it's possible to handle enum values that were added to a library after you wrote your code.
Admin
Of course we have no further questions, Remy. Because we know that if we did, we might be given an answer, one which leads to more questions, confusion, and onwards down a slippery slope into madness along with some alcohol dependency.
Admin
Hate to break it to you but you're just delaying the inevitable.
Admin
It's the classic "public static class Constants { public const int Zero=0; }" nonsense, but on steroids. (Really, I'd say that template identifier should be a string rather than a number.)