-
Notifications
You must be signed in to change notification settings - Fork 273
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Clarified the use of var (AV1520) and supplemented AV1707 (names) (#252)
- Loading branch information
Showing
2 changed files
with
25 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,33 @@ | ||
--- | ||
rule_id: 1520 | ||
rule_category: maintainability | ||
title: Only use `var` when the type is very obvious | ||
title: Only use `var` when the type is evident | ||
severity: 1 | ||
--- | ||
Only use `var` as the result of a LINQ query, or if the type is very obvious from the same statement and using it would improve readability. So don't | ||
Use `var` for anonymous types (typically resulting from a LINQ query), or if the type is [evident](https://www.jetbrains.com/help/resharper/2021.3/Using_var_Keyword_in_Declarations.html#use-var-when-evident-details). | ||
Never use `var` for [built-in types](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/built-in-types). | ||
|
||
// what type? int? uint? float? | ||
var item = 3; | ||
// Projection into anonymous type. | ||
var largeOrders = | ||
from order in dbContext.Orders | ||
where order.Items.Count > 10 && order.TotalAmount > 1000 | ||
select new { order.Id, order.TotalAmount }; | ||
|
||
// Not obvious what base-class or interface to expect. | ||
// Also difficult to refactor if you can't search for the class. | ||
var myfoo = MyFactoryMethod.Create("arg"); | ||
// Built-in types. | ||
bool isValid = true; | ||
string phoneNumber = "(unavailable)"; | ||
uint pageSize = Math.Max(itemCount, MaxPageSize); | ||
|
||
Instead, use `var` like this: | ||
// Types are evident. | ||
var customer = new Customer(); | ||
var invoice = Invoice.Create(customer.Id); | ||
var user = sessionCache.Resolve<User>("[email protected]"); | ||
var subscribers = new List<Subscriber>(); | ||
var summary = shoppingBasket.ToOrderSummary(); | ||
|
||
var query = from order in orders where order.Items > 10 and order.TotalValue > 1000; | ||
var repository = new RepositoryFactory.Get(); | ||
var list = new ReadOnlyCollection(); | ||
|
||
In all of three above examples it is clear what type to expect. For a more detailed rationale about the advantages and disadvantages of using `var`, read Eric Lippert's [Uses and misuses of implicit typing](https://docs.microsoft.com/en-us/archive/blogs/ericlippert/uses-and-misuses-of-implicit-typing). | ||
// All other cases. | ||
IQueryable<Order> recentOrders = ApplyFilter(order => order.CreatedAt > DateTime.Now.AddDays(-30)); | ||
LoggerMessage message = Compose(context); | ||
ReadOnlySpan<string> key = ExtractKeyFromPair("[email protected]"); | ||
IDictionary<Category, Product> productsPerCategory = | ||
shoppingBasket.Products.ToDictionary(product => product.Category); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters