Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the IDictionary.TryGetValue(TKey, out TValue) method to fix CA1854 warnings #12152

Merged
merged 5 commits into from
Dec 31, 2023

Conversation

yahiheb
Copy link
Collaborator

@yahiheb yahiheb commented Dec 27, 2023

https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1854

When an element of an IDictionary is accessed, the indexer implementation checks for a null value by calling the IDictionary.ContainsKey method. If you also call IDictionary.ContainsKey in an if clause to guard a value lookup, two lookups are performed when only one is needed.

Copy link
Collaborator

@turbolay turbolay left a comment

Choose a reason for hiding this comment

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

My comments are not requirements, the PR is good.

@@ -163,12 +163,13 @@ private List<SmartCoin> GenerateSmartCoins(IEnumerable<(string Cluster, decimal
{
var key = KeyManager.GenerateNewKey(new LabelsArray(targetCoin.Cluster), KeyState.Clean, false);

if (!generatedKeyGroup.ContainsKey(targetCoin.Cluster))
if (!generatedKeyGroup.TryGetValue(targetCoin.Cluster, out List<(HdPubKey key, decimal amount)>? value))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Rider always recommends here to use var. I know @kiminuo prefers very much to be specific all the time. Personal choice I guess, it's a discussion we're having all the time. Feel free to leave it as you did, simply a comment.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Using var makes reviews less friendly for people not using an IDE. Not everyone has an IDE. Not everytime one wants to switch to a different branch just because a complex line of code is vared. var can silently change code semantics during refactorings, etc. etc.

Anyway, #7153 was rejected, so it's up to everyone who likes what.

@kiminuo kiminuo changed the title Use the IDictionary.TryGetValue(TKey, out TValue) method to fix CA1854 warnings Use the IDictionary.TryGetValue(TKey, out TValue) method to fix CA1854 warnings Dec 28, 2023
yahiheb and others added 2 commits December 31, 2023 18:36
Co-authored-by: Kimi <58662979+kiminuo@users.noreply.github.com>
@kiminuo kiminuo merged commit a939a36 into WalletWasabi:master Dec 31, 2023
7 checks passed
@yahiheb yahiheb deleted the fixwarnings-CA1854 branch December 31, 2023 21:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants