开发者

VB.NET How do you name your variables that relate to their type?

Just wondering what everyone uses to name variables that relate to an already descriptive class?

For example:

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

There doesnt seem to be any set rule above is ok in C# but not advised in VB (although it will compile just fine) I have seen prefixes like 'o' (oCustomersData) aswell as 'my' (myC开发者_StackOverflow中文版ustomersData) but I'm not sure I like either have also seen abbreviations like cd & cc the argument being that all you have to do is hover over the variable to see the type but this seriously damages self documentation and readability.

Whats your thoughts folks?


I would probably go with:

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

Name the variables by their meaning in the current context: Another example might be.

Dim excludedCustomers as CustomersCollection
excludedCustomers = custData.GetExcluded()


I would code it thus:

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

I'm suggesting the variable name customers because I'm assuming that is the variable you will be using throughout the method and therefore it is more descriptive of what you are working on. For example later in the code you might have:

For each customer In customers
    ....
Next

Not advised in VB (although it will compile just fine)

I've not seen that advise before and I use this method all the time.

I have seen prefixes like 'o' (oCustomersData) as well as 'my' (myCustomersData) but I'm not sure I like either.

I never prefix variables like this. The only variables I prefix are class variables with _; eg _customers

I have also seen abbreviations like cd & cc the argument being that all you have to do is hover over the variable to see the type but this seriously damages self documentation and readability.

You have provided the answer. There is no need and it's damaging to readability to use short variable names like this. If the argument to use short variable names is typing speed I'd argue learn to type properly and use intellisense.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜