Coding Standards for C#?
infinite9 asks: "I've been given the task of writing the coding standards for my corporation. I've been in IT for 12 years, so many things are obvious. Recently though, my employer has standardized on C# and .NET, and since I'm new to the technology (isn't everyone?) I'm not sure what to include. I've included a paragraph about signing assemblies with a standard key pair as well as a few other odds and ends. Apart from the obvious statements that apply to any language like good comments, good variable names, and maintainable code, can anyone suggest other C#/.NET related things that I should include?"
Just one rule about coding in c#:
Don't.
It's 10 PM. Do you know if you're un-American?
Don't attempt to come up with any of your own formatting rules (as much as it's tempting to make your code look like the rest of your code)... having a standard formatting implemented by the IDE results in much cleaner formatting and will ensure that the people you hire will understand the code that much sooner.
To work out some standards use what you know from Java, Python or any other language with a C based syntax.
The guys at ICSharpCode (http://www.icsharpcode.net/TechNotes/) have some nice documents on coding styles/standards for coding for #Develop (you don't have to follow them when coding using #Develop)
Also have a look at something like FX Cop from MS (http://www.gotdotnet.com/team/libraries) to help enforce coding standards.
t
I would also stress: use the inline documentation stuff a log (/// comments). Use NDOC to generate documents from the XML output. Make use of the using() {} statement to encourage early resource disposal.
It doesn't really matter what coding standards you implement, as long as something consistent is used. And that doesn't really matter much either.
Microsoft will probably innovate a new language syntax soon, my guess would be C##. It will undoubtedly be more scalable, secure, robust and less prone to errors. And it'll automatically be included in the latest version of Windows, but not be compatible with previous versions due to architecture limitations.
Personally, I'm holding out for that version. I'm going to push my company to standardize on that version. Because it just makes good business sense.
We need a moderation for Sarcastic!
Maybe you'll find this useful: White paper on resource management in components written for the Common Language Runtime (CLR).
-jk
Learn to use Visual Studio.NET. It will do everything you want it to. Don't worry about tabstops, etc. because it does stuff like that for you. Some helpful shortcuts:
/// = When used a line above functions, events, etc. it will autogenerate the XML based comments
CTRL+[SPACE] = Type ahead
I used to program C# with UltraEdit before my company could afford VS.NET. VS.NET paid for itself the first day that I used it because it saved me so much time.
If you have the time, I'd highly recommend reading the C# language specification.
THERE IS NO DATA. THERE IS O
Coding standards:
Another word of advice, "C#" is a bad name for your perl script. I hear there's already something named that.
Michael.
Linux : Mac
use an abstraction layer and your code will be portable (maybe) to gtk#
Before adopting WHATWG, read the moonlight.NET EULA [http://www.microsoft.com/interop/msnovellcollab/moonlight.mspx]
> and since I'm new to the technology (isn't everyone?)
;-)
Used Java?
Having an IT guy come up with coding standards is like asking the janitor to design an operating system.
(The fact that he's consulting with Slashdot regarding aspects of his job is kind of a hint.)
You might want to look at what Microsoft has published about standards first, use that as a foundation
= /library/en-us/cpgenref/html/cpconnetframeworkdesi gnguidelines.asp
http://msdn.microsoft.com/library/default.asp?url
There are a set of notes towards a set of C# conventions at http://www.xoc.net/standards. RVBA has long been the most widely used set of conventions for Visual Basic. The new conventions take into account the features of C# and .NET.
It's easy to abbreviate code with lots of Using statements. However, this obfuscates code, since there are several thousand classes scattered across numerous namespaces. Plus there's no different in byte code between explicit naming and abbreviated naming combined with Using statements. In our group, even the folks originally opposed to all that extra typing have now adopted this style, just for readability. One more thing: After using doxygen for the past year, I view Microsoft's xml tags a distant second. The latest doxygen 1.3 candidate does a fairly decent job with C#, and the resulting documentation is much more informative than Microsoft's. Just my two scents...
Make sure you recommend CVS, or some other type
of versioning system to save hell down the road.
Enforce under penalty of death or termination of
employment that DOCUMENTATION IS PART OF DEVELOPMENT.
I've had many a contract where I've basically just
had to say "screw it" and redo a rats nest of
undocumentated code because of zero documentation.
For every annoying gentoo user, are three even more annoying anti-gentoo crybabies. Take Yosh from #Gimp for example.
....several other readers. Take a look at FxCop.
It will help you check your code for conformance to design guidelines and point out possible usage errors, localization issues, security problems, and possible performance improvements.
A complete coding standard is difficult to come up with in one shot. Also, developers generally have their "de-facto" standards that they like to follow, so getting buy-in can be difficult. Perhaps it's easier to just allow the coding standard to evolve as needed by using a collaboration facility to come up with it.
Take a look at Twiki.
Recently though, my employer has standardized on C# and .NET, and since I'm new to the technology (isn't everyone?)
.NET now is like adopting J2EE when it was at version 1.0 or 1.1. The details of the APIs aren't hammered out, yet, so a lot of code your company creates will be "legacy" very quickly--probably within one year. If you don't believe me, ask anyone who jumped onto the JSP and Java Servlet bandwagon when they first came out (before tag libraries, etc.).
I'm suprised your company would take such a high-risk action. "Standardizing" on something so new and untested is, IMO, irresponsible.
Adopting
Healthcare article at Kuro5hin
http://www.gotdotnet.com/team/libraries/
.NET assemblies for conformance to the .NET Framework Design Guidelines . It uses reflection, MSIL parsing, and callgraph analysis to inspect your assemblies for more than 200 defects in the following areas: naming conventions, library design, localization, security, and performance (see rule descriptions). The package includes both GUI and command line versions of the tool, as well as the SDK to create your own rules.
This very useful program is a code analysis tool that checks your
I think it's pretty funny, yet deadly serious... They go through each feature of C# and end up concluding that you should avoid them all.
C# Best Practice: Do not use boxing and unboxing
check it out here:
csharp faq
When developing your coding standards, categorize the "rules" into three categories: recommended, guidelines, and rules.
Recommendations are really just suggestions. For instance, the last coding standards I helped to author we recommended that open brackets (the '{' character) be placed at the end of the expression rather than on a separate line. This recommendation was not followed by some, but it really isn't that critical towards code readability. For the record, I actually like putting open brackets on new lines, so that the open & close brackets line up. I did change my practices, but I occasionally forgot, and didn't get yelled at since it was merely a "recommendation".
Guidelines are "rules" to be followed at all times unless there is a really good reason not to. For instance, we had a guideline that instructions should not be divided between lines, and that only one instruction should appear per line. Well, if you have a really long piece of logic, splitting the instruction across two lines makes sense. Conditional expressions (i.e. something of the form of "(ab)? a : b") counted as a single instruction.
Rules must be followed at all times. For instance, one rule we used was no "goto" statements. Pretty simple and obvious.
We found that by having the rules in this form, people that would categorically reject some of the guidelines under different circumstances were more receptive towards the guidelines when presented in this manner. So long as they weren't forced to adopt these "rules" they somehow seemed less ominous. YMMV.
--Be human.
well, me thnks he was joking about the truth. but he's right most MS shops won't look at anything else...at my work we are an ms shop.
-we develop on NT
-using VB
-we use VSS for source control
-MS-SQL server bor DB
-outlook
-MS office
We recently decieded we needed a good debuging package...(we have lots o bugs..:( )
I suggested bugzilla as soon as my boos found out it ran on linux he was like nope.
but like what ever the culture in an MS shop is weird they always want ms cause they want to go with the market leader....what ever that means.
what ever dude.
you go back to using emacs, or VI
I will code in a modern IDE with fun little popups that remind me what function I was looking for because the guy who wrote the library I'm using left the company and used name like "NSTMRE" (can you remember that?) for functions.
any way when I moved from Academics (emacs coding of Fortran and C)
to Industrly VB and Java Coding in an uptodate IDE my productivity went up.
just my opinion but
:
:
- Properties
should Return/set a value without modifiying the rest of the contents of the Object....ie if you call them repeatedly you will always get the same result every time...(asuming you don't do anything else inbetween)
- Functions / Proceduters
can and should be anything modifying contents of the object, or not and returning a value or not.
BTW...do you mean external to the function ? or external to the Object. cause do if you are Moding contencts directly that don't belong to you lookout.......(are there any languages left that let you do that?)
It is VERY nice to see people being helpful; and not just making cracks because C# is a Microsoft product.
I think with the interesting people, their lives can't possibly be wrapped up into a nice little package.
I wasn't going to comment on this thread, not being particularly informed on C# itself, but two things have struck me that might be worth mentioning.
If you disagree, post your argument. (-1, Overrated) isn't your personal censorship tool for views you don't like.
We need a moderation for Sarcastic!
I've thought about this as well. The problem is would it +1 Sarcastic or -1 Sarcastic? I think it should be something along the lines of +1 Sarcastic(Funny) and -1 Sarcastic(Asshole). Or how about a 0 Sarcastic that just adds "Sarcastic(" and ")" to the original modifier name?
Just a thought.
A site that purports to be a language FAQ and yet goes thru the features one at a time eliminating those that can be misused would rule out all good languages -- all useful tools in fact.
.Net is very nice, I don't have much respect for nonsense like this.
There is some value in a list of wise warnings, but that's not how this site presents itself, so I'm forced to conclude that it's just another anti-MS propaganda piece.
Since the REAL story about MS is pretty nasty, yet the technology of C# and
"Those who have never entered upon scientific pursuits know not a tithe of the poetry by which they are surrounded."
The language isn't so new, it's Java with all the annoying bits fixed. And while the APIs could change I suppose, I'd say it's better than using something which is already a legacy technology. For any Microsoft shop using VB6, COM, or MFC, moving to .Net is a no-brainer.
Don't.
why not?
Be advised (warned ?) that the code written by the webforms designer is very, very buggy.
If you want to wire in custom events for custom controls, or you just like to code your ascx controls in the source view (not the gui designer) then be prepared to have VS.Net intermittently decide to wipe out your event wirings.
Also be prepared to have VS.Net hijack your xml (the aspx or ascx page) formating any time you switch from source editor to gui editor.
I know this is slightly OT but...does anyone else find this "keyword overloading" (IIRR Don Box's essential .NET was where I saw this nice euphemism, or perhaps it was "namespace overloading") annoying? C# is C and C++ derived...why not use Imports (which has not been used for anyting in C#)?
SFAIK you can assign meta-data to all functions etc.. in C#.
Defing some basic meta-data that should be present.
Say Author, Data, Changes / BUGS , Links to design and test documentation etc...
It should be easy to link any good configuration management software into the meta-data.
thank God the internet isn't a human right.
One more crippling bombshell hit the already beleaguered *BSD community when IDC confirmed that *BSD market share has dropped yet again, now down to less than a fraction of 1 percent of all servers. Coming on the heels of a recent Netcraft survey which plainly states that *BSD has lost more market share, this news serves to reinforce what we've known all along. *BSD is collapsing in complete disarray, as fittingly exemplified by failing dead last in the recent Sys Admin comprehensive networking test.
You don't need to be a Kreskin to predict *BSD's future. The hand writing is on the wall: *BSD faces a bleak future. In fact there won't be any future at all for *BSD because *BSD is dying. Things are looking very bad for *BSD. As many of us are already aware, *BSD continues to lose market share. Red ink flows like a river of blood.
FreeBSD is the most endangered of them all, having lost 93% of its core developers. The sudden and unpleasant departures of long time FreeBSD developers Jordan Hubbard and Mike Smith only serve to underscore the point more clearly. There can no longer be any doubt: FreeBSD is dying.
Let's keep to the facts and look at the numbers.
OpenBSD leader Theo states that there are 7000 users of OpenBSD. How many users of NetBSD are there? Let's see. The number of OpenBSD versus NetBSD posts on Usenet is roughly in ratio of 5 to 1. Therefore there are about 7000/5 = 1400 NetBSD users. BSD/OS posts on Usenet are about half of the volume of NetBSD posts. Therefore there are about 700 users of BSD/OS. A recent article put FreeBSD at about 80 percent of the *BSD market. Therefore there are (7000+1400+700)*4 = 36400 FreeBSD users. This is consistent with the number of FreeBSD Usenet posts.
Due to the troubles of Walnut Creek, abysmal sales and so on, FreeBSD went out of business and was taken over by BSDI who sell another troubled OS. Now BSDI is also dead, its corpse turned over to yet another charnel house.
All major surveys show that *BSD has steadily declined in market share. *BSD is very sick and its long term survival prospects are very dim. If *BSD is to survive at all it will be among OS dilettante dabblers. *BSD continues to decay. Nothing short of a miracle could save it at this point in time. For all practical purposes, *BSD is dead.
Fact: *BSD is dying
I would suggest downloading the C# standards draft and it contains a suggested coding standard.