Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Coding standards

Posted on 2006-05-13
12
Medium Priority
?
400 Views
Last Modified: 2010-04-16
I'm a little confused by some of the coding standards for C#. They have some commonality but there are other places where they diverge. Some things I've yet to find a standard for. Even Microsoft's 101 samples don't stick to their own coding standards - and use sloppy component names such as editbox1, editbox2, etc. So, some questions:

1. Properties. Some standards say prefix private properties with m_ - and then say don't ever use underscores. That's obviously contradictory, and I don't particularly like underscores anyway. In Delphi we used to prefix with an f for private properties. What is the preferred method?

2. Types and variables of those types. The following code is supposed to be non-preferred because I've used a variable name the same as a type name, differing only by case (why oh why is C# case sensitive?). What should I be doing?
            public enum Style {None, Overlap1, Overlap2, OverlapBoth};
            public Style style;
3. Components. What is the naming convention? Some say Hungarian, others say don't ever use Hungarian again. Currently I've got things like buttonOk, radioButtonStyleNone, radioButtonStyleOverlapBoth - ie camelCase the component type and suffix the name of it.

4. Forms. I've got an EditNodeForm class contained within a file called EditNodeForm.cs. Should that be formEditNode, or what?

I realise this can be a subjective topic so all I want are opinions really. Thanks

Geoff M.
0
Comment
Question by:gmayo
  • 5
  • 3
  • 3
  • +1
12 Comments
 
LVL 48

Assisted Solution

by:AlexFM
AlexFM earned 105 total points
ID: 16673738
I am using FxCop for testing my programs:
http://www.gotdotnet.com/team/fxcop/

Install it and scan your application - you will have a lot of things to change. This is great program which tests coding standards and even finds many potential bugs.
0
 

Accepted Solution

by:
rafaelfranco earned 165 total points
ID: 16673849
Geoff, take a look at:
http://www.tiobe.com/standards/gemrcsharpcs.pdf

There are many tips for coding standards in C#.
0
 
LVL 35

Assisted Solution

by:mrichmon
mrichmon earned 105 total points
ID: 16683472
We also use FX cop.

however some things to note, more comments of my own.

1.  We don't use an underscore nor anyother preix for private members.  Most people that use an underscore prefix do this so that they are clearly private or so they group together inan alphabetical list.  Since the members will not be externally visible, this seems to me to be pure preference.

1.5 :o)  I like that C# is case sensitive.

2. The reason this is not prefered is that often the difference betweena public and private member is that the private member is non-capital an the public is capitalized.  Therefore you now have a class name or struct or enum name that matches what could be a variable name.

Instead be more descriptive in the naming of one or the other if possible.

3. That is so debatable.  Even microsoft can't keep it straight.  In some stuff they say things like if an acronym is two letters or less then do it this way and if more than tow letters do it that way, but then they don't always follow the convention.  In fact if you follow the FX Cop version then it doesn't follow microsoft's versions.  We have decided (and you will have to make a decision for yourself) that we are doing similar case to actul camel case and javascript.  Basically ALWAYS have only the first letter capitalized - regardlesss of acronym lengths.

Hope that helps

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 8

Author Comment

by:gmayo
ID: 16684886
195 issues on first pass (14 classes). It looks pretty useful - thanks. The PDF is also useful though it doesn't address all my issues.

So let me get this straight:
1. Use camelCasing for private properties.

2. This example should be an "enum Style" for the enum, and a "Style LineStyle" for the property.

3/4. Nobody knows/agrees.

Personally I like the T prefix in Delphi for types. Thus your class can be called TSomething and a variable instance of it called Something. Also, why do all the standards say "don't prefix types with anything" and then go on to say "prefix interfaces with an I"?!?! Contradict themselves again - though I agree with the I part of it (which is the same as Delphi).

I'll keep this discussion open for a couple more days. Thanks.

Geoff M.
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 16688121
The purpose of using FxCop is following standards even if they are not 100% consistence. By this way our code looks exactly like .NET code, interfaces look as user expects, and our libraries look like .NET libraries for user.
Except of this, FxCop teaches us a lot of useful things, not only about coding standards (which look sometimes strange). I found very useful to read help pages using links from FxCop messages. I decided to follow most of it's recommendations.

3-4. Default names given by form designer comply coding standards.
0
 
LVL 8

Author Comment

by:gmayo
ID: 16693493
"3-4. Default names given by form designer comply coding standards."

I disagree vehemently with this one. button1, button2, button3 is NOT a good way to name components. The question is, should it be:
buttonOk, buttonCancel
ButtonOk, ButtonCancel
OkButton, CancelButton
okButton, cancelButton
btnOk... etc etc?

Geoff M.
0
 
LVL 35

Expert Comment

by:mrichmon
ID: 16694075
>>3-4. Default names given by form designer comply coding standards.
No they do not!

They may follow case notation, but are NOT best practice for coding standards.  When I evaluate code, such as when interviewing a candidate, I will immediately think less of the programmer's ability if you have textbox1, textbox2, etc.
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 16696683
Designer cannot give meaningful names to variables, because it has no idea what is the purpose of using them. But it gives names according to standard. This doesn't mean that we must accept them without changes.
0
 
LVL 8

Author Comment

by:gmayo
ID: 16711953
Of course it doesn't know what to call them, but to leave them as button1, button2 is just sloppy programming. Like mrichmon, I think less of a programmer for doing this. As part of SIL2 work later this year (albeit Delphi rather than C#) I will be auditing code and if I see that, the code will be rejected automatically.

Mrichmon, how do you label components?

Thanks.

Geoff M.
0
 
LVL 8

Author Comment

by:gmayo
ID: 16793662
Points split but only a B as no further discussion was forthcoming :-(

I still don't know how I should be labelling components!

Thanks anyway

Geoff M.
0
 
LVL 35

Expert Comment

by:mrichmon
ID: 16793821
I never got the email saying you responded again asking about components.

But a B?

I think the responses here mostly (with a few exceptions I bet you can guess which) addressed your question completely.

No one can really give you a 100% hands down this is the correct way answer, as that doesn't exist.  Depending on where you go you will end up with different standards, whether Hungarian, Camelcase,  etc.

You need to decide what the standards ou will use are and stick to it.

If you want standards used by others then try using a tool like FXCop to conform to thier standard - and uncheck the portions that contradict.
0
 
LVL 8

Author Comment

by:gmayo
ID: 16801011
I asked four opinions and only got decent replies to two of them. Following EE guidelines, that only really qualifies as a B. If you re-read my original question, I was only asking for opinions.

If you are really that aggrieved by a B, ask the moderators to up it to an A and I'll agree, even if it does go against EE ethos.

Geoff M.
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…
Suggested Courses
Course of the Month13 days, 1 hour left to enroll

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question