Wednesday, June 21, 2006

Cheeburger, Cheeburger, cheeps, pepksi

One of my coworkers doesn't have a blog, but he successfully identified another sacred cow that we're converting to cheeburgers on our project. Yet, he's so altruistic, he won't take credit for the idea, saying that it's Marjorie's idea instead. So that he can remain anonymous, I'll just call him Zak T. No, that's too obvious, let's call him Z Tamsen instead.

Zak's sacred cow is the convention in the .NET world of using Pascal casing for namespaces, which is a terrible idea. We've already run into situations where a namespace clashes with a class name, which is annoying. So, we've decided to make all our namespaces all lower case, ala Java, with underscores to separate logical names (very un-ala Java).

Namespaces in .NET are particularly broken, and not just the capitalization convention. This is one of the things that Java got really right, but in a very subtle way. One of the early annoyances for most Java developers is learning the concept of a root directory, and how the package names of your code must match up to the directory structure. Once you grok that concept, though, it makes perfect sense. Only much later do you figure out that this is one of the subtle elegances of the Java language. Because the package structure is tied to the underlying directory structure, it is difficult to create class names that overlap because the OS won't let you create 2 classes by the same name in the same directory. Score one for Java, leveraging the OS to prevent headaches for developers. Of course, with the advent of virtual directory structures in JAR files, you can now create conflicts, but it is thankfully still rare.

Namespaces in .NET have no such useful restrictions. It is trivially easy to create name conflicts because the namespace is just an arbitrary string. Most of the .NET developers I know (especially if they've done any Java) use the useful "namespace must match directory structure" convention (with less restrictions on the actual root folder). In fact, one of my colleagues, Yasser, has created a very useful utility called Namespacer that verifies that your .NET class' namespace matches the directory structure. After some use on our project, he's planning to open source it. Short of fixing namespaces in .NET, at least there is a way to verify the adherence to a useful convention.

7 comments:

Cory Foy said...

Great post Neal. It's definately something I've run into before.

More annoying is that when I get a patch, or have a source file and want to know what directory it is in, in Java I can just go to it. In .NET, I can't. It's particularly prevalent in NUnit, trying to figure out where the patches go before I commit them.

woolfel said...

I'd have to agree completely. The problem also occurs when there are multiple classes with the same name like Node, but have different namespace. To get around that, you have to setup directories in VS.NET, which is more annoying than not. Having all the files in the same physical directory is one short-sighted annoyance of VS.NET

Neal Ford said...

That's true, but Java is a cross-platform language, and they still felt is was safe to assume hiearchical directory structure. The .NET guys are clever; they could have figured out a way to fix this glaring problem.

Daniel Root said...

What about case-insensitive languages like VB? It seems like following MS naming guidance and using a code checker like FxCop should catch most namespace conflict issues. I think PascalCase is way more readable than all-lowercase or using underscores.

Neal Ford said...

Excellent point, of which many .NET developers don't pay enough attention. .NET guidelines insists that you must accommodate case-insensitive languages. However, even in that case, Pascal case does you no good, and the underscores may help you. Sorry, but my_company.my_massive_project.unit_tests is much more readable than MyCompnay.MyMassiveProject.UnitTests. Maybe this is just an opinion thing.

schriner said...

The greatest thing about Java´s restriction is about navigating in completely foreign code. Finding a source file in something as large as e.g. the Eclipse project is trival in Java. You know where to look.

Partial classes are imho one of the worst ideas for larger projects as well.

Shailen Sukul said...

I do not think that underscores are any more readable than Pascal casing. It definitely sounds like an opinion thing and I personally like the freedom that .Net offers. I will leave the rest to personal discipline.