• Bogolese (unregistered)

    No 42?

  • Prime Mover (unregistered) in reply to Bogolese

    You blind, bruv?

  • (nodebb)

    Developer 745 was responsible for writing this snippet. He was birthed by alcoholic parents 623 and 679.

  • some ron (unregistered)

    At least they are not using magic numbers, where nobody knows what they mean...

  • Steve (unregistered)

    And that's what you get when some PHB insists that 'every line of code' should be commented

  • (nodebb)

    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.

  • (nodebb)

    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.

  • Naomi (unregistered)

    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).

  • (nodebb) in reply to Naomi

    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

  • Brian (unregistered) in reply to Naomi
    Tangent, but do .NET enums even protect against unexpected values?

    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.

  • 516052 (unregistered) in reply to Naomi

    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.

  • Two Pi Man (unregistered)

    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.

  • Dan Bugglin (google) in reply to Naomi

    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.

  • Dan Bugglin (google) in reply to Naomi

    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.

  • DrFloyd5 (unregistered) in reply to Naomi

    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.)

  • Gnasher729 (unregistered) in reply to Steve

    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.

  • Gnasher729 (unregistered) in reply to 516052

    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.

  • Chris (unregistered)

    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.

  • 516052 (unregistered) in reply to Chris

    Hate to break it to you but you're just delaying the inevitable.

  • Mike Rosoft (unregistered)
    Comment held for moderation.

Leave a comment on “Templated Comments”

Log In or post as a guest

Replying to comment #:

« Return to Article