Skip to content

[code-simplifier] simplify: use indexer-style dict initializer and inline lambdas in SerializerUtilities#9009

Open
Evangelink wants to merge 1 commit into
mainfrom
simplify/serializer-utilities-dict-initializers-839e4b2bc5c18116
Open

[code-simplifier] simplify: use indexer-style dict initializer and inline lambdas in SerializerUtilities#9009
Evangelink wants to merge 1 commit into
mainfrom
simplify/serializer-utilities-dict-initializers-839e4b2bc5c18116

Conversation

@Evangelink

Copy link
Copy Markdown
Member

Code Simplification — 2026-06-10

This PR simplifies recently modified code from #8974 (SerializerUtilities refactoring) to improve clarity, consistency, and alignment with project coding standards.

Files Simplified

  • src/Platform/Microsoft.Testing.Platform/ServerMode/JsonRpc/SerializerUtilities.cs — fix dictionary initializer coding standard violation, eliminate local variable
  • src/Platform/Microsoft.Testing.Platform/ServerMode/JsonRpc/SerializerUtilities.TestNodeSerializers.cs — standardize two lambdas to match the expression-body pattern used by other serializers in the same file

Improvements Made

1. Fix Add-style dictionary initializer (coding standard violation)

SerializerUtilities.cs contained the only Add-style { { key, value } } dictionary initializer across the four serializer files. The project coding standards explicitly require the indexer syntax [key] = value for dictionary initializers. Replaced and removed the unnecessary local variable:

// Before
Serializers[typeof(KeyValuePair<string, string>)] = new ObjectSerializer<KeyValuePair<string, string>>(o =>
{
    Dictionary<string, object?> values = new()
    {
        { o.Key, o.Value },
    };
    return values;
});

// After
Serializers[typeof(KeyValuePair<string, string>)] = new ObjectSerializer<KeyValuePair<string, string>>(
    o => new Dictionary<string, object?> { [o.Key] = o.Value });

2. Standardize verbose lambdas in SerializerUtilities.TestNodeSerializers.cs

TestNodeUpdateMessage and TestNodeStateChangedEventArgs serializers used a verbose block-body pattern with a local values variable and an explicit return, while Artifact and RunResponseArgs in the same file already used the cleaner expression-body pattern. Aligned them:

// Before
Serializers[typeof(TestNodeUpdateMessage)] = new ObjectSerializer<TestNodeUpdateMessage>(ev =>
{
    Dictionary<string, object?> values = new()
    {
        [JsonRpcStrings.Node] = Serialize(ev.TestNode),
        [JsonRpcStrings.Parent] = ev.ParentTestNodeUid?.Value,
    };
    return values;
});

// After
Serializers[typeof(TestNodeUpdateMessage)] = new ObjectSerializer<TestNodeUpdateMessage>(ev => new Dictionary<string, object?>
{
    [JsonRpcStrings.Node] = Serialize(ev.TestNode),
    [JsonRpcStrings.Parent] = ev.ParentTestNodeUid?.Value,
});

Changes Based On

Recent changes from:

Testing

  • ✅ No functional changes — behavior is identical
  • ✅ The KeyValuePair<string, string> serializer still produces { key: value } (indexer setter has same semantics as Add for non-duplicate keys)
  • ✅ Both lambdas produce the same Dictionary<string, object?> as before

Review Focus

Please verify:

  • Functionality is preserved (indexer vs Add produces same result for non-duplicate keys)
  • Simplifications improve consistency within the files
  • Changes align with project conventions (indexer-style dict initializers, expression-body lambdas)

🤖 Automated content by GitHub Copilot. Posted via a maintainer's GitHub token, so it appears under their account — the account owner did not write or approve this content personally. Generated by the Code Simplifier workflow.{ai_credits_suffix} · [◷]( · )

Add this agentic workflows to your repo

To install this agentic workflow, run

gh aw add githubnext/agentics/workflows/code-simplifier.md@main
  • expires on Jun 11, 2026, 2:28 PM UTC

…rializerUtilities

- SerializerUtilities.cs: replace Add-style { { key, value } } dictionary initializer
  with indexer-style [key] = value (per project coding standards), and eliminate
  unnecessary local variable by using an expression-body lambda.
- SerializerUtilities.TestNodeSerializers.cs: convert two verbose block-body lambdas
  (TestNodeUpdateMessage, TestNodeStateChangedEventArgs) to the same expression-body
  pattern already used for Artifact and RunResponseArgs in the same file.

Both files were refactored as part of #8974 (splitting the monolithic SerializerUtilities.cs).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings June 10, 2026 14:28
@Evangelink Evangelink added type/automation Created or maintained by an agentic workflow. type/tech-debt Code health, refactoring, simplification. labels Jun 10, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR performs small, non-functional simplifications in the JSON-RPC serialization utilities within Microsoft.Testing.Platform, aligning serializer lambdas and dictionary initializers with established repo coding standards and patterns used elsewhere in the same serializer set.

Changes:

  • Replaced an Add-style dictionary initializer with an indexer-style initializer for the KeyValuePair<string, string> serializer and inlined the dictionary creation.
  • Simplified the TestNodeUpdateMessage and TestNodeStateChangedEventArgs serializers to use expression-bodied lambdas consistent with other serializers in the same file.
Show a summary per file
File Description
src/Platform/Microsoft.Testing.Platform/ServerMode/JsonRpc/SerializerUtilities.cs Uses indexer-style dictionary initializer for the KeyValuePair<string,string> serializer and removes the temporary local variable.
src/Platform/Microsoft.Testing.Platform/ServerMode/JsonRpc/SerializerUtilities.TestNodeSerializers.cs Inlines dictionary creation in two serializer lambdas to match the expression-bodied pattern used by nearby serializers.

Copilot's findings

  • Files reviewed: 2/2 changed files
  • Comments generated: 0

@Evangelink Evangelink marked this pull request as ready for review June 10, 2026 14:57

@Evangelink Evangelink left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Note

🤖 Automated review by GitHub Copilot. Posted via a maintainer's GitHub token, so it appears under their account — the account owner did not write or approve this content personally. Generated by the Expert Code Review workflow. To request a follow-up action, reply by tagging @copilot directly.

✅ 22/22 dimensions clean — no findings.

Scope: src/Platform/Microsoft.Testing.Platform/ServerMode/JsonRpc/ (serialization hot-path — hotspot dimensions Threading, Performance, IPC Wire Compatibility applied with extra scrutiny.)

Notes per applicable dimension:

  • Algorithmic Correctness — Behavior is preserved. The { { o.Key, o.Value } } Add-style initializer and { [o.Key] = o.Value } indexer initializer both produce an identical single-entry dictionary for a KeyValuePair<string, string> (the only consumer being test-metadata trait serialization). No edge case difference surfaces.
  • IPC Wire Compatibility — The serialized key/value pairs are unchanged in all three lambdas. No wire format regression.
  • Performance — No allocation delta; local variable elimination is neutral to the JIT.
  • Cross-TFM — Indexer-style collection initializers and expression-body lambdas compile identically across net462, netstandard2.0, net8.0, and net9.0.
  • Code Structure & Simplification — Changes align the two converted lambdas with the expression-body pattern already used by every other serializer in the same file (Artifact, DiscoverResponseArgs, RunResponseArgs). Consistent style throughout the file is a net positive.
  • Coding Standards Compliance — Both transformations follow the explicit project guideline: "prefer the indexer syntax [key] = value over the Add-style { { key, value } } initializer"; and "ensure the final return statement of a method is on its own line" is satisfied by eliminating the intermediary local variable entirely.
  • Test Completeness — Purely mechanical style change; no behavioral path altered, so no new tests are required.
  • Dimensions 2 (Threading), 3 (Security), 4 (Public API), 7 (IDisposable), 8 (Defensive Coding), 9 (Localization), 10–14 (Test dimensions), 17 (Documentation), 18 (Analyzer Quality), 20 (Build Infrastructure), 22 (PowerShell) — all N/A for this diff.

🤖 Automated content by GitHub Copilot. Posted via a maintainer's GitHub token, so it appears under their account — the account owner did not write or approve this content personally. Generated by the Expert Code Review (on PR ready) workflow.{ai_credits_suffix} ·

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

Labels

type/automation Created or maintained by an agentic workflow. type/tech-debt Code health, refactoring, simplification.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants