Should I really Pascal case almost all public identifiers in C#?

168 views Asked by At

I'm coming to C# from java. Java has a set of conventions that make it easy to see the use of an identifier by its casing.

Reading up on the Microsoft conventions it seems like they want you to Pascal case almost everything except parameters. It seems to me this is a much less information rich convention.

Is it really considered bad form in the .NET/Mono world to break this convention and do I just need to get used to it, or can I carry my Java habits over to my C# code that others are going to use?

5

There are 5 answers

2
Martin Zikmund On BEST ANSWER

Well, ultimately it depends on you what you choose. If others are ok with the Java style of naming, you can use it, but I would probably stick to pascal case at least for public members your classes to be consistent with the rest of the language.

ReSharper offers quick way to convert naming conventions be reformatting code, so you might also try to use your learnt ways for new classes and let it perform it's magic before you check in the code for others to see :-) .

0
zmbq On

It all depends on readers of your code. If you're the only one reading it, or the other readers are going to be Java-versed, it might not be a problem. Then again, if your code readers will be more C#y people, your code will look strange to them, and might confuse them.

1
Vilx- On

Personally I think that the name of the function is far more important than it's casing, as well as it's XML comments. Even if you don't stick to any conventions, nobody will have a problem in using your code as long as these 2 things are in order. And on the other hand - no amount of tradition is going to help with DoMagic2_2(int a, string b, decimal bb). So focus on that and use whatever convention is easier for you.

1
Alejandro On

I don't find why it should be less information, anyway the casing don't tells everything, but the most information about what you're dealing with comes from the syntax and usage instead. Although I never done any serious Java, so far they're not that different, probably the most notable being the method names being lowercase (Java) vs uppercase (C#).

In any case, they're just conventions, and like any convention, they're to some extent arbitrary. It really depends on what you prefer more, who will have to read your code, and what code is already on the codebase. The most important thing about conventions is to make all the code look the same, in whatever style you/others like.

If you already have code written (by you or by others) and have to modify that, by all means stick to whatever it's already there, just for the sake of consistency. But if starting from scratch, you may either want to decide to follow Java standard for your own comfort or C# standard for ease of reading by others. Or, why not decide with your team on a "middle ground" custom convention?, so that everyone is happy with it.

0
Aswin Ramakrishnan On

I recommend going with the convention as it makes your code more civilized. Given that there will be more C# developers reading (or) will be interested in reading your C# code rather than Java developers. For instance, if you want to post things in a blog or a thread like StackOverflow, your code will make more sense / won't look too foreign to corresponding developers if you follow the convention. Also, I believe adapting to a specific convention isn't tough at all. I'm a C# developer and I currently work on Rails, so take my word on it.