Skip to content

Update AV1130: Return interfaces to unchangeable collections#354

Open
dennisdoomen wants to merge 1 commit intodevelopfrom
copilot/pr298-update-av1130
Open

Update AV1130: Return interfaces to unchangeable collections#354
dennisdoomen wants to merge 1 commit intodevelopfrom
copilot/pr298-update-av1130

Conversation

@dennisdoomen
Copy link
Copy Markdown
Owner

This PR updates guideline AV1130.

It was split out of #298 so the change can be reviewed independently.

Files:

  • _rules/1130.md

Part of the replacement for #298.

Split from #298.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
severity: 2
---
You generally don't want callers to be able to change an internal collection, so don't return arrays, lists or other collection classes directly. Instead, return an `IEnumerable<T>`, `IAsyncEnumerable<T>`, `IQueryable<T>`, `IReadOnlyCollection<T>`, `IReadOnlyList<T>`, `IReadOnlySet<T>` or `IReadOnlyDictionary<TKey, TValue>`.
You generally don't want callers to be able to change an internal collection, so don't return arrays, lists or other collection classes directly. Instead, return an `IEnumerable<T>`, `IAsyncEnumerable<T>`, `IReadOnlyCollection<T>`, `IReadOnlyList<T>`, `IReadOnlySet<T>` or `IReadOnlyDictionary<TKey, TValue>`.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
You generally don't want callers to be able to change an internal collection, so don't return arrays, lists or other collection classes directly. Instead, return an `IEnumerable<T>`, `IAsyncEnumerable<T>`, `IReadOnlyCollection<T>`, `IReadOnlyList<T>`, `IReadOnlySet<T>` or `IReadOnlyDictionary<TKey, TValue>`.
You generally don't want callers to be able to change an internal collection, so don't return arrays, lists or other mutable collection classes directly. Instead, return an `IEnumerable<T>`, `IAsyncEnumerable<T>`, `IReadOnlyCollection<T>`, `IReadOnlyList<T>`, `IReadOnlySet<T>` or `IReadOnlyDictionary<TKey, TValue>`.

You generally don't want callers to be able to change an internal collection, so don't return arrays, lists or other collection classes directly. Instead, return an `IEnumerable<T>`, `IAsyncEnumerable<T>`, `IQueryable<T>`, `IReadOnlyCollection<T>`, `IReadOnlyList<T>`, `IReadOnlySet<T>` or `IReadOnlyDictionary<TKey, TValue>`.
You generally don't want callers to be able to change an internal collection, so don't return arrays, lists or other collection classes directly. Instead, return an `IEnumerable<T>`, `IAsyncEnumerable<T>`, `IReadOnlyCollection<T>`, `IReadOnlyList<T>`, `IReadOnlySet<T>` or `IReadOnlyDictionary<TKey, TValue>`.

Be aware that `IEnumerable<T>` is often perceived as lazy-evaluated. If your collection is already materialized, consider returning `IReadOnlyCollection<T>` or even a plain array to make the intent clear.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Be aware that `IEnumerable<T>` is often perceived as lazy-evaluated. If your collection is already materialized, consider returning `IReadOnlyCollection<T>` or even a plain array to make the intent clear.
Be aware that `IEnumerable<T>` is often perceived as lazy-evaluated. If your collection is already materialized, consider returning `IReadOnlyCollection<T>` or `IReadOnlyList<T>` to make the intent clear.

Returning an array goes against the rule itself: it is mutable (read the first line).


Be aware that `IEnumerable<T>` is often perceived as lazy-evaluated. If your collection is already materialized, consider returning `IReadOnlyCollection<T>` or even a plain array to make the intent clear.

**Exception:** Immutable collections such as `ImmutableArray<T>`, `ImmutableList<T>` and `ImmutableDictionary<TKey, TValue>` prevent modifications from the outside and are thus allowed.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
**Exception:** Immutable collections such as `ImmutableArray<T>`, `ImmutableList<T>` and `ImmutableDictionary<TKey, TValue>` prevent modifications from the outside and are thus allowed.
**Exception:** Immutable collections such as `ImmutableArray<T>`, `ImmutableList<T>` and `ImmutableDictionary<TKey, TValue>`, as well as `FrozenSet<T>` and `FrozenDictionary<TKey, TValue>` prevent modifications from the outside and are thus allowed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants