What's the proper naming convention for a prop

2019-02-04 04:14发布

Pretty simple question: When i have a persistable object, it usually has a property called ID (for abstract classes).

So .. is the naming convention ID or Id?

eg.

public int ID { get; set; }

or

public int Id { get; set; }

cheers :)

PS. This is for .NET btw. FXCop conformat would be a bonus.

9条回答
干净又极端
2楼-- · 2019-02-04 04:34

As others pointed out, Id is right according to .NET conventions, but somehow it doesn't feel right, so many people use ID (and still live).

I don't know what the content of your field will be (numbers, strings, guids), but if you want to skip the problem you may simply use another .NET convention for object identifiers: Name

查看更多
Rolldiameter
3楼-- · 2019-02-04 04:36

Whatever you like it to be, just be consistent. ID is not a word, it's an abbreviation of identity. How to write abbreviations is something people argue about for a long time. E.g. is it

getResourceURL

or

getResourceUrl

actually both can be found in use in different frameworks. Another popular abbr. with similar problem is UTF8.

It's just important to be consistent, because otherwise people always have to look up the correct capitalization for every method, if every method handles it in a different way.

I have my own convention for that. If the abbr. is at the end of the name, it is all capitalized, if it's somewhere else, it follows camel notation rules. E.g.

getResourceURL
urlOfResource
compareUrlToString

Why? I like abbr. to be capitalized. Most people expect URL or UTF to be capitalized. However, if in the middle of a name, it destroys the advantage of camel notation. The advantage of camel notation is that you see where a new word starts by capitalization. So compare:

compareURLToString
compareUrlToString

In the first case, I don't see immediately the URL is one word and To is the next one. The T might be part of URL (URLT) and be a different abbr., thus I'll use the second form. If it's at the end however, it won't play a role, no other word follows, thus I prefer the capitalized form. I stick to this convention throughout all of my code.

查看更多
啃猪蹄的小仙女
4楼-- · 2019-02-04 04:41

I usually go with Identifier. If I really want to keep it short (as part of a longer identifier, for example), I use Id, unless it's a parameter or private member.

The .NET Framework Naming Guidelines say this:

An acronym is a word that is formed from the letters of words in a term or phrase. For example, HTML is an acronym for Hypertext Markup Language. You should include acronyms in identifiers only when they are widely known and well understood. Acronyms differ from abbreviations in that an abbreviation shortens a single word. For example, ID is an abbreviation for identifier. In general, library names should not use abbreviations.

The two abbreviations that can be used in identifiers are ID and OK. In Pascal-cased identifiers they should appear as Id, and Ok. If used as the first word in a camel-cased identifier, they should appear as id and ok, respectively.

查看更多
我欲成王,谁敢阻挡
5楼-- · 2019-02-04 04:43

"ID" is one of abbreviations hardcoded in FxCop naming rules that are always considered wrong spelled even if they are parts of other words. The only way I found to override it is adding "ID" to Acronyms as ():

<Dictionary>
<Acronyms>
   <CasingExceptions>
        <Acronym>ID</Acronym>
  </CasingExceptions>
</Acronyms>
</Dictionary>

It worked with FxCop 1.36. P.S. In general, I think that "Id" is a better alternative but sometimes it's impossible to change a name if it's generated based on XML files (or web services) which we don't control

查看更多
乱世女痞
6楼-- · 2019-02-04 04:49

We use ID internally, because Id seems to me like psychology speak, but FxCop complains, as ID is not an acronym, its an abbreviation (for Identity/Identifier). According to the FxCop rule, only acronyms that are two characters long are allowed to be all caps. Everything else should be Proper case.

We put a SuppressMessage attribute on the ID property, and everyone is happy.

查看更多
登录 后发表回答