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
How To Use AI Assistant to Generate Test Data For .NET Applications
How To Use AI Assistant to Generate Test Data For .NET Applications for better unit tests.
Rider 2024.1 EAP 5: Collections Visualizer, Sticky Lines, Improved GitHub Actions Support, and More
We are mid-way through the Early Access Program for Rider 2024, which means the EAP 5 build has just been released, bringing a host of amazing updates. But before you go to download it, let’s go over the new features this preview contains. Collections visualizer Rider 2024.1 introduces t…