Docsity

Coding Standards and Best Practices for C# Developers, Papers of Software Engineering

Toggle sidebar
bg1
Deceit - Code Standards
Formatting, Name Standards, etc.
I think we’ve all more or less grown up around the formatting and standards detailed here…
http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=336
The only thing I might mention is that the articles suggests using either the PascalNamingConvention or
the camelNamingConvention. We’ve all learned to use the Camel notation, but since C# specifically uses
the PascalStyle, we should probably change to using that style.
Documentation
Code checked into the repository should be documented on a number of levels:
All methods, properties, and events must be documented with standard C# style (just type ///
the line above it and fill in the blanks) before 2 days after writing. We don’t want to spend too
much time documenting, yet we also don’t want to keep each other in the dark or forget what
our own code was doing.
oException: inherited class members are already documented in the super class so you
don’t need to do it again.
All code should be documented with inline comments explaining what the code does. Don’t
repeat what’s written in the code. Instead, use these to divide the code into sections, explain
why you do things, and any caveats, unclear details, and things you’d want to know if you were
reading the code for the first time.
oNote, it’s best if you simply pseudo-code with comments first, then go in and write your
code.
The architecture of your code should be documented in a number of ways:
oIf helpful, use a UML diagram to show the relationships between your classes.
Preventing code duplication
We should make every effort to keep from writing the same code twice. We shouldn’t even write similar
code twice. Here are two guidelines to prevent the duplication of code:
Firstly duplication of your own code: anytime you find yourself writing nearly the same thing
over and over again (by which I mean more than about twice), try to find some way to abstract
out the common behavior and use it in all instances. If unsure how to do this, ask for advice.
Secondly, to prevent duplication within the project: anytime you’re writing something that isn’t
specific to your project, put it in the Common project. Likewise, check the Common project to
see if someone has already written the code that you’re needing to use.
pf3
pf4

Partial preview of the text

Download Coding Standards and Best Practices for C# Developers and more Papers Software Engineering in PDF only on Docsity!

Deceit - Code Standards

Formatting, Name Standards, etc.

I think we’ve all more or less grown up around the formatting and standards detailed here… http://www.csharpfriends.com/Articles/getArticle.aspx?articleID= The only thing I might mention is that the articles suggests using either the PascalNamingConvention or the camelNamingConvention. We’ve all learned to use the Camel notation, but since C# specifically uses the PascalStyle, we should probably change to using that style.

Documentation

Code checked into the repository should be documented on a number of levels:  All methods, properties, and events must be documented with standard C# style (just type /// the line above it and fill in the blanks) before 2 days after writing. We don’t want to spend too much time documenting, yet we also don’t want to keep each other in the dark or forget what our own code was doing. o Exception: inherited class members are already documented in the super class so you don’t need to do it again.  All code should be documented with inline comments explaining what the code does. Don’t repeat what’s written in the code. Instead, use these to divide the code into sections, explain why you do things, and any caveats, unclear details, and things you’d want to know if you were reading the code for the first time. o Note, it’s best if you simply pseudo-code with comments first, then go in and write your code.  The architecture of your code should be documented in a number of ways: o If helpful, use a UML diagram to show the relationships between your classes.

Preventing code duplication

We should make every effort to keep from writing the same code twice. We shouldn’t even write similar code twice. Here are two guidelines to prevent the duplication of code:  Firstly duplication of your own code: anytime you find yourself writing nearly the same thing over and over again (by which I mean more than about twice), try to find some way to abstract out the common behavior and use it in all instances. If unsure how to do this, ask for advice.  Secondly, to prevent duplication within the project: anytime you’re writing something that isn’t specific to your project, put it in the Common project. Likewise, check the Common project to see if someone has already written the code that you’re needing to use.

Unit tests

All production code should be unit tested. I will allow some flexibility in testing practices. If you’d like to go the Extreme Programming route and write your tests first, by all means go ahead. But you must at some point write unit tests for your code. You can do this by right-clicking in the editor and choosing “Create Unit Test…” Add your tests to the Test Project. Remember, unit tests are especially useful when testing some concretized form of an abstract class. That way, anytime new classes are added, there is already code to test with.

Asserts and Exceptions

Don’t let things “silently fail.” Use asserts to prevent bugs. This is long standing practice of good coding, which though somewhat reduced in importance in modern languages like C#, is still useful to catch some bugs right away. Anytime you are assuming anything about the status of a variable, etc., assert this assumption explicitly. This can be done in C# using the Debug.Assert function (you must add “using System.Diagnostics”). Here are some instances where you should assert yourself:  Checking that parameter values satisfy all assumptions (i.e. that some integer is positive, that some string is non-empty, etc.). Note that often in public methods, the correct behavior may be to throw an ArgumentException.  Anything that should be invariant (e.g. in a banking application you might assert that the sum of all the transactions on a person’s account gives the account balance, I’m sure you’ll find places where this should be done for us). If a class has lots of invariants, you can add a private method called something like invariantsAreTrue(), and assert it at various places within the code.  Assuring that methods fulfill their contract. For instance, it may make sense to ensure that the state of the system is as it should be at the end of a method. In particular, I don’t really care how slow the code is with the assertions running. Debug code can be slow, and the stuff won’t run in release implementations. Some examples: the debug version of Excel does every calculation twice: once with the optimized calculation engine, and again with a simpler slower engine.

The Self-Testing Process

You should test your own code in a number of ways:  Build it. If you break the build you wear the dunce cap. Don’t do it.

Breaking sections up by white space is fine, but one line is enough, plus possibly one comment. Too much whitespace can make code harder to read since you can’t see all of it at once.