Welcome to Community Server Sign in | Join | Help

Consts, Enums and Naming

A couple of items related to constants, enums and naming.

1. What is the official style guidance on naming consts? From the MS docs it looks like they're to be named like any other member. Of course, you see a lot of code with OLDER_SCHOOL_CONVENTIONS. I'm not even going to begin arguing for hungarian or c_whateverConst--but I will admit, I think the uppercase/underscore style finally won me over the last few months.

Of course FxCop freaks out about this and you can't save a rule set with that one turned off (big shortcoming)--which reminds me, of something Keith Pleas said that got a laugh tonight. FxCop itself doesn't make it through FxCop's rules. Which I believe (not having tried it myself), because the conventions in the framework themselves don't seem to be that much closer to FxCop-seal-of-approval than anyone else's code.

Another random FxCop annoyance. Member field naming. I cop to prefering _underscoredPrefixing on non-public members. According to FxCop this makes me a bad person. I disable the rule and I can be a good person, or, I can go to fully sanctioned naming: legalPrivateMember. Okay fine. What if this member is backing a public property LegalPrivateMember? Now I'm a bad person again. FxCop doesn't like members having only case as the differentiator.

Okay not fine. That's silly. I'm not going to go adding semantics to one of the two to gain full compliance. If it's dateCreated on the inside, it's DateCreated on the outside to me (the majority of the time). In fact, I'm just going to go back to _dateCreated and DateCreated anyway, because I'm already a scofflaw.

2.) Enums with strings hehe. I understand they're a special type, I understand that they only hold instances of ValueType. I dug through the Rotor code checking out how they work, I suppose I could rewrite all of that myself (and I don't know that I legally could do that anyway, since it would be a derived work and I might want to use this for things other than research, non-commercial gain or building my very own ECMA implentation.

But I really would like something enum-like in terms of syntax for declaring consts or static readonlys. And I can totally use enums for that--for any integral type. Well, sometimes my consts aren't integral types. What then?

I'd love something along the lines of...

class Whatever()
{
 private const[] Default
 {
  int Timeout = 10,
  string VsKeyPageSize = "VS:PageSize",
  string QryStrPageID = "pgid"
 }

 ...
 doSomethingWith(Request.QueryString[Default.QryStrPageID]);
 ...
}

which I know is just my personal syntactical pipedream... an array of const, what the hell is that?!? But I like the naming that comes from using enums (and it would make it much more workable to not name consts in some special way to identify them).

I suppose there are other ways to the same place (I've done it with nested classes before containing only static members, this just seems like a hack/feels dirty), it's just been something that thinking about const naming drove me to think about again.

Published Friday, August 22, 2003 1:30 AM by grant

Comments

Friday, October 17, 2003 6:19 AM by grant

# re: Consts, Enums and Naming

Constant arrays sound and looks like a great idea. I would love to have it. And now that Java gets enum's, isn't it time for C# to take it up another notch and include something like this?
Anonymous comments are disabled