SlideShare uma empresa Scribd logo
1 de 36
Back-2-Basics: .NET Coding Standards For The Real World
Agenda 3
Overview
Why Do You Need Standards? First, you might not agree witheverything I say… that’s okay! Pick a standard for your company Every programmer on the same page Easier to read and understand code Easier to maintain code Produces more stable, reliable code Stick to the standard!!!
Coding Standard After selecting a standard: Make sure it’s easily available to each programmer Print or electronically Enforce via code reviews Provide programs to make it easier for programmers to maintain: StyleCop – Free for C# programmers. CodeIt.Right – Enterprise edition shares profiles. Can create custom profiles for your company.
Real World Analysis Example Scenario: In production Client Server Application with millions in sales Nine projects of 183,177 lines of .NET code written by multiple programmers (junior to principal) StyleCop Analyze Code.It Right 7,076 < 1,100 6,216 < 2,527 5,091 < 1,837 This is why you need to follow good coding practices throughout the lifecycle of the project!
Code.It Right Stats All Important
Assembly Layout
General Assembly Tips Never hardcode strings that might change based on deployment such as connection strings Best place to put them are in user or application level Settings in your application. Remember to save on exit and load on start! Automatic in VB.NET  Other locations can include: Registration Database, Databases and more…  DEMO
General Assembly Tips Sign (strong name) your assemblies, including the client applications.  Also, sign interop assemblies with the project’s .snk file Name assemblies in the following format: <Company>.<Component>.dll Project names should be named the same. Microsoft.VisualBasic.dll dotNetTips.Utility.dll Acme.Services.References.dll
Mark as CLS Compliant Forces your assembly to be compliant with the Common Language Specification (CLS).  Assemblies, modules, and types can be CLS-compliant even if some parts of the assembly, module, or type are not CLS-compliant, as long as these two conditions are met:  If the element is marked as CLS-compliant, the parts that are not CLS-compliant must be marked using CLSCompliantAttribute with its argument set to false.  A comparable CLS-compliant alternative member must be supplied for each member that is not CLS-compliant. DEMO
Element Order
Elements of the Same Type Should appear in this order: public elements protected elements internal elements private elements Use #region/ #endregion to group namespace-level and class-level elements Use separate regions to organize the private, protected, and internal members.
Namespaces The general rule for naming namespaces is to use the company name followed by the technology name and optionally the feature and design CompanyName.TechnologyName[.Feature][.Design]
Enums Use abbreviations sparingly for Enums and their values Do not use an Enum suffix on Enum type names Use a singular name for most Enum types, but use a plural name for Enum types that are bit fields
Enums Always add the FlagsAttribute to a bit field Enum type Avoid providing explicit values for Enums (unless necessary) Avoid specifying a type for an Enum Public Enum WebServiceAction   GetCurrentMode   PauseService   ResumeService   StartService   StopService End Enum
Interfaces Prefix interface names with the letter “I” Do not use the underscore character. Use abbreviations sparingly. Challenging to version over releases The smaller, more focused the interface the better public interface IDisposable {     // Methods
    void Dispose(); }
Classes Use a noun or noun phrase to name a class. Do not use a type prefix, such as C for class, on a class name.  Do not use the underscore character (_). Avoid putting multiple classes in a single file. A file name should reflect the class it contains.  Provide a default private constructor if there are only static methods and properties on a class.  Use Static Class in C# or a Module in VB.NET Explicitly define a protected constructor on an abstract base class.
Events Use Pascal case Use an EventHandler suffix on event handler names.  Name an event argument class with the EventArgs suffix. Use EventHandler<> to declare handlers. Be careful with non-UI threads calling back to the UI! Use the BackgroundWorker Control in Windows Forms DEMO
Member Variables (Fields) Use camel case as a rule, or uppercase for very small words Prefix private variables with a "_” Member variables should not be declared public Use a Property instead Const member variables may be declared public
Properties Use Pascal case Use a noun or noun phrase to name properties Properties that return arrays or collections should be methods.  Do not use write-only properties  Consider providing events that are raised when certain properties are changed.  Name them <Property>Changed Properties should not have dependencies on each other Setting one property should not affect other properties Properties should be settable in any order. DEMO
Constructors Do not call code from a constructor! Can cause Exceptions. Capture parameters only. Provide a constructor for every class.  Do not use the this./ Me. reference unless invoking another constructor from within a constructor. Provide a protected constructor that can be used by types in a derived class.
Destructors Avoid using destructors! It is not possible to predict when garbage collection will occur. The garbage collector has no knowledge of unmanaged resources such as window handles, or open files and streams.  Implement IDisposible if you do! Use the try/finally (or Using) to make sure that unmanaged resources are disposed of even if an exception interrupts your application.  DEMO
Methods Use Pascal case Use verbs, verb phrases or verb-object pair to name methods Avoid methods with more than five arguments Use structures for passing multiple arguments Always mark public and protected methods as virtual/Overridable  in a non-sealed class.  Methods with return values should have a name describing the value returned DEMO
Method Overloading Use method overloading to provide different methods that do semantically the same thing.  Use method overloading instead of allowing default arguments.  Default arguments do not version well and therefore are not allowed in the Common Language Specification (CLS). Overloaded methods should call the most complete method If you provide the ability to override a method, make only the most complete overload virtual and define the other operations in terms of it DEMO
Do Not Expose Generic List List is a generic collection designed for performance not inheritance and Applies to public API’s Does not contain any virtual members Can not tell when updated Use Instead: System.Collections.ObjectModel.Collection System.Collections.ObjectModel.ReadOnlyCollection System.Collections.ObjectModel.KeyedCollection Use Interfaces: IDictionary DEMO
Stop Exceptions BEFORE They Happen! Defensive Programming
Prevent Exceptions Practice Defensive Programming! Any code that might cause an exception (accessing files, using objects like DataSets etc.) should check the object (depending on the type) so an Exception is not thrown For example, call File.Exists to avoid a FileNotFoundException Check and object for null Check a DataSet for rows Check an Array for bounds Check String for null or empty DEMO
Parameters Always check for valid parameter arguments Perform argument validation for every public or protected method Throw meaningful exceptions to the developer for invalid parameter arguments Use the System.ArgumentException class Or your own class derived from System.ArgumentException  DEMO
Enums Never assume that Enum arguments will be in the defined range. Enums are just an Int32, so any valid number in that range could be sent in! Always use Enum.IsDefined to verify value before using! DEMO
TryParse Use the .TryParse method on value types when assigning from a string. WILL NOT cause an exception! Returns result of True/False DEMO
Exceptions When doing any operation that could cause an exception, wrap in Try - Catch block Use System.Environment.FailFast instead if unsafe for further execution Do not catch non-specific exceptions (for common API’s) Use Finally for cleanup code When throwing Exceptions try using from System instead of creating custom Exceptions Use MyApplication_UnhandledException event in VB.NET WinForm apps Use Application_Error event in ASP.NET apps
Code Style
Variables Avoid single character variable names i, t etc. Do not abbreviate variable words (such as num, instead of number) Unless they are well known like Xml, Html or IO If deriving from a core type, add the suffix of the identify type.  ArgumentException or FileStream Use camel case for local variables firstName
Accessing Class Member Variables Preface all calls to class members with this./Me., and place base./MyBase. before calls to all members of a base class Class BaseClass 	Public Sub ProcessData() 	End Sub End Class Class MainClass 	Inherits BaseClass 	Public Sub AnalyzeData() 	End Sub 	'Correct usage of this. and base. 	Public Sub Good() 		Me.AnalyzeData() 		MyBase.ProcessData() 	End Sub 	'Incorrect usage. 	Public Sub Bad() 		AnalyzeData() 		ProcessData() 	End Sub End Class

Mais conteúdo relacionado

Mais de David McCarter

Rock Your Code With Code Contracts -2013
Rock Your Code With Code Contracts -2013Rock Your Code With Code Contracts -2013
Rock Your Code With Code Contracts -2013David McCarter
 
Rock Your Code with Code Contracts
Rock Your Code with Code ContractsRock Your Code with Code Contracts
Rock Your Code with Code ContractsDavid McCarter
 
Back-2-Basics: Code Contracts
Back-2-Basics: Code ContractsBack-2-Basics: Code Contracts
Back-2-Basics: Code ContractsDavid McCarter
 
Back-2-Basics: .NET Coding Standards For The Real World (2011)
Back-2-Basics: .NET Coding Standards For The Real World (2011)Back-2-Basics: .NET Coding Standards For The Real World (2011)
Back-2-Basics: .NET Coding Standards For The Real World (2011)David McCarter
 
How To Survive The Technical Interview
How To Survive The Technical InterviewHow To Survive The Technical Interview
How To Survive The Technical InterviewDavid McCarter
 
Real World API Design Using The Entity Framework Services
Real World API Design Using The Entity Framework ServicesReal World API Design Using The Entity Framework Services
Real World API Design Using The Entity Framework ServicesDavid McCarter
 
Building nTier Applications with Entity Framework Services
Building nTier Applications with Entity Framework ServicesBuilding nTier Applications with Entity Framework Services
Building nTier Applications with Entity Framework ServicesDavid McCarter
 
Code Easier With Visual Studio 2010 & Extensions
Code Easier With Visual Studio 2010 & ExtensionsCode Easier With Visual Studio 2010 & Extensions
Code Easier With Visual Studio 2010 & ExtensionsDavid McCarter
 
Back-2-Basics: Exception & Event Instrumentation in .NET
Back-2-Basics: Exception & Event Instrumentation in .NETBack-2-Basics: Exception & Event Instrumentation in .NET
Back-2-Basics: Exception & Event Instrumentation in .NETDavid McCarter
 
Building nTier Applications with Entity Framework Services (Part 2)
Building nTier Applications with Entity Framework Services (Part 2)Building nTier Applications with Entity Framework Services (Part 2)
Building nTier Applications with Entity Framework Services (Part 2)David McCarter
 
Building nTier Applications with Entity Framework Services (Part 1)
Building nTier Applications with Entity Framework Services (Part 1)Building nTier Applications with Entity Framework Services (Part 1)
Building nTier Applications with Entity Framework Services (Part 1)David McCarter
 
Back-2-Basics: .NET Coding Standards For The Real World
Back-2-Basics: .NET Coding Standards For The Real WorldBack-2-Basics: .NET Coding Standards For The Real World
Back-2-Basics: .NET Coding Standards For The Real WorldDavid McCarter
 
Back-2-Basics: Exception & Event Instrumentation in .NET
Back-2-Basics: Exception & Event Instrumentation in .NETBack-2-Basics: Exception & Event Instrumentation in .NET
Back-2-Basics: Exception & Event Instrumentation in .NETDavid McCarter
 
Back-2-Basics: .NET Coding Standards For The Real World
Back-2-Basics: .NET Coding Standards For The Real WorldBack-2-Basics: .NET Coding Standards For The Real World
Back-2-Basics: .NET Coding Standards For The Real WorldDavid McCarter
 
Building nTier Applications with Entity Framework Services (Part 2)
Building nTier Applications with Entity Framework Services (Part 2)Building nTier Applications with Entity Framework Services (Part 2)
Building nTier Applications with Entity Framework Services (Part 2)David McCarter
 
Building nTier Applications with Entity Framework Services (Part 1)
Building nTier Applications with Entity Framework Services (Part 1)Building nTier Applications with Entity Framework Services (Part 1)
Building nTier Applications with Entity Framework Services (Part 1)David McCarter
 
Building N Tier Applications With Entity Framework Services 2010
Building N Tier Applications With Entity Framework Services 2010Building N Tier Applications With Entity Framework Services 2010
Building N Tier Applications With Entity Framework Services 2010David McCarter
 

Mais de David McCarter (17)

Rock Your Code With Code Contracts -2013
Rock Your Code With Code Contracts -2013Rock Your Code With Code Contracts -2013
Rock Your Code With Code Contracts -2013
 
Rock Your Code with Code Contracts
Rock Your Code with Code ContractsRock Your Code with Code Contracts
Rock Your Code with Code Contracts
 
Back-2-Basics: Code Contracts
Back-2-Basics: Code ContractsBack-2-Basics: Code Contracts
Back-2-Basics: Code Contracts
 
Back-2-Basics: .NET Coding Standards For The Real World (2011)
Back-2-Basics: .NET Coding Standards For The Real World (2011)Back-2-Basics: .NET Coding Standards For The Real World (2011)
Back-2-Basics: .NET Coding Standards For The Real World (2011)
 
How To Survive The Technical Interview
How To Survive The Technical InterviewHow To Survive The Technical Interview
How To Survive The Technical Interview
 
Real World API Design Using The Entity Framework Services
Real World API Design Using The Entity Framework ServicesReal World API Design Using The Entity Framework Services
Real World API Design Using The Entity Framework Services
 
Building nTier Applications with Entity Framework Services
Building nTier Applications with Entity Framework ServicesBuilding nTier Applications with Entity Framework Services
Building nTier Applications with Entity Framework Services
 
Code Easier With Visual Studio 2010 & Extensions
Code Easier With Visual Studio 2010 & ExtensionsCode Easier With Visual Studio 2010 & Extensions
Code Easier With Visual Studio 2010 & Extensions
 
Back-2-Basics: Exception & Event Instrumentation in .NET
Back-2-Basics: Exception & Event Instrumentation in .NETBack-2-Basics: Exception & Event Instrumentation in .NET
Back-2-Basics: Exception & Event Instrumentation in .NET
 
Building nTier Applications with Entity Framework Services (Part 2)
Building nTier Applications with Entity Framework Services (Part 2)Building nTier Applications with Entity Framework Services (Part 2)
Building nTier Applications with Entity Framework Services (Part 2)
 
Building nTier Applications with Entity Framework Services (Part 1)
Building nTier Applications with Entity Framework Services (Part 1)Building nTier Applications with Entity Framework Services (Part 1)
Building nTier Applications with Entity Framework Services (Part 1)
 
Back-2-Basics: .NET Coding Standards For The Real World
Back-2-Basics: .NET Coding Standards For The Real WorldBack-2-Basics: .NET Coding Standards For The Real World
Back-2-Basics: .NET Coding Standards For The Real World
 
Back-2-Basics: Exception & Event Instrumentation in .NET
Back-2-Basics: Exception & Event Instrumentation in .NETBack-2-Basics: Exception & Event Instrumentation in .NET
Back-2-Basics: Exception & Event Instrumentation in .NET
 
Back-2-Basics: .NET Coding Standards For The Real World
Back-2-Basics: .NET Coding Standards For The Real WorldBack-2-Basics: .NET Coding Standards For The Real World
Back-2-Basics: .NET Coding Standards For The Real World
 
Building nTier Applications with Entity Framework Services (Part 2)
Building nTier Applications with Entity Framework Services (Part 2)Building nTier Applications with Entity Framework Services (Part 2)
Building nTier Applications with Entity Framework Services (Part 2)
 
Building nTier Applications with Entity Framework Services (Part 1)
Building nTier Applications with Entity Framework Services (Part 1)Building nTier Applications with Entity Framework Services (Part 1)
Building nTier Applications with Entity Framework Services (Part 1)
 
Building N Tier Applications With Entity Framework Services 2010
Building N Tier Applications With Entity Framework Services 2010Building N Tier Applications With Entity Framework Services 2010
Building N Tier Applications With Entity Framework Services 2010
 

Último

From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsRoshan Dwivedi
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 

Último (20)

From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 

Back-2-Basics: .NET Coding Standards For The Real World

  • 1. Back-2-Basics: .NET Coding Standards For The Real World
  • 2.
  • 5. Why Do You Need Standards? First, you might not agree witheverything I say… that’s okay! Pick a standard for your company Every programmer on the same page Easier to read and understand code Easier to maintain code Produces more stable, reliable code Stick to the standard!!!
  • 6. Coding Standard After selecting a standard: Make sure it’s easily available to each programmer Print or electronically Enforce via code reviews Provide programs to make it easier for programmers to maintain: StyleCop – Free for C# programmers. CodeIt.Right – Enterprise edition shares profiles. Can create custom profiles for your company.
  • 7. Real World Analysis Example Scenario: In production Client Server Application with millions in sales Nine projects of 183,177 lines of .NET code written by multiple programmers (junior to principal) StyleCop Analyze Code.It Right 7,076 < 1,100 6,216 < 2,527 5,091 < 1,837 This is why you need to follow good coding practices throughout the lifecycle of the project!
  • 8. Code.It Right Stats All Important
  • 10. General Assembly Tips Never hardcode strings that might change based on deployment such as connection strings Best place to put them are in user or application level Settings in your application. Remember to save on exit and load on start! Automatic in VB.NET  Other locations can include: Registration Database, Databases and more… DEMO
  • 11. General Assembly Tips Sign (strong name) your assemblies, including the client applications. Also, sign interop assemblies with the project’s .snk file Name assemblies in the following format: <Company>.<Component>.dll Project names should be named the same. Microsoft.VisualBasic.dll dotNetTips.Utility.dll Acme.Services.References.dll
  • 12. Mark as CLS Compliant Forces your assembly to be compliant with the Common Language Specification (CLS). Assemblies, modules, and types can be CLS-compliant even if some parts of the assembly, module, or type are not CLS-compliant, as long as these two conditions are met: If the element is marked as CLS-compliant, the parts that are not CLS-compliant must be marked using CLSCompliantAttribute with its argument set to false. A comparable CLS-compliant alternative member must be supplied for each member that is not CLS-compliant. DEMO
  • 14. Elements of the Same Type Should appear in this order: public elements protected elements internal elements private elements Use #region/ #endregion to group namespace-level and class-level elements Use separate regions to organize the private, protected, and internal members.
  • 15. Namespaces The general rule for naming namespaces is to use the company name followed by the technology name and optionally the feature and design CompanyName.TechnologyName[.Feature][.Design]
  • 16. Enums Use abbreviations sparingly for Enums and their values Do not use an Enum suffix on Enum type names Use a singular name for most Enum types, but use a plural name for Enum types that are bit fields
  • 17. Enums Always add the FlagsAttribute to a bit field Enum type Avoid providing explicit values for Enums (unless necessary) Avoid specifying a type for an Enum Public Enum WebServiceAction GetCurrentMode PauseService ResumeService StartService StopService End Enum
  • 18. Interfaces Prefix interface names with the letter “I” Do not use the underscore character. Use abbreviations sparingly. Challenging to version over releases The smaller, more focused the interface the better public interface IDisposable { // Methods void Dispose(); }
  • 19. Classes Use a noun or noun phrase to name a class. Do not use a type prefix, such as C for class, on a class name. Do not use the underscore character (_). Avoid putting multiple classes in a single file. A file name should reflect the class it contains. Provide a default private constructor if there are only static methods and properties on a class. Use Static Class in C# or a Module in VB.NET Explicitly define a protected constructor on an abstract base class.
  • 20. Events Use Pascal case Use an EventHandler suffix on event handler names. Name an event argument class with the EventArgs suffix. Use EventHandler<> to declare handlers. Be careful with non-UI threads calling back to the UI! Use the BackgroundWorker Control in Windows Forms DEMO
  • 21. Member Variables (Fields) Use camel case as a rule, or uppercase for very small words Prefix private variables with a "_” Member variables should not be declared public Use a Property instead Const member variables may be declared public
  • 22. Properties Use Pascal case Use a noun or noun phrase to name properties Properties that return arrays or collections should be methods. Do not use write-only properties Consider providing events that are raised when certain properties are changed. Name them <Property>Changed Properties should not have dependencies on each other Setting one property should not affect other properties Properties should be settable in any order. DEMO
  • 23. Constructors Do not call code from a constructor! Can cause Exceptions. Capture parameters only. Provide a constructor for every class. Do not use the this./ Me. reference unless invoking another constructor from within a constructor. Provide a protected constructor that can be used by types in a derived class.
  • 24. Destructors Avoid using destructors! It is not possible to predict when garbage collection will occur. The garbage collector has no knowledge of unmanaged resources such as window handles, or open files and streams. Implement IDisposible if you do! Use the try/finally (or Using) to make sure that unmanaged resources are disposed of even if an exception interrupts your application. DEMO
  • 25. Methods Use Pascal case Use verbs, verb phrases or verb-object pair to name methods Avoid methods with more than five arguments Use structures for passing multiple arguments Always mark public and protected methods as virtual/Overridable in a non-sealed class. Methods with return values should have a name describing the value returned DEMO
  • 26. Method Overloading Use method overloading to provide different methods that do semantically the same thing. Use method overloading instead of allowing default arguments. Default arguments do not version well and therefore are not allowed in the Common Language Specification (CLS). Overloaded methods should call the most complete method If you provide the ability to override a method, make only the most complete overload virtual and define the other operations in terms of it DEMO
  • 27. Do Not Expose Generic List List is a generic collection designed for performance not inheritance and Applies to public API’s Does not contain any virtual members Can not tell when updated Use Instead: System.Collections.ObjectModel.Collection System.Collections.ObjectModel.ReadOnlyCollection System.Collections.ObjectModel.KeyedCollection Use Interfaces: IDictionary DEMO
  • 28. Stop Exceptions BEFORE They Happen! Defensive Programming
  • 29. Prevent Exceptions Practice Defensive Programming! Any code that might cause an exception (accessing files, using objects like DataSets etc.) should check the object (depending on the type) so an Exception is not thrown For example, call File.Exists to avoid a FileNotFoundException Check and object for null Check a DataSet for rows Check an Array for bounds Check String for null or empty DEMO
  • 30. Parameters Always check for valid parameter arguments Perform argument validation for every public or protected method Throw meaningful exceptions to the developer for invalid parameter arguments Use the System.ArgumentException class Or your own class derived from System.ArgumentException DEMO
  • 31. Enums Never assume that Enum arguments will be in the defined range. Enums are just an Int32, so any valid number in that range could be sent in! Always use Enum.IsDefined to verify value before using! DEMO
  • 32. TryParse Use the .TryParse method on value types when assigning from a string. WILL NOT cause an exception! Returns result of True/False DEMO
  • 33. Exceptions When doing any operation that could cause an exception, wrap in Try - Catch block Use System.Environment.FailFast instead if unsafe for further execution Do not catch non-specific exceptions (for common API’s) Use Finally for cleanup code When throwing Exceptions try using from System instead of creating custom Exceptions Use MyApplication_UnhandledException event in VB.NET WinForm apps Use Application_Error event in ASP.NET apps
  • 35. Variables Avoid single character variable names i, t etc. Do not abbreviate variable words (such as num, instead of number) Unless they are well known like Xml, Html or IO If deriving from a core type, add the suffix of the identify type. ArgumentException or FileStream Use camel case for local variables firstName
  • 36. Accessing Class Member Variables Preface all calls to class members with this./Me., and place base./MyBase. before calls to all members of a base class Class BaseClass Public Sub ProcessData() End Sub End Class Class MainClass Inherits BaseClass Public Sub AnalyzeData() End Sub 'Correct usage of this. and base. Public Sub Good() Me.AnalyzeData() MyBase.ProcessData() End Sub 'Incorrect usage. Public Sub Bad() AnalyzeData() ProcessData() End Sub End Class
  • 37. Strings When building a long string, always (almost) use StringBuilder, not string! C# StringBuilder builder = new StringBuilder("The error "); builder.Append(errorMessage); // errorMessage is defined elsewhere builder.Append("occurred at "); builder.Append(DateTime.Now); Console.WriteLine(builder.ToString()); VB Dim builder As New StringBuilder("The error ") builder.Append(errorMessage) 'errorMessage is defined elsewhere builder.Append("occurred at ") builder.Append(DateTime.Now) Console.WriteLine(builder.ToString()) DEMO
  • 38. Parameters Use descriptive parameter names Parameter names should be descriptive enough such that the name of the parameter and its value can be used to determine its meaning in most scenarios Do not prefix parameter names with Hungarian type notation Public Function ManageIIs(ByVal server As String, ByVal userName As String, ByVal password As System.Security.SecureString, ByVal domain As String, ByVal instance As String, ByVal action As IIsWebServiceAction) As Int32 End Function
  • 39. Generic Type Parameters If possible use descriptive type parameters for generic parameters Prefix with T Use only T, K etc if is self-explanatory C# public static string ConvertArrayToString<TArray>(TArray[] array) where TArray : IEnumerable { } VB Public Shared Function ConvertArrayToString(Of TArray As {IEnumerable})(ByVal array As TArray()) As String End Function DEMO
  • 40. Commenting Comment your code! While coding or before Keep it short and understandable Mark changes with explanation, who changed it and the date (if that is your company standard) NEVER WAIT UNTIL AFTER YOU ARE DONE CODING!
  • 41. Xml Commenting Now supported by VB.NET and C#! Comment all public classes and methods! XML can be turned into help docs, help html with applications like Sandcastle http://sandcastle.notlong.com Very useful for teams and documentation for users. Make this easy by using GhostDoc http://ghostdoc.notlong.com DEMO
  • 42. Let’s See What We Have Learned/ Know What’s Wrong With This Code?
  • 43. Constructor public class FileCache { public FileCache(string tempPath)   {    var appData = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),Global.UserDataFolder); var cacheDir = Path.Combine(appData, tempPath);    if (Directory.Exists(cacheDir) == false) {      Directory.CreateDirectory(cacheDir);   }    } } public class FileCache { public FileCache(string tempPath)   {    this.TempPath = tempPath;   } } 43
  • 44. Method public static string SQLValue(string parameter, string value) {   if (String.IsNullOrEmpty(parameter))   {   throw new ArgumentNullException("parameter");   }     if (string.IsNullOrEmpty(value))     return (parameter + " = NULL");   else     return (parameter + " = '" + value.Trim() + "'"); } public static string SQLValue(string parameter, string value) { if (String.IsNullOrEmpty(parameter))    {      throw new ArgumentNullException("parameter");     }     if (string.IsNullOrEmpty(value))    return (String.Format("{0} = NULL", parameter));   else     return (String.Format("{0} = '{1}'", parameter, value.Trim())); } public static string CreateSQLParameter(string name, string value) { if (String.IsNullOrEmpty(name))   {    throw new ArgumentNullException(”name");   }     return string.IsNullOrEmpty(value) ? (String.Format("{0} = NULL", name)) : (String.Format("{0} = '{1}'", name, value.Trim())); } public static string SQLValue(string name, string value) { if (string.IsNullOrEmpty(value))    return (name + " = NULL");   else    return (name + " = '" + value.Trim() + "'"); }
  • 45. Exception private void UpdateVendor() { try { //Code that causes and exception } catch (ValidationException ex) { //Clean up code throw ex; } catch (Exception ex) { LogWriter.WriteException(ex, System.Diagnostics.TraceEventType.Error, this.Name); } }
  • 46. Structure Public Structure UserInfo Private _contactName As String Public Property ContactName() As String Get Return _contactName End Get Set(ByVal value As String) _contactName = value End Set End Property Private _email As String Public Property Email() As String Get Return _email End Get Set(ByVal value As String) _email = value End Set End Property Public Overrides Function ToString() As String Return Me.ContactName End Function Public Overloads Overrides Function GetHashCode() As Integer Return Me.ContactName.GetHashCode Or Me.Email.GetHashCode End Function Public Overloads Overrides Function Equals(ByVal obj As [Object]) As Boolean Dim testObject As UserInfo = CType(obj, UserInfo) If Me.ContactName = testObject.ContactName AndAlso Me.Email = testObject.Email Return True Else Return False End If End Function End Structure Public Structure UserInfo Private _contactName As String Public Property ContactName() As String Get Return _contactName End Get Set(ByVal value As String) _contactName = value End Set End Property Private _email As String Public Property Email() As String Get Return _email End Get Set(ByVal value As String) _email = value End Set End Property Public Overrides Function ToString() As String Return Me.ContactName End Function Public Overloads Overrides Function GetHashCode() As Integer Return Me.ContactName.GetHashCode Or Me.Email.GetHashCode End Function End Structure Public Structure UserInfo Private _contactName As String Public Property ContactName() As String Get Return _contactName End Get Set(ByVal value As String) _contactName = value End Set End Property Private _email As String Public Property Email() As String Get Return _email End Get Set(ByVal value As String) _email = value End Set End Property Public Overrides Function ToString() As String Return Me.ContactName End Function End Structure Public Structure UserInfo Private _contactName As String Public Property ContactName() As String Get Return _contactName End Get Set(ByVal value As String) _contactName = value End Set End Property Private _email As String Public Property Email() As String Get Return _email End Get Set(ByVal value As String) _email = value End Set End Property End Structure
  • 47. Enum public enum MergeTypes { InsCo = 1, Agents = 2, Vendors = 3 } public enum MergeType { InsuranceCompanies, Agents, Vendors } public enum MergeType { None, InsuranceCompanies, Agents, Vendors }
  • 48. Fields public class Contact { protected string mNamePrefix = ""; protected string mFirstName = ""; protected string mLastName = ""; protected string mPhone1 = ""; protected string mExtension1 = ""; protected string mEmailAddress = ""; //Code } public class Contact { private string _namePrefix = string.Empty; protected string NamePrefix { get { return _namePrefix; } set { _namePrefix = value; } } //Code }
  • 49. Events public delegate void ReportListEventHandler(object sender, ReportListEventArgs e); public event ReportListEventHandler ReportSelected; public event EventHandler<ReportListEventArgs> ReportSelected;
  • 51. Products To Help Out StyleCop http://stylecop.notlong.com CodeIt.Right http://codeitright.notlong.com FXCop http://fxcop.notlong.com Or Use Analyze in VS Team Systems Refactor Pro! For Visual Studio http://refactorpro.notlong.com I Use All 4! 51
  • 52. Resourses (Besides My Book) Design Guidelines for Class Library Developers http://DGForClassLibrary.notlong.com .NET Framework General Reference Naming Guidelines http://namingguide.notlong.com

Notas do Editor

  1. C# Use Style Cope to enforce.
  2. DEMO: DefensiveProgramming.vb - SafeIntegerSet
  3. The original exception object should not be re-throwed explicitly. Violation of this rule will complicate debugging and ruin the exception's stack trace.
  4. ToStringGetHashCodeEquals
  5. Or use an automatic property!