Improving Code Quality • writing better, faster code, in less time with Visual Studio 2010 Code Quality best practices in day-to-day development using Visual Studio 2010. As building software becomes more complex, the probability of exposing end-users to application defects increases exponentially. Software quality begins as soon as you start writing code. Improving code quality is the first line of offense in quality management. Learn how to improve code quality by using Visual Studio tools like Unit Testing, Static Code Analysis, Code Profiler, Test Impact Analysis, and Intellitrace. • Randy Pagels • Sr. Developer Technical Specialist • Microsoft Corporation • Email: email@example.com • Website: http://www.teamsystemcafe.net • Specialist covering Heartland • (MI, OH, KY, and TN) • Team Foundation Server • Visual Studio ALM tools • Visual Studio Test Professional • Lab Management
Improving Code Quality writing better, faster code, in less time!
The Big Picture Release Test Cost of Bugs Development Software Phase
Push Quality Upstream Release Test Cost of Bugs Development Software Phase
Unit Testing • Improve your code quality • Isolate and show that the individual parts are correct • Provides a strict, written contract that must be satisfied • Code Coverage Metrics • Bind to a Data Source • Generate tests from a binary • Execute during automated builds • Contextual Execution • Faster and more reliable
Code Coverage • Verify code paths touched by test cases • A measure in the effectiveness of testing efforts • Helps identify code not covered by tests • Summary data and line level • Part of shared team reports
Code Analysis Overview • Catch common programming errors • Analyze application and database code for defects and security vulnerabilities • Detect code defects early on • Use Code Analysis as part of check-in policy to prevent defects • Improve and maintain code quality using Code Metrics • Manage and triage Code Analysis warnings and errors using the Rule Set editor • Automate Code Analysis as part of your build process
Catch Common Programming Errors What if the parameter ‘order’ is null? Code Analysis will warn the developer of this type of programming error and others like it.
Detect Code Defects Early On Just like compiler warnings, Code Analysis errors are shown in the Error List for easy triage.
Rule Set Editor The new “Rule Set” editor allows you to manage Code Analysis errors and warnings Inline Help system displays detailed information about each rule and provides code examples that will cause the error
Understand Code Quality The Code Metrics window allows you to see the overall “code health” of your application
Today developers have difficulties in identifying Performance Bottlenecks • Is my application using too much CPU? • Is my application using too much memory? • Does my application block on I/O or Database call unnecessarily? • Is my application making unnecessary Database calls?
Evolution of Visual Studio Profiler Sample Profiling Instrumentation Profiling .NET Memory Profiling Reports and Analysis Enhanced Analysis Capabilities Report Comparisons 64-Bit Support Developer Productivity Platform and Technology
Developer Productivity • New Summary and Function Details reports • Editor Integration • Just My Code • Rules and Guidance • Profiling In Progress Window
Summary and Function Details reports Developer Productivity • Actionable performance data presented on the summary page • Actionable performance chart allows the user to identify performance bottlenecks visually and zoom into the non-performing area • Visualized Call Tree and highlighted hot lines in the new Function Details Page
Summary ReportsDeveloper Productivity Easily Locate Performance Spikes Identify Most Expensive Call Path Detect Functions responsible for most work
Function Details ReportsDeveloper Productivity Visualize the call pattern of each method along with corresponding performance data Identify code with considerable performance impact
Tier Interaction ProfilingPlatform and Technology • Analyze the performance of a multi-tier application against its Data tier and SQL Server • Analyze page request patterns and database interactions for web applications Show database queries and requests
Tier Interaction Profiling Platform and Technology Timing information for servicing each page Number of page requests for each ASP.NET page Database B Connection + Number of Queries + total query time for each page Detailed Query Information for each page and each database connection
Difficulty Identifying the “right” tests • Today, there is no tool that identifies the “right” tests in need of re-verification • Developers either • Run more unit tests than needed • Run too few unit tests than needed • Testers either • Verify more tests than needed • Verify too few tests than needed • Which leads to • Lower productivity • Comprise the application’s quality
Test Impact Analysis • Drives quality upstream by preventing bugs from getting into the system earlier in the life cycle. • Developers know the “right” tests impacted by their code change • Testers know the “right” tests to verify for a given build • Enhance development process by requiring verification of impacted automated tests before developers check changes into Source Control System
TIA – Show Impacted Tests One Code Change resulted in 21 impacted Tests
Show Code Changes View impacted tests per code change
Analyze Impacted Tests During Build Enable Test Impact Analysis Build report lists impacted tests and associated code changes
Static Code Analysis Code Profiler Test Impact Analysis
Reduce Cost Downstream Release Test Cost of Bugs Development Software Phase
The world of development… • Developers have poor tools to reproduce problem scenarios and environments • Developers spend too much time sending bugs “over-the-wall” back to testers to get more information • Once a problem has occurred, it’s too late • Why can’t I simply rewind my application? • Why can’t I go back to what caused the problem?
Benefits of IntelliTrace • Low overhead for collection • Enable collection in Dev, Test and Production environments • Pay-as-you-go collection model • Collect as little or much data as performance requirements allow • Diagnose a specific set of user issues • Application’s misuse of the .NET Framework • Class of failures in the application’s code • First-class debugging experience in Visual Studio
What is IntelliTrace? • Application flight recorder and debugger • Similar to SysInternalsFileMon/RegMonexperience • Dynamically instruments code • Two flavors: “Low overhead” and “more in-depth” • Records program execution flow • Records program data (parameters& return values) • Allows the user to analyze and debug code in the familiar Visual Studio debugger
Feature breakdown • IntelliTrace Default Experience • IntelliTraceEvents View • Configuring IntelliTrace • User controllable settings • Collecting call information • “Turning up the dial” to collect richer data • IntelliTrace Calls View • Debugger integration and the navigation gutter • Search history from code
IntelliTrace in Visual Studio 2010 * IntelliTrace is only supported on Microsoft Visual Studio 2010 Ultimate
IntelliTrace™ • Codenamed “Historical Debugging” • Reduces debug “cycling” • Start Debug • Encounter defect • Drop into debugger • Move back through execution history to locate defect • Historical debug trace enables debugging off-machine test runs
IntelliTrace Events The IntelliTrace window collects a list of common uses of the .NET Framework similar to SysInternalsFileMon/RegMon. Clicking the event causes the instruction pointer to highlight the line of source corresponding to the event. The navigation control allows you to step backwards, go forward, and jump back to live debugging mode quickly from any historical context.
IntelliTrace Events The IntelliTrace window collects a list of common uses of the .NET Framework similar to SysInternalsFileMon/RegMon. Clicking the event causes the instruction pointer to highlight the line of source corresponding to the event.
Post-mortem Analysis with IntelliTrace The thread chart gives the developer a timeline view of what took place in the application’s execution. All exceptions that occurred during the execution are summarized and offer the user a “jumping off point” to start a debugging session.
Customizable XML start screen Improved readability Unnecessary gradients removed Reduced clutter Code editor based on WPF Search-as-you-type View Call Hierarchy Redesigned thread window for debugging Simplified one click web deployment Visual Studio IDE Enhancements
Visual Studio IDE Enhancements cont. • WPF Start Page • Enhanced Docking Behavior • Zoom (Ctrl + mouse wheel) • Box Selection (Alt + select with mouse) • View Call Hierarchy (Ctrl + K, T) – design time • Navigate To – in the Edit menu or (Ctrl + Comma) • Quick Search (Ctrl-"comma")-All Spaces treated as wildcards. Camel case. • Highlighting References – select any symbol • Generate From Usage feature lets you use classes and members before you define them. • IntelliSense Suggestion Mode (vs. Completion Mode, Ctrl + Alt + Space to toggle). Use suggestion mode when classes and members are used before they are defined. What's New in the Visual Studio 2010 Editor http://msdn.microsoft.com/en-us/library/dd465268.aspx
Find bugs early in the development cycle Code review by expert framework builders Tweak your use of the .NET Framework Analyze application and database code for defects Use the new “Rule Sets” feature to manage code defects Can be used at the team level with TFS Prevent Code Defects
Find resource bottlenecks CPU performance issues Memory usage Multiple modes of measurement Sampling Instrumentation Memory Allocation Measure, diagnose and fix performance problems across tiers Visualize bottlenecks in concurrent code and analyze resource usage Write Faster Code
Choose the Right Tests • Option to run the Test Impact Data Collector • Identifies tests impacted by code changes from multiple angles • Impacted Tests • Code Changes • Calling Tests • List of changes made since last build • Significantly reduces test time • Run only the necessary tests • Deep integration with Test Case Management