[CSHARP-363] Proposal: use leading underscore for private field names Created: 12/Dec/11 Updated: 02/Apr/15 Resolved: 31/Dec/11 |
|
| Status: | Closed |
| Project: | C# Driver |
| Component/s: | None |
| Affects Version/s: | 1.3.1 |
| Fix Version/s: | 1.4 |
| Type: | Task | Priority: | Minor - P4 |
| Reporter: | Robert Stam | Assignee: | Robert Stam |
| Resolution: | Done | Votes: | 2 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Backwards Compatibility: | Minor Change |
| Description |
|
This proposal is to adopt the rather ugly convention of using leading underscores for field names. While this is not a very attractive convention, it seems like the least ugly way to avoid having property names that differ only in case from their underlying backing field names (which causes warnings when compiling with the CLSCompliant assembly attribute). We will allow a few weeks to go by to allow time for comments before deciding whether to do this or not. |
| Comments |
| Comment by Craig Wilson [ 12/Jun/12 ] |
|
I think Rick B. was referring to mongodb fields rather than actual fields because of his reference to an "attribute or config setting". This ticket is to the actual driver code, nothing related to mongodb documents. |
| Comment by Robert Stam [ 12/Jun/12 ] |
|
Using underscores for fields is one of several common naming conventions in use by the community at large. Even Microsoft uses it internally sometimes (which you can verify by decompiling parts of the .NET Framework). We have already completed migrating to this naming convention (almost 6 months ago) and while not everyone is going to love it in general it has been well received. |
| Comment by Rick B. [ 12/Jun/12 ] |
|
Shouldn't this be based on an attribute or config setting? Underscores are certainly not a standard and are not suggested by Microsoft. From Microsoft's coding standards: Do not use underscores, hyphens, or any other nonalphanumeric characters. Do use Pascal casing in field names. Do name fields with nouns or noun phrases. Do not use a prefix for field names. For example, do not use g_ or s_ to distinguish static versus non-static fields. The general recomendation is: Do not use a prefix for member variables (, m, s_, etc.). If you want to distinguish between local and member variables you should use “this.” in C# and “Me.” in VB.NET. |
| Comment by Robert Stam [ 31/Dec/11 ] |
|
Would only be backward breaking if you subclassed one of the few internal classes that has protected fields and you were referencing the fields directly instead of through the matching property. |
| Comment by Robert Stam [ 31/Dec/11 ] |
|
Only for static fields. Instance fields would still have a single underscore. I don't like the m_ and s_ convention. |
| Comment by Sridhar Nanjundeswaran [ 31/Dec/11 ] |
|
I have never seen double underscores used. why not just a single underscore. I have the seen a convention of m_ for instance and s_ for static though |
| Comment by Robert Stam [ 30/Dec/11 ] |
|
I'm thinking of using two leading underscores for static fields. I don't think I've seen this convention used before, but it is similar to Ruby's @@ prefix for class variables (and a single @ prefix for instance variables). Can anyone think of any reason this would be a bad idea? |
| Comment by Craig Wilson [ 15/Dec/11 ] |
|
+1 from me. |
| Comment by Brian Knight [ 12/Dec/11 ] |
|
It may be ugly, but it's a pretty common convention - I think it's the ReSharper default behaviour too. If it comes down to preference, I'd vote for it over the simple case-difference for the backing field names. |