views:

253

answers:

9

In a recent VB.NET project I adopted the naming conventions I'm used to using in C#. Namely, often calling a variable the same name as the class it references, only with a different case, e.g.

Foo foo = new Foo(); // C#

Dim foo As New Foo() ' VB.NET

I find this is often the clearest way to write code, especially for small methods. This coding style obviously works fine in C#, being case sensitive, and because of the syntax highlighting provided by Visual Studio, it is very easy to see that the class name and the variable name are different.

However, to my surprise, this also worked fine nearly 100% of the time* in VB.NET. The only issue was that the variable name then appeared to take on a multiple identity. Namely it could be used to call both instance methods and Shared (static) methods of the Foo class. This didn't really cause any problems though, it just meant that Intellisense would provide a list containing both static and instance methods after you hit the '.' after the variable name.

I found, again to my surprise, that this didn't actually lead to any confusion in my project, and it's been very successful so far! However I was the only person working on this particular project.

Here is a slightly longer example:

Dim collection as Collection = New Collection()
For Each bar As Bar in Bar.All()
    collection.SomeInstanceMethod(bar)
Next
collection.SomeSharedMethod()

* The only issue I found with this was that sometimes the 'Rename' refactoring tool got confused, i.e. when renaming a class it would rename the variables with the same name as the class as well, in their declaration lines (Dim foo As...), but not the other references to that variable, causing compiler issues (duh). These were always easy to correct though.

Another small annoyance is that the VB.NET syntax highlighter doesn't highlight class names any differently than variable names, making it not quite as nice as when using it in C#. I still found the code very readable though.

Has anyone else tried allowing this in a team environment? Are there any other potential issues with this naming convention in VB.NET?

A: 

VB.NET isn't case sensitive! This equates to:

Foo Foo = new Foo(); // C#

As a standard in our team environment we would use:

Dim oFoo as Foo 'VB.NET
Stevo3000
which is perfectly legal ;)
Thomas Levesque
Yes, but you wouldn't write it ;)
Stevo3000
Not for local variables, but it's common for properties to have the same name as their type. See this post by Eric Lippert : http://blogs.msdn.com/ericlippert/archive/2009/07/06/color-color.aspx
Thomas Levesque
@Thomas: true, but VB.NET shops will try to avoid this for the components they have control over for this very reason. It's not too much of a problem when consuming 3rd party assemblies, but mostly a problem when writing assemblies with these naming conflicts
STW
+7  A: 

Although VB is case-insensitive, the compiler is intelligent enough to not being confused between the object-instance and the class.

However, it's certainly very dangerous and wrong to use the same name in a case-insensitive language! Especially if other programmers are working on that project.

Moayad Mardini
One such danger is that if the declaration of "foo" goes away (gets commented out, or even just cut to be pasted) then the compiler will change the resolution to point back to the type, essentially taking the intended `Dim foo as new Foo()` and converting it to `Dim Foo as New Foo()`. It's a pain to keep clean!
STW
Yes, I have the feeling it might confuse some people, but since the compiler handles it so solidly and deterministically, is it really that bad? I would have though the compiler would probably at least warn, if not throw a genuine error about this if it had any possibility to cause unplanned behaviour and/or the conversion of such VB.NET code to IL was undefined.
Sam Salisbury
Sam, as many computer science instructors say, just because you can do that doesn't mean you have to. Although the compiler doesn't complain, it IS a bad practice which should be avoided.
Moayad Mardini
It's just plain wrong to say that it is very dangerous. Moayad, you respond to Sam by saying that is it bad because it is a bad practice. That's not what I'd call an argument ;-)
Meta-Knight
Hmm, I guess this is a slightly contentious issue... I agree with Meta-Knight though, you can't just assert that it IS bad practice. (I'm not saying it's not bad practice, but maybe it's not THAT bad, I understood it fine throughout the initial development, and it's still easy to debug.) LOL, oh well I need to find a better way than using traditional VB <a href="http://en.wikipedia.org/wiki/Hungarian_notation">Hungarian notation</a>, and I don't fancy having to come up with a descriptive essay for each variable I want to declare. The official MS documentation doesn't seem to say much on this..
Sam Salisbury
Let's try again... That was meant to say 'hungarian notation'! I'm a bit new to this, better read the docs.There are links to the official MS documentation as comments to the original question now anyway, if anyone knows the one true way to name things nicely in VB.NET, please let me know!Thanks all
Sam Salisbury
Meta-Knight, I agree, that wasn't a very good response :)Sam, you can refer to :Cwalina, Krzysztof, and Brad Abrams. Framework Design Guidelines: Conventions, Idioms, and Patterns for Reusable .NET Libraries. Boston, MA: Addison-Wesley Professional, 2005.
Moayad Mardini
+3  A: 

I have to move back and forth between VB and C#, and we consider this poor practice. We also don't like letting variable names in C# differ from their type only by case. Instead, we use an _ prefix or give it a more meaningful name.

Whenever you start a new language it's inevitable you'll notice a bunch of things that are different and miss the old way of doing things. Often this is because you are initially unaware of different features in the other language has that address the same problem. Since you're new to VB, here are a couple notes that will help you get things done:

It's not 100% correct to say that VB.Net is case-insensitive unless you also make the point that it is case-aware. When you declare an variableidentifier, the IDE will take note of what case you used and auto-correct other uses to match that case. You can use this feature to help spot typos or places where the IDE might be confused about a variable or type. I've actually come to prefer this to real case-sensitive schemes.

VB.Net imports namespaces differently. If you want to use the File class, you can just say IO.File without needing to import System.IO at the top. The feature especially comes in handy when learning a new API with a few nested namespace layers, because you can import a top-level section of API, type the next namespace name, and you'll be prompted with a list of classes in that namespace. It's hard to explain here, but if you look for it and start using it, you'll really miss it when going back to C#. The main thing is that, for me at least, it really breaks my flow to need to jump to the top of the file to add yet another using directive for a namespace I may only use once or twice. In VB, that interruption is much less common.

VB.Net does background compilation. The moment your cursor leaves a line, you know whether or not that line compiles. This somewhat makes up for not highlighting class names, because part of why that's useful in C# is so you know that you typed it correctly. VB.Net gives you even more confidence in this regard.

Joel Coehoorn
Interesting notes, thanks. I had noticed (and been appalled by) the nesting of namespaces when using VB.NET, though I see your point about it being good for beginners, or those who will never bother to learn a more succinct language like C#.You can just type a class name and hit "Ctrl+Space" to auto-insert the `using Namespace` declaration in C# by the way, as long as you have the references in place.
Sam Salisbury
+1  A: 

As Moayad notes, the compiler can tell the difference--but it's bad practice that can lead to maintenance issues and other side effects.

A better practice all-around is to try to name the variable in the context they're being used, rather than just the type name. This leads to self-documenting code and requires fewer comments (comments are greatly abused as an excuse to write dense code).

STW
Yes I deinitely agree when it comes to longer methods, but for really small utility methods, there is often nothing else known about the context the function is being called in that could provide for a more descriptive name.
Sam Salisbury
I'd vote you up if I had enough reputation, I need 15 though :(
Sam Salisbury
+2  A: 

I have done the same thing in the past. I'm starting to move away from it though because Visual Studio will occasionally get confused when it auto formats the code and changes the casing on my static method calls to lower case. That is even more annoying than not being able to differentiate the variable and class names by case only. But, purely from technical perspective it should not cause any issues.

Brian Gideon
Hmm, this hasn't happened to me yet, think this may be the most persuasive reason not to use these names though! Which version of Visual Studio are you using? I've only ever used 2008
Sam Salisbury
VS 2008. It does not happen all of the time.
Brian Gideon
+1  A: 

It's only safe as long as the compiler can always tell whether Foo means the class or the variable, and eventually you'll hit a case where it can't. Eric Lippert discusses the sort of thing that can go wrong on his blog.

stevemegson
+1  A: 

I'm going to differ with the rest of the answers here... I don't think there is any problem with doing this. I do it regularly, and have absolutely 0 problems resulting from it.

If you use lowercase for the variable name you can easily differentiate the variable from the type, and the compiler will not confuse the two identifiers.

If you delete the variable declaration, the compiler will think other references to this variable now refer to the type, but it's not really a problem because those will be tagged as errors.

Meta-Knight
Yay I'm not the only one who doesn't think it's that bad! :) Good point about deleting the variable declaration as well, you're right, there's no reason for confusion because you can't have a static (Shared) member with the same signature as an instance member, so there's no chance the compiler could be confused on this issue. Intellisense doesn't even get confused... I think that must say something.
Sam Salisbury
A: 

Well, this isn't the final answer, and I don't think there is a definitive one, but the general opinion seems to be that it's not a good idea to use this naming convention! There must be one true way to write nice VB.NET variable names though, and I don't like any of the alternatives...

Here are links to the official Microsoft guidelines for anyone who's interested, although they don't seem to cover this particular question (please correct me if I've missed it).

Visual Basic Naming Conventions: http://msdn.microsoft.com/en-us/library/0b283bse.aspx

Declared Element Names: http://msdn.microsoft.com/en-us/library/81ed9a62.aspx

Cheers all!

Sam Salisbury
+1  A: 

I use this convention all the time, and it's never been a problem. The most natural name for a variable is often the class name, and therefore that's what you should call it (Best name for an arbitrary Line? line.).

The only downside is when some tool interprets the context incorrectly. For example, visual studio 2010 beta 1 sometimes uses the class highlight on variables named the same as the class. That's a bit annoying.

Context sensitivity is much closer to how I think than case sensitivity.

Strilanc
Cool, so VS 2010 will do more syntax highlighting for VB than VS 2008? I think with that addition, there may be absolutely no reason not to use this convention... What do you mean by your last sentence btw?
Sam Salisbury
Yes, it highlights classes light blue. The last sentence means I like context-sensitive languages more than case-sensitive languages.
Strilanc