Tuesday, October 24, 2006

My Naming Conventions in .NET

^ 我想尽量兼顾VB.NET和C#。

^ Field/Class Variable: Pascal Case。Always indicate Private, do not use Dim.

^ Property: Pascal Case。

^ Parameter: Camel Case。在method body中,定义一个Local Variable,采用Pascal Case命名,除了首字母其他完全一致,然后把parameter赋值给相应的Local Variable。

^ Property和Method是给“外人”看的,命名要“好看”,所以就要规规矩矩地Pascal Case。Field是自己用的,所以命名可以有点儿“特色”。

d Field: Camel Case with Leading Underscore. In VB.NET, always indicate "Protected" or "Private", do not use "Dim".
Of all the items here, the leading underscore is really the only controversial one. I personally prefer it over straight underscore-less camel case for my private variables so that I don't have to qualify variable names with "this." to distinguish from parameters in constructors or elsewhere where I likely will have a naming collision. With VB.NET's case insensitivity, this is even more important as your accessor properties will usually have the same name as your private member variables except for the underscore.


d Properties: Pascal Case, no underscores. Try to avoid abbreviations. Members must differ by more than case to be usable from case-insensitive languages like Visual Basic .NET.
VB: Public Property RecordID As Integer
C#: public int RecordID

d Parameters: Camel Case. Try to avoid abbreviations. Parameters must differ by more than case to be usable from case-insensitive languages like Visual Basic .NET.
VB: ByRef recordID As Integer
C#: ref int recordID

d What are your thoughts on naming conventions that need to be used to distinguish the class variables, function local variables and method arguments?
d By class variables, I assume you mean fields. Those should be private (use properties for public exposure to provide a level of abstraction). For function locals, those are by definition private. For method arguments, those need to be camelCased. For the privates, I have seen two patterns in the .NET Framework: one is leading underscore, and the other is this.Foo. Either works for me.

d However, I then had to do some VB.NET development and the language isn't case sensitive. I've seen some examples that add a "Field" suffix in VB.NET . I think that is worse than a leading "_". So I came full circle and started using "_" again. :)

e Designing .NET Class Libraries: Naming Conventions (January 26, 2005)
e .NET Programming Standards and Naming Conventions