VB.NET如何命名与其类型相关的变量?

时间:2011-07-12 12:23:07

标签: vb.net naming-conventions typename

只是想知道每个人用什么来命名与已经描述的类有关的变量?

例如:

Dim customersData as New CustomersData
Dim customersCollection as CustomersCollection
customersCollection = customersData.GetAll()

上面似乎没有任何设置规则在C#中没有问题但在VB中没有建议(尽管它会编译得很好)我看过像'o'(oCustomersData)这样的前缀以及'my'(myCustomersData)但是我我不确定我是否也看过像cd& amp;这样的缩写词。 cc这个论点是你所要做的就是将鼠标悬停在变量上以查看类型,但这严重损害了自我文档和可读性。

你的想法是什么人?

2 个答案:

答案 0 :(得分:4)

我可能会选择:

Dim custData as New CustomersData 'Better name might be CustomersDataService
Dim customers as CustomersCollection
customers = custData.GetAll()

根据当前上下文中的含义命名变量: 另一个例子可能是。

Dim excludedCustomers as CustomersCollection
excludedCustomers = custData.GetExcluded()

答案 1 :(得分:3)

我会这样编码:

Dim customersData As New CustomersData
Dim customers As CustomersCollection
customers = customersData.GetAll()

我建议使用变量名称客户,因为我假设这是您将在整个方法中使用的变量,因此它更能描述您正在处理的内容。例如,您可能会在以后的代码中使用:

For each customer In customers
    ....
Next
  

VB中没有建议(虽然编译得很好)

我之前没有看过这个建议,我一直都在使用这种方法。

  

我看过像'o'(oCustomersData)和'my'(myCustomersData)这样的前缀,但我不确定是否也喜欢。

我从不为这样的变量添加前缀。我前缀的唯一变量是带有_的类变量;例如_customers

  

我也看过像cd& amp;这样的缩写词。 cc这个论点是你所要做的就是将鼠标悬停在变量上以查看类型,但这严重损害了自我文档和可读性。

您已提供答案。没有必要,并且使用像这样的短变量名可读性是有害的。如果使用短变量名称的参数是打字速度,我认为学会正确输入并使用智能感知。