Volume 31 Number 12
[Visual Studio Development]
Productivity Enhancements in Visual Studio 2017 RC
By Kasey Uhlenhuth; 2016
If you want to be more productive, you have to save more time or more effort on the tasks you perform every day. Visual Studio 2017 RC boosts your productivity with automated tasks and improved navigation, as well as with immediate feedback on the quality and state of your code.
The strides Visual Studio has made in refactorings, code generation, code analysis, navigation, testing, and debugging for .NET developers were made possible by project “Roslyn,” a six-year undertaking to re-architect the C# and Visual Basic compilers. Visual Studio 2017 RC leverages the Visual Studio 2015 investment in Roslyn to crank out tons of cool new features.
When developers drill into a bug, discover the implications of a refactoring, or work to grok an unfamiliar code base, they rely on the accuracy and ease of code navigation. This release delivers a greatly improved navigation experience—with Find All References, GoTo and Indent Guides—to get you from A to B with greater confidence and fewer distractions (even in large code bases).
Previously, Find All References would discover all usages of a symbol and then output the results as a simple, flat list in the Results Window. Now, as Figure 1 shows, the references are colorized and arranged in a hierarchical display with custom grouping, sorting, filtering and searching to help you rapidly home in on the particular reference you desire—even when there are many references in the list. Furthermore, hovering over a reference displays a tool tip to give foresight into where you’ll be navigating to before you actually go there. You can save or “lock” your Find All References results for situations when you need to run the command several times but want your original results to persist (for example, when investigating and drilling down into the impact of a refactoring).
Figure 1 Find All References Improvements Include Colorization, Custom Organization and Hover Previews
To save even more time, learn the handy keyboard shortcuts shown in Figure 2.
Figure 2 Keyboard Shortcuts for General Navigation
|Go To Definition||Peek Definition||Find All References||Go To Implementation||Go To All (File/Type/Member/Symbol)|
|F12||Alt+F12||Shift+F12||Ctrl+F12||Ctrl+T or Ctrl+,|
Another feature that has undergone significant improvement in this release is GoTo (formerly Navigate To). GoTo All is a fast, complete search for any file, type, member or symbol declaration in a solution. Icons at the bottom of the search bar allow you to filter your results by group or adjust the scope of the search, as shown in Figure 3. (But if you prefer the keyboard over the mouse, you can also take advantage of a query syntax that lets you filter directly by typing a simple prefix). The gear icon in the bottom-right corner lets you customize a handful of settings, including the placement of the search bar, a live preview of the files containing the highlighted result, and additional file information for each result.
Figure 3 Use Icons or Query Syntax to Filter Results by Files, Types, Members or Symbols
Finally, you might recognize Indent Guides from the popular Productivity Power Tools extension. Now, as part of the core product, the dotted, gray vertical lines serve as landmarks in code to provide context within your frame of view. When hovering over a bottom brace, you get a colorized preview of the top matching brace, and when hovering over the guide itself, you get a colorized preview of the code surrounding and containing the matching top brace. These features aim to give you a better sense of your code “geography” and context without scrolling or navigating. Indent Guides also work well with the existing outlining features in Visual Studio and even offer more regions that are expandable and collapsible.
You can also enable “Map Mode” on your scroll bar. This transforms the scrollbar into a “map” of your code, allowing you to view a miniature version of the entire document—including errors, breakpoints and so forth. Hovering over any part of the “map” will display a preview of the code at that point in the document.
And, again, you can increase your efficiency by learning the useful keyboard shortcuts shown in Figure 4.
Figure 4 Keyboard Shortcuts for GoTo
|Go To All||Go To Line||Go To File||Go To Type||Go To Member||Go To Symbol|
|Shortcut||Ctrl+T or Ctrl+,||Ctrl+G||Ctrl+1, F||Ctrl+1, T||Ctrl+1, M||Ctrl+1, S|
|Query Prefix||No prefix||:||f||t||m||#|
Writing and Reading Code
Along with navigation, developers spend a lot of time writing and reading code. Visual Studio 2017 RC focuses on facilitating the writing of correct code, as well as maintaining the readability of developer code bases. Building on features in Visual Studio 2015, this release provides a refined IntelliSense experience, more refactorings and code fixes, and customizable code-style configuration and enforcement.
IntelliSense The goal of Visual Studio is to assist rather than hinder the various code-writing workflows that are being used today. One of the most obvious ways Visual Studio makes you productive is with IntelliSense (see Figure 5). Visual Studio 2017 RC updates the IntelliSense experience with several enhancements: smart preselection, filtering and XAML support.
Figure 5 IntelliSense Now Has Highlighting and Filtering
Smart preselection will determine the “target type” required at a position in code and will preselect items in the IntelliSense completion list matching that type. This speeds your typing flow and removes the burden of having to figure out the expected type at a given location. IntelliSense filtering allows you to filter the completion list by category; for example, you can filter out extension methods or view only events. This feature boosts productivity when you’re working in a large code base where there are many items in the completion list or when dealing with unfamiliar code.
Finally, this release delivers a whole new experience for XAML IntelliSense to help developers bind quickly and correctly and see only relevant information. This smarter completion experience includes completion when binding events, paths and functions with x:Bind; camelCase matching support (for example, “RTB” will complete as “RichTextBox”); and namespace prefix autocompletion.
Code Analysis Visual Studio 2015 introduced the live code analysis feature, which enables “as-you-type” feedback on your code. This lets you learn about issues early, before they build, rather than accumulating a set of problems you might never get around to fixing. To resolve the errors identified in live code analysis, you use the lightbulb menu or the shortcut “Ctrl+.” to access code fixes and refactorings. Visual Studio 2017 RC takes live analysis and code fixes a step further by amplifying the set of refactorings and code fixes available, and by introducing code style analyzers that identify style issues in code as soon as they’re typed.
Visual Studio 2015 included some core refactorings: extract method or interface, change method signature, inline temporary variable, introduce local variable, and remove unnecessary usings and imports. Visual Studio 2017 RC expands the set of refactorings and fixes to help you maintain a readable code base and catalyze your development workflows. For example, a significant number of developers initially write all their classes, interfaces, and other types in a single file and then extract each type into a file with the matching name later. Visual Studio 2017 RC expedites this process with the refactoring option “Move Type To Matching File.” Other refactorings you can look forward to include:
- Sync file and type name
- Convert property to method
- Use object initializer
- Convert null-check + throw to use ?? + throw
- Convert string.Format to interpolated string
- Make method synchronous
- Add missing case
- Add braces
Additionally, this release introduces some basic code analysis and fixes for XAML. Using the same lightbulb mechanism in C# and Visual Basic, you can sort and remove unnecessary namespaces and add missing namespaces in your XAML files.
Maintaining a consistent, readable code base is a challenging endeavor. There are good reasons to aspire to a readable code base: If all code looks consistent, it’s easier to onboard new developers; and if all code looks consistent, code reviews can focus on logic rather than on formatting and style minutiae. Visual Studio 2017 RC establishes a way to configure code style with built-in style rules and custom naming conventions.
In the RC, you can go to Tools | Options | Text Editor | [C#/Basic] | Code Style | General to see the built-in configurable style rules, which include preferring var over explicit type, predefined over framework type, this over nothing. Each rule comes with a description, a configurable preference and a configurable severity. The different severities work as follows:
- Error displays in the editor as a red squiggle, appears in the Error List and will break the build.
- Warning displays in the editor as a green squiggle, appears in the Error List and will only break the build if the configuration “treat warning as errors” is enabled.
- Suggestion displays in the editor as gray dots, appears in the Error List and will not break the build (see Figure 6).
- None has no display in the editor, does not appear in the Error List and will not break the build.
Figure 6 Enforce Team Style and Naming Conventions in the Editor so You Get Live Diagnostics When You Violate Rules
Because preferences for naming styles vary widely across the .NET developer community, you can create your own custom naming conventions to enforce on your team. Visual Studio 2017 RC provides a set of defaults that can be configured to enforce team conventions, including that members (except fields) should be PascalCase, types should be PascalCase, interfaces start with “I,” and async methods end with “Async.” You can configure and add to these defaults by going to Tools | Options | Text Editor | [C#/Basic] | Code Style | Naming. Naming rules are placed in a grid for configuration just like the standard style rules (note that order matters here—the first matching style rule is applied).
To add a custom naming rule, you click the green plus (“+”) button at the bottom of the rule grid to open the rule creation dialog. You can then use default specifications (for example, “all classes”) and styles (such as, “use prefix _”) to craft a custom naming rule. If the built-in specifications and styles aren’t sufficient to piece together your desired naming convention, you can write your own.
You can also adjust your style rules from the editor by pressing “Ctrl+.” to trigger the lightbulb menu, selecting the rule fix in the menu and then clicking the gear icon in the live code preview.
Supporting unit tests creates an interesting tension between selecting enough tests to ensure code is correct while still running as few tests as possible so you don’t have to wait so long for test results. This tradeoff between correctness and time has often left developers feeling unproductive and frustrated. To mitigate this stress, Visual Studio 2017 RC introduces Live Unit Testing for C# and Visual Basic (see Figure 7).
Figure 7 Live Unit Testing Provides Instant Feedback on Whether Code Is Touched by Passing, Failing or No Tests
Live Unit Testing analyzes data generated at run time to run only impacted tests after an edit and provides immediate feedback on the status of the tests in the editor. These inline visualizations appear on a line-by-line basis:
- If a line of executable code is hit by at least one failing test, it’s decorated with a red “x.”
- If a line of executable code is hit by all passing tests, it’s decorated with a green checkmark.
- If a line of executable code is hit by no tests, it’s decorated with a blue dash.
The live code coverage and test result information provided by Live Unit Testing removes the burden of manually selecting and running tests. The live feedback also serves to notify you instantly if your change has broken the program—if inline visualizations shift from green checkmarks to red x’s, you know you broke a test. At any point in time you can hover over the check or x to see what tests are hitting the given line. Additionally, you can navigate directly to that test by clicking on it in the hover tool tip.
If you’re part of a team that practices test-driven development, Live Unit Testing gamifies the workflow; in other words, all tests will be red and failing at first, and as you implement each method, you’ll see them turn green if they succeed. For all other developers, Live Unit Testing provides visual feedback for when they’ve broken their code.
When all else fails, developers rely on debugging to help them identify the source of an issue. Visual Studio 2017 RC saves you time by reducing the number of actions required to step through a program and to drill into exception information.
Run To Click does exactly what it sounds like; it executes a program until it reaches the target line of code and breaks in debug mode. Essentially, it removes the need for developers to constantly add, hit and remove temporary breakpoints by combining all these actions into one click. To use this feature, you simply need to press the green “run to here” icon that appears to the left of the code line when you hover over it in debug mode.
Perhaps the biggest productivity improvement in the debugger is the new Exception Helper. The redesigned dialog displays the most important information from an exception at the top level, like inner exception details and the expression that returns null, as shown in Figure 8. Visual Studio 2017 RC also enables you to prohibit breaking on exception types in specific cases—allowing you to disregard exceptions thrown from third-party libraries or certain .dlls while debugging.
Figure 8 The New Exception Helper Removes the Need to Drill into Dropdowns to Find the Most Important Information and Actions
Visual Studio 2017 RC focuses on making you more productive by saving you time and effort. I’m thrilled to be able to share improvements in navigation with GoTo and Find All References, enhancements to IntelliSense both in C#/Visual Basic and XAML, an expansion of live code analysis with more refactorings and fixes and the addition of code style, an interactive way of testing with Live Unit Testing, and an efficient debugging experience with the new exception helper. I look forward to hearing your feedback and hope you have a productive experience with Visual Studio 2017 RC.
Kasey Uhlenhuth is a program manager on the .NET and Visual Studio team at Microsoft and is currently working on modernizing the C# developer experience. Previously, she worked on C# Interactive and Node.js Tools for Visual Studio. Reach her at firstname.lastname@example.org or on Twitter: @kuhlenhuth.
Thanks to the following Microsoft technical experts for reviewing this article: David Carmona, Kevin Pilch-Bisson and Mark Wilson-Thomas
Kevin Pilch has worked on Developer Tools at Microsoft for almost 15 years. During that time he has worked on the C# and VB IDE experience, including features like IntelliSense, formatting, refactoring, colorization and more. Most recently he has been the manager of the IDE team for Microsoft's Roslyn project. He loves spending time with his kids, training for marathons, and playing hockey. His weaknesses include beer and chocolate chip cookies. You can find Kevin on Twitter @Pilchie.
Mark Wilson-Wilson is a Program Manager on the Visual Studio IDE team, where he looks after the Visual Studio Editor. He’s been involved in developer tooling for over 10 years, including working on tools for Office, SQL, WPF and Silverlight. You can find him on Twitter @MarkPavWT.