ITF11006.NET Industrial Programming
Guidelines – Development Guidelines – UX Guidelines Quality Measures – Code Metrics – Code Analyses Robustness – Error handling (exceptions) – Resource handling
Guidelines Design Guidelines for Developing Class Libraries ( Design Guidelines for Developing Class Librarieshttp://msdn.microsoft.com/en-us/library/ms aspx – Guidelines for Names Capitalization Conventions ( Capitalization Conventionshttp://msdn.microsoft.com/en-us/library/ms229043(v=vs.100).aspx) Use Pascal casing ALLWAYS Except, use camel casing for: – parameter names – variables and (none-public) fields – Member Design Guidelines Choosing Between Properties and Methods ( Choosing Between Properties and Methods – Never public fields (except for structs) Documentation of code
Documentable Entities – Class / Struct – Method –…–… Cannot document Namespace through XML tag Shows up in – Object Browser – Intellisense – Library Documentation ( Library Documentation
Documentation of code (cont.) XML tags ( XML tags – summary – param – example – code – exception – returns GhostDoc ( GhostDoc
Guidelines Design Guidelines for Developing Class Libraries ( Design Guidelines for Developing Class Librarieshttp://msdn.microsoft.com/en-us/library/ms aspx – Guidelines for Names Capitalization Conventions ( Capitalization Conventionshttp://msdn.microsoft.com/en-us/library/ms229043(v=vs.100).aspx) – Member Design Guidelines Choosing Between Properties and Methods ( Choosing Between Properties and Methods Documentation of code UX Guidelines
Windows Size – Default – Min – Max Windows Position Resizing must work! Clean layout Shortcuts Tab-order
Industrial Programming Guidelines – Development Guidelines – UX Guidelines Quality Measures – Code Metrics – Code Analyses Robustness – Error handling (exceptions) – Resource handling
Code Metrics Cyclomatic complexity – Structural complexity of code – Calculates number of different code paths – High values requires more unit tests to cover all paths Class Coupling – Measures the Efferent Coupling, the number of classes this method depends on – High coupling means brittle, likely to change, probably unfocused and lack of responsibility Lines of Code – Based on IL, so approximate, but better than source – Excludes white space, comments, braces, types etc.
Code Metrics (cont.) Maintainability Index: – A weighted number based on Halstead volume, cyclomatic complexity and lines of code – Maintainability Index = MAX(0,( * ln(Halstead Volume) * (Cyclomatic Complexity) * ln(Lines of Code))*100 / 171) – A green rating is between 20 and 100 and indicates that the code has good maintainability. A yellow rating is between 10 and 19 and indicates that the code is moderately maintainable. A red rating is a rating between 0 and 9 and indicates low maintainability.
Code Metrics MetricGreenYellowRed Maintainability Index <40 Cyclomatic complexity< >20 Class Coupling< >20 Lines of Code< >30 Use MS official sample – “Image slideshow in full screen mode” ( )
Code Metrics – VS 2010
Code Metrics – Excel
Code Analyses Rule Set Rule Action Automatic / Manual execution Use from Start
Code Analyses - Settings
Code Analyses – Results/Fix
Code Analyses – Requirements Microsoft Minimum Recommended Rules +Microsoft.Naming (All) HiOf.Net.ruleset
Industrial Programming Guidelines – Development Guidelines – UX Guidelines Quality Measures – Code Metrics – Code Analyses Robustness – Defensive Programming – Error handling (exceptions) – Resource handling
Defensive Programming Handle anticipated deviations – Missing Directory / File – Lack of database connectivity – Lost database connection Verify input data – Do not let the user enter erroneous data Guard against SQL injection – Use SQL procedures / bind variables
Exception Handling
Catching Exceptions
Exceptions Design Guidelines for Exceptions ( Design Guidelines for Exceptions Do not throw System.Exception Inherit from System.Exception End with Exception
User Defined Exceptions public class NewException : BaseException, ISerializable { public NewException() { // Add implementation. } public NewException(string message) { // Add implementation. } public NewException(string message, Exception inner) { // Add implementation. } // This constructor is needed for serialization. protected NewException(SerializationInfo info, StreamingContext context) { // Add implementation. }
Exceptions Exception handling Throwing exceptions Exception info – Message – InnerException Library code rethrows/do not catch Clients shall handle exceptions
Resource handling Unmanaged resources – Implementing IDisposable – Consuming Using vs. try/finally Closing vs Disposing – Database, files, …