Technology with opinion

Wednesday, September 07, 2005

Best Practices: C# & .Net

  1. Private and class-level variables should be written using camel casing
  2. Public members should be named using Pascal casing
  3. Use proper namespacing within all your .Net classes & projects such as: [CompanyName].[BusinessUnit].[ProjectName].[VSNetProjectName]
    Example:
    Microsoft.Office.HelloWorld.Ui
    Microsoft.Office.HelloWorld.Data
  4. Create separate projects and namespaces for your UI, Data and Business layers
  5. Use DataSet when retrieving results from database and the data needs to be reread, serialized, cached or multiple sets of data need to be related and joined
  6. Use DataReader when retrieving results from database and the data needs to be read once and displayed quickly
  7. Use XPathDocument when reading XML and needing to extract elements (use XPath, etc) out of the XML document
  8. Use XmlDocument when needing to perform two or more of the following: read, write or serialize.
  9. Use Command objects to execute SQL code or stored procedures
  10. Use EnterpriseLibrary (Application Blocks) in your application where possible, as they are a collection of best practices in working with .Net
  11. Use StringBuilder object to construct strings instead of using plus (+) sign to concatenate
  12. Use regular ‘for’ loop instead of ‘for each’ loop where possible, as traditional ‘for’ loop is faster and more efficient
  13. Avoid using DataAdapter objects
  14. Avoid using the DataGrid for inline editing, especially the more complex the process and logic is, as this creates something resembling spaghetti code
  15. Add attributes to web service classes and their public exposable members by using [WebService] and [WebMethod]. Properly namespace your web service and document the web service and your web methods this way
  16. Add XML Comments to all private and public members
  17. Use source code control

No comments: