ReSharper 2023.2 EAP 2: New C# Inspections for Local Functions, the “N+1” Problem, and More
The second EAP build for ReSharper 2023.2 has just been released!
You can download it from our website. Heads up: This update is heavy on C# language support. Let’s go over the most important updates.
Inspections for working with local functions
ReSharper 2023.2 EAP 2 introduces two new inspections and corresponding quick-fixes aimed at improving code readability with local functions.
The use of local functions can present certain challenges with respect to code readability. A common issue is that developers reading the code are forced to scroll through all helper functions just to ensure that a method doesn’t contain any more executable code hidden between or after the functions. To address this problem, ReSharper will now recommend putting an explicit
continue before local functions at the end of a method.
Another challenge with using local functions is that it can be difficult to predict the code’s behavior when executable code and helper functions are intermingled. To mitigate this issue, ReSharper will suggest moving local functions to the end of a method or block and separating them from executable code with an explicit
continue, or another control flow jump statement. By maintaining a clear separation between executable code and helper functions, developers can better understand the flow of the code they are reading.
We often use ReSharper’s own codebase to test how our inspections improve code quality. This time, we stumbled upon a perfect example. Check out how a real production code method immediately becomes more concise and easier to understand once these inspections are applied:
With EAP 2, we’re also making a number of small improvements in other related features to keep them consistent with the new inspections. Most notably, when Extract method has to move local functions along with the code on which it is used, code refactoring will place the moved local functions at the end of the extracted method. Extract local function will also change its default position so that the new function is at the end of the containing member.
Inspections for common Entity Framework issues
The “N+1” problem
If you’ve ever worked with databases using an object-relational mapping (ORM) framework like Entity Framework, you might have heard about or even faced the “N+1” problem. This refers to the situation where unnecessary database calls are made when iterating a collection (N being the number of rows fetched by the query, plus the original query). Since making multiple calls for the same data to the database is much slower than one, fixing the “N+1” problem can help you speed up your application.
In the EAP 2 build, we’ve finally delivered new inspections which detect this problem and notify you about it. If ReSharper finds a place in your code with a possible “N+1” problem, you will see the Possible multiple queries to the database for related entities (N+1 problem) and Possible multiple queries to the database (N+1 problem) highlighting. The description of the first inspection will show you which entities are forgotten by the initial query and will lead to additional calls.
To help you investigate the issue further, ReSharper gives you a way to navigate back and forth between the initial query and the call for related entities, producing possible performance overhead.
In addition, ReSharper provides you with a quick-fix to add Include and
ThenInclude method calls (also known as Eager loading) to the initial query with all required relations. Eager loading allows you to retrieve the related entities in a single query, reducing the number of database round trips and fixing the problem.
Query returns incomplete data
In addition to detecting the “N+1” problem, we’ve introduced one more useful inspection when you work with Entity Framework: Query can return incomplete data for related entities.
This inspection reports situations where a query is performed over a set of entities, and related entities are accessed within the query without being eagerly loaded or explicitly included in the query. This leads to not getting the full data we expect from this query.
To avoid this problem, you can either eagerly load related entities using
ThenInclude method calls or enable lazy loading so related entities are loaded on-demand when accessed. ReSharper helps you deal with this problem by offering you a quick-fix to add
ThenInclude method calls to the initial query. If you want to investigate the issue before applying a fix, ReSharper offers the same back and forth navigation as for the “N+1” problem.
For the full list of features and improvements included in this build, please see our issue tracker.
That’s it for now!
As always, we’d love to hear your opinions and suggestions in the comments below.
Subscribe to Blog updates
Thanks, we've got you!
A Second Set of Bug Fixes: ReSharper 2023.2.2 and Rider 2023.2.2 Are Here!
ReSharper and Rider have just received their second set of bug-fix updates for the 2023.2 release! Let’s take a look at the most important issues that have been resolved in this update. ReSharper For the full list of resolved issues, please refer to our issue tracker. …
ReSharper 2023.3 Early Access Program Begins!
Hello everyone, The Early Access Program for ReSharper 2023.3 has started! Before you download the first EAP build, let’s take a look at what is in store for you. Working with aliases With this first EAP build, we are introducing support for C# 12’s ability for alias directives to refer…
Rider Kicks Off the Early Access Program for the 2023.3 Release!
The Early Access Program for Rider 2023.3 has just begun with the release of the Rider 2023.3 EAP 1 build. There are several ways to get your hands on it: Download and install them from our website. Use the Toolbox App. Install this snap package from the SnapCraft store if you’re using a c…
Eager, Lazy and Explicit Loading with Entity Framework Core
Entity Framework Core (EF Core) supports a number of ways to load related data. There’s eager loading, lazy loading, and explicit loading. Each of these approaches have their own advantages and drawbacks. In this post, let’s have a quick look at each of these ways to load data for navigational prope…