between various types of components within your project. The Architecture Explorer lets you deep
dive into different areas while still leaving a trail of breadcrumbs to help you understand where
you are. The ability to generate Sequence Diagrams lets you quickly understand how a particular
method behaves, especially as it relates to other methods and classes.
the architecture explorer
One of the hardest aspects of navigating a new code base is understanding where you are in
relation to everything else. The Architecture Explorer window (Figure 54-15) allows you to move
very quickly through the code structure with single clicks, leaving a trail that always makes it easy to
figure out how you got to wherever you end up. Some elements can be dragged from the Architecture
Explorer directly onto the design surfaces of many of the other diagrams in this chapter.
fiGure 54-14
exploring Code .
1069
fiGure 54-15
1070 .
chaPter 54 ViSuAl STudio ulTimATe For ArchiTecTS
Figure 54-15 shows the progression of columns, each of which contains a series of nodes. Each time
you click a node in a column, a new column opens up based on the node selected. Between each of the
columns is a collapsed Action column, which you can expand by clicking it (Figure 54-16). Selecting
a different action allows you to change the next step in the navigation path. In some cases, actions
actually perform some task outside of the Architecture Explorer. Double-clicking a node will often
open it in the editor window.
fiGure 54-16
You can select multiple nodes in a column to see a union of the results in the next
column.
Each column in the Architecture Explorer can be independently filtered in one
of two ways. The simplest way is to type into the textbox at the top of
the column and press Enter. This filters the content based on the information
you entered. The other way is to use the Category and Property Filter box
(Figure 54-17) by clicking the small filter icon in the top-left of the column.
When a column is filtered, it will have a large filter icon in its background.
The first column is the Architecture, which is a special column that contains
views, each of which belongs to a domain. The Solution domain offers the
Class View, which is based on a logical view of your classes, and the Solution View, which is based
on the physical layout of your files. The File System domain allows you to load compiled assemblies
from disk and analyze them in the Architecture Explorer.
The Saved DGQL Queries domain offers access to previously saved queries. These queries can be
saved as a part of your solution or they can be located in your Documents\Visual Studio 10\
ArchitectureExplorer\Queries folder. To save a query, use the Save Query to Favorites button on
the left of the Architecture Explorer.
dependency Graphs
When you inherit an existing code base, one of the more diffi cult tasks is trying to fi gure out the
dependencies between the different assemblies, namespaces, and classes. A Dependency Graph allows
you to visualize the dependencies between items at different levels of focus. The easiest way to create
a Dependency Graph is the Architecture . Generate Dependency Graph menu. This option allows
fiGure 54-17
exploring Code .
1071
you to create a Dependency Graph by assembly, by namespace, by class, or by some custom criteria
that you can define. Four basic options specify the way a Dependency Graph is arranged based on the
direction of arrows: top to bottom, left to right, bottom to top, and right to left. Figure 54-18 shows
an example of the Left to Right view. There is also a Quick Clusters view, which attempts to arrange
the items so that they are closest to the things they are connected to.
fiGure 54-18
In addition to these views, there is a Dependency Matrix view (Figure 54-19), which shows a
colored square whenever the item in the row depends on the item in the column.
fiGure 54-19
Visual Studio 2010 includes three analyzers for dependency graphs that can help you find potential
problems with a code base. These are enabled and disabled under the Analyzers context menu and
will update the legend to colorize any items that match criteria defined by the analyzer.
1072 .
chaPter 54 ViSuAl STudio ulTimATe For ArchiTecTS
.
Circular References: These can become difficult to detect, especially as the number of
components involved grows. This analyzer will detect strongly connected items and colorize
them.
.
Show Hubs: A hub is a component that many other components depend on. These can be
difficult to change because the impact of most changes tends to ripple out through the rest
of the system.
.
Unreferenced Nodes: These are nodes that no other node takes a dependency on. This can
be an issue because it might indicate that the item it represents is not used by anyone else. It
may also mean that the items that are dependent on it are not in the diagram.
Generate sequence diagram
A .NET Sequence Diagram allows you to model the
implementation of a C# or VB method. The best way to create a
.NET sequence diagram is to right-click a method signature in
the editor window and select Generate Sequence Diagram
to display the Generate Sequence Diagram dialog shown in
Figure 54-20.
Once you click the OK button, Visual Studio analyzes
your project and produces a .NET Sequence Diagram as in
Figure 54-21. You can make changes to this sequence diagram
without affecting the underlying code, so you can use this
diagram to rapidly try out different ideas about how the code
should work. fiGure 54-20
fiGure 54-21
summary .
1073
You can add a .NET Sequence Diagram to any .NET project by using the Add
New Item dialog. If you add a Sequence Diagram this way, it will not be related
to a specific .NET method.
suMMary
Modeling Projects provide a great way for you to communicate the design of your project clearly,
unambiguously, and effectively. You can use Use Case Diagrams, Activity Diagrams, and Sequence
Diagrams to model user requirements from a number of different perspectives. You will use
Component Diagrams, Class Diagrams, and Layer Diagrams to model the structure of your
application. The ability to verify that your application meets the architecture as designed by the
Layer Diagram can be a useful sanity check to ensure project quality standards remain high and
architectural decisions are not abandoned once the project gets underway.
Getting up to speed with an existing code base can be very hard. Directed Graphs are an easy
way to identify the relationships between various parts of your application. The new Architecture
Explorer allows you to rapidly move through the connections between components in the system to
find the items that you are looking for. Finally, the ability to generate a Sequence Diagram from an
existing method allows you to quickly grasp the fundamentals of how a method interacts with other
methods and classes within the application.
55 55
Visual studio Ultimate
for Developers
what’s in this chaPter?
.
Analyzing code for potential problems
.
Profiling applications to find bottlenecks
.
Developing database projects
The Premium and Ultimate editions of Visual Studio 2010 have many advanced features
for developers mainly designed to improve quality and facilitate database development. The
quality tools include code metrics, static analysis, and profiling tools. It’s not that you can’t
develop quality software with other editions or that using these tools will make sure your
software performs well. Obviously, there’s more to it than using a few tools, but these can be
of great help and will reduce the time invested in other tasks like code review and debugging.
The most interesting new debugging tool is called IntelliTrace, which allows you to capture
environment information associated with failed test cases.
Chapter 26 already examined some of the tools available for working with databases in Visual
Studio 2010. This chapter looks at two main areas: SQL-CLR development, and tools to help
teams working with databases. The first will aid in developing and deploying .NET code
that will be hosted inside SQL Server. The second will allow you to version schema changes,
isolating developers and allowing them to compare changes, auto-generate data, and share
their modifications easily with other developers or DBAs.
code Metrics
Code metrics serve as a reference to know how maintainable your code is. Visual Studio
2010 provides five metrics for your source code, which are all shown in the Code Metrics
window (Figure 55-1). To open this window use the Analyze . Calculate Code Metrics or
1076 .
chaPter 55 ViSuAl STudio ulTimATe For deVeloperS
the Analyze . Windows . Code Metric Results menu. Once the window is displayed, you can
use the toolbar button in the top left to recalculate the metrics.
fiGure 55-1
Directly from the list, you can filter any of the metrics to show methods that fall within a specified
range, export to Excel, configure columns to remove metrics, or create a Team Foundation Server
work item. Export to Excel is particularly useful to generate reports using pivot tables or to work
with a flat view of the information using filters and sorts. For example, if you want to look for
methods with more than 15 lines of code, filtering directly in the Code Metrics window will get
you a lot of namespaces and types, but you will have to expand each to see whether there are any
methods, whereas in Excel you can easily filter out namespaces and types and only look at methods.
As you use the metrics to make decisions about your code, bear in mind that the actual values are not
as important as relative values. Having a rule that states “all methods must have less than 25 lines
of code” is not as useful as one that makes relative statements such as “prefer shorter methods.” You
should also consider the changing values as important, so if your average Maintainability Index is
going down it might be a sign you need to focus on making code easier to maintain.
For each metric except for Maintainability Index, lower numbers are considered
to be better.
lines of code
The name is self-explanatory; however, it’s worth mentioning that the purpose of this metric should
be only to get a clue of the complexity of the code, and must not be used to measure progress.
Clearly, a method with five lines of code that calls other methods will be simpler than if you inline
all 25 lines of code in that method.
Code Metrics .
1077
public class OrdersGenerator
{
public void GenerateOrder(Order order)
{
IsUnderCreditLimit(order);
IsCustomerBlocked(order.Customer);
AreProductsInStock(order);
IsCustomerBlocked(order);
SaveOrder(order);
}
// remaining methods are omitted.
}
If you compare a class with six methods, as shown in the preceding code, with a class having
the same functionality, but with all the code inlined in one method, the latter will have 25 lines.
Assuming the remaining methods have five lines each, the former will be 30 lines long, although it
is simpler. You have to be careful about how to consider this metric; a longer class might be better
than a short one.
Use the Extract Method refactoring discussed in Chapter 8 to reduce this metric.
Be sure to keep an eye on extracted methods to see if they might be better off in
a new class.
depth of inheritance
This metric counts the base classes; some recommendations are to have a value lower than six.
But this, like other metrics, has to be looked at with special care. It’s hard to give a recommended
value and it’s relative to which classes you are inheriting from. If you inherit from LinkLabel,
you will have a depth of 4, but your base classes are less likely to change than if you inherit from
ProviderXComponent and have a depth of 1. It’s more probable that ProviderX will change his
component and break yours, while Microsoft will take more care not to break code. But you’ll
probably never update ProviderX’s library. The point is that this metric is relative to what base
classes you have.
class coupling
This counts the dependencies an item has on other types except for primitives and built-in types
like Int32, Object, and String. The more dependencies you have, the harder it’s supposed to be to
maintain, because it would be more probable that changes on other types will cause a break in
your code. Similarly to depth of inheritance, the importance you give is relative to the dependencies
you have. A class referencing System libraries is less likely to have a break than classes referencing
other types on active development. You can see a value for this metric at each level of the hierarchy
(project, namespace, type, and member).
1078 .
chaPter 55 ViSuAl STudio ulTimATe For deVeloperS
cyclomatic complexity
Cyclomatic Complexity is a measure of how many paths of execution there are through your code.
A method with higher cyclomatic complexity is going to be harder to understand and maintain than
one with a lower value. It is hard to find a recommended value for this metric because it depends on
the level of your team and on the team that will maintain the product. Far more important is trending
information — a steadily increasing cyclomatic complexity indicates that your code is getting harder
to understand and follow. Having said that, sometimes a complex solution is warranted.
Maintainability index
This metric is calculated using a formula that considers cyclomatic complexity, lines of code, and
the Halstead volume, which is a metric that considers the total and distinct number of operators
and operands. It will give you a range between 0 and 100, with the higher being easier to maintain
than the lower.
excluded code
Code marked with the CompilerGenerated and GeneratedCode attributes won’t be considered
in the metrics. Datasets and Web Service Proxies are examples of code marked with the