-
Notifications
You must be signed in to change notification settings - Fork 1
C# Standards
In many projects, style guides and coding standards are synonymous. In the CSW, however, we aim to differentiate between the two. We designate as “standards” those things which would make code comprehension quite difficult if they weren’t adopted broadly (don’t use Hungarian notation, don’t abbreviate, etc.), and as “style” those things which don’t necessarily made it difficult to understand the code, but would still improve if everyone were on the same page. Recommendations on the use of whitespace would fall in this category.
-
Type names, constants and method names should use Pascal casing.
-
Camel casing applies to local variables declared inside a method, and arguments or parameters used in method declarations or constructors.
-
Hungarian notation should not be used. There has historically been some flexibility in terms of referencing server-side control IDs in ASP.NET. However, since these items are renamed by the ASP.NET engine and can be referenced using the recommended jQuery selectors, use of these prefixes can be confusing (e.g.
txt_UserName
andbtn_Submit
render as<input type="text"/>
and<input type="submit"/>
on the client-side.) -
Method names should be verbs (optionally followed a direct object), as they describe an action, e.g.
stringReader.ReadLine()
. -
Always use C# alias types (
string
,int
, etc.). Some have said to use the .NET types (System.String
,System.Int32
, etc.) when calling static methods (likeInt32.Parse()
), but recent trends and tools discourage this. -
Namespaces should be well thought-out and professional, following the methods used by Microsoft
Company.Product.Layer.Specialization
(e.g.System.Web.UI.WebControls
) -
Always use correct spelling and grammar in names, variables, comments, and database design.
-
Do not use abbreviations, either in object or method names, variables, or namespaces.
-
Use of well-known acronyms is permitted, but follow Microsoft's rules for capitalization.
- When used in PascalCasing or as the second word in camelCasing, only the first letter of an acronym should be capitalized.
using System.Data.Sql; using System.Linq;
- Always capitalize both letters in two-letter acronyms.
using System.IO; using System.Web.UI.WebControls;
- With the exception of the
Control
class and all objects that inherit from it, only the “I” inId
should be capitalized, as it is an abbreviation for “identity” or “identifier” and not an actual acronym.
-
Always use curly braces for conditional statements. Never take the one-liner shortcut, or the one subline shortcut, as they are both vague.
-
Avoid very long ternary operators that use multiple lines. Statements are very short and easily readable, e.g.
// This is ok. return isIdSpecified ? id : GetNewId();
-
Avoid nested ternary operators.
Pages in this wiki make use of the following resources throughout
- Martin, R. C., Feathers, M. C., Ottinger, T. R., Langr, J. J., Schuchert, B. L., Grenning, J. W., Wampler, K. D., ... Coplien, J. O. (2011). Clean code: A handbook of agile software craftsmanship. Upper Saddle River [etc.: Prentice Hall.
- Lowy, Juval, (July 2011). “C# Coding Standard: Guidelines and Best Practices.” (Version 2.4) www.idesign.net, © 2011 IDesign Inc.
Resources besides these will be referenced directly where they are cited.