NDepend

Improve your .NET code quality with NDepend

Advanced Code Search : A Case Study

This morning I stumbled on a complex test to write. The need was to create and show a custom Form (written with Windows Form) that relies on the System.ComponentModel.BackgroundWorker to do initialization stuff without freezing the UI. The test is complex because after creating and showing the form, it must wait somehow to release the UI thread for a while to let the BackgroundWorker achieve the RunWorkerCompleted on the UI thread.

I know that this is something we’ve done in the past and I know this is tricky enough to not reinvent the wheel. But with a test suite of over 13.000 tests this is quite challenging to find where we did that. So I decided to use NDepend querying facility to search.

First I analyze all NDepend assemblies, test assemblies included. Then I generate a code query to match all classes that derive from Form. This can be done from the NDepend Search panel : search Form by name in third-party types and then use a right-click menu to generate the code query:

The CQLinq code query generated is:

60 classes are matched:

Let’s refine this query to match all methods that create any of those form classes.This could be achieved by iterating over (all methods) x (all form classes), but the NDepend.API extension method ThatCreateAny() acts like a join and operates in a linear time. For our search scenario, waiting a few seconds to get a search result is not a problem. But for a code rule written with CQLinq, this is important to run it as fast as possible in a few milliseconds, to run all queries and rules often in Visual Studio within a few seconds, hence the query performance entry on the documentation.

280 methods are instantiating some form classes. Let’s refine the query to match only tests method. The cleanest way would be to check for the usage of TestAttribute, but here just checking for parent assemblies names that contain “Test” is enough:

Still 122 test methods matched.

Before filtering the result even more, let’s refine the query to display for each test the form class(es) it instantiates. This can be achieved with a LINQ range variable formsCreated that we use in the result:

We can now browse which form(s) are instantiated by each test:

Finally let’s browse only tests that use some asynchronous related code. Many ways can be used to check for asynchronous usages. The easiest way is certainly to look at methods called by a test method, and check which ones have named related to async stuff. I tried a few words like “Async” “Sync” “Thread” “TimeOut” “Wait”… and “Wait” worked:

In the source code of the highlighted test I had everything I needed for my scenario, including a link to a tricky stackoverflow answer that we found years ago. I found what I needed within a few minutes and had a bit of fun. I hope the methodology and the resulting query can be adapted to your advanced search scenarios.

Service Oriented Architecture, A Dead Simple Explanation

Service Oriented Architecture: A Dead Simple Explanation

Service-oriented architecture (SOA) has been with us for a long time. The term first appeared in 1998, and since then it’s grown in popularity. It’s also branched into several variants, including microservice architecture. While microservices dominate the landscape, reports of SOA’s death have been greatly exaggerated. So, let’s go over what SOA is. We’ll cover why it’s an architectural pattern that isn’t going anywhere. Then we’ll see how you can apply its design concepts to your work. Continue reading Service Oriented Architecture: A Dead Simple Explanation

C#_Features_A_List_of_the_Best_Ones

C# Features: An Exhaustive List of the Best Ones

The first post I wrote for the NDepend blog was about C# 8.0 features. That post inspired a sequel, followed by the series’ final chapter. Those posts dealt with the future. Today’s post, on the other hand, is all about what’s already here. I’ll make a list of the C# features I consider to be some of the best ones. For brevity’s sake, I’ll do the briefest overview of each feature, though.
Also, be warned: I’m going to use “C# feature” loosely throughout this post. So you’ll see some items that are, technically, .NET frameworks features. The reason for this choice is that, for many developers, C# and .NET are interchangeable. That’s the way they think, talk, and google about this topic.
Without further ado, let’s dive into it.

Continue reading C# Features: An Exhaustive List of the Best Ones

REST_vs_RESTful_The_Different_and_Why_the_Difference_Doesn_t_Matter

REST vs. RESTful: The Difference and Why the Difference Doesn’t Matter

What’s the difference between a REST API and a RESTful one? Is there a difference? This sounds like the kind of academic question that belongs on Reddit. But then you find yourself in a design session, and the person across the table is raising their voice.

The short answer is that REST stands for Representational State Transfer. It’s an architectural pattern for creating web services. A RESTful service is one that implements that pattern.

The long answer starts with “sort of” and “it depends” and continues with more complete definitions.

Continue reading REST vs. RESTful: The Difference and Why the Difference Doesn’t Matter

Self_Documenting_Code_vs._Comments_Turns_Out_It_s_Both_or_Neither

Self Documenting Code vs. Comments? Turns Out It’s Both or Neither

It’s been about a month since my last research post, and I’ve been musing about the next topic.  What should it be?  Well, I’ve decided.  Since I love nothing more than throwing the gates wide for everyone’s internet anger, I thought I’d weigh in on the subject of self documenting code vs comments.

I’ll be awaiting your rage below, in the comments.

Continue reading Self Documenting Code vs. Comments? Turns Out It’s Both or Neither

Visual_Studio_Enterprise_vs._Professional_Essential_Differences

Visual Studio Enterprise vs. Professional: Essential Differences

If you’re a .NET developer, then it’s overwhelmingly likely that you’re a Visual Studio user. There are alternatives to it, sure. But the product from the Redmond giant is the go-to when it comes to developing for the .NET framework.

For a newcomer, though, things can get confusing since Visual Studio isn’t a single thing. Instead, it comes in several shapes and sizes. Which one should you pick? What are the features that matter for your use case? Since Visual Studio isn’t free—most editions aren’t, at least—you want to get the best bang for your buck.

That’s what this post is going to cover. As its title suggests, we’ll focus on the differences between the enterprise and professional editions of the integrated development environment (IDE). By the end of the post, you’ll have learned enough to make an informed decision on which version of the IDE better suits your needs. Let’s get started.

Continue reading Visual Studio Enterprise vs. Professional: Essential Differences

domain_driven_design_demystified

Domain-Driven Design Demystified

Domain-driven design, or DDD, is a software design methodology aimed at producing better software. Engineers achieve this by working closely with domain experts during the continuous design process.

Eric Evans created domain-driven design and wrote a book about the practice called Domain-Driven Design: Tackling Complexity in the Heart of Software. His main points are to use models, use the language of the business, and follow specific technical patterns during development. Together, these steps will help you deal with complex software without painting yourself into a corner.

In this post, I’ll go further into demystifying domain-driven design. First, I’ll share a story about how I came to know the practice myself. Then I’ll get into some details about the modeling part of the process. Finally, I’ll do an overview of the technical bits of the craft.

Continue reading Domain-Driven Design Demystified

Should Architects Write Code

Should Architects Write Code? You Bet They Should!

There’s a common misconception that’s permeated our profession: Architects don’t need to write code to do their jobs.

Now, this may seem like a harmless approach. After all, writing code is what developers do. And architects should be busy with more important tasks.

However, keeping architects from writing code can limit the potential of your development teams. It can also result in an architectural mess when requirements and business needs change.

So today let’s look at why giving your software architect time to write code is a good thing. But first, we’ll start off by looking at what life is like as an architect.

Continue reading Should Architects Write Code? You Bet They Should!