Skip to content

[code-simplifier] refactor: simplify nested ternary and duplicate assignments in recent changes#9477

Draft
Evangelink wants to merge 1 commit into
mainfrom
code-simplifier/simplify-recent-changes-20260627-f5b59a3389bd8ff9
Draft

[code-simplifier] refactor: simplify nested ternary and duplicate assignments in recent changes#9477
Evangelink wants to merge 1 commit into
mainfrom
code-simplifier/simplify-recent-changes-20260627-f5b59a3389bd8ff9

Conversation

@Evangelink

Copy link
Copy Markdown
Member

Code Simplification — 2026-06-27

This PR simplifies recently modified code to improve clarity, consistency, and maintainability while preserving all functionality.

Files Simplified

  • src/Adapter/MSTestAdapter.PlatformServices/MSTestSettings.Configuration.cs — Replace nested ternary chain with an if/else chain
  • src/Platform/Microsoft.Testing.Extensions.VideoRecorder/VideoRecorderSessionHandler.cs — Eliminate duplicate field assignments in constructor

Improvements Made

1. Removed Nested Ternary (MSTestSettings.Configuration.cs)

The parallelism:workers parsing in SetSettingsFromConfig used a nested ternary chain:

// Before — nested ternary (violates project guidelines)
settings.ParallelizationWorkers = int.TryParse(workers, out int parallelWorkers)
    ? parallelWorkers == 0
        ? Environment.ProcessorCount
        : parallelWorkers > 0
            ? parallelWorkers
            : throw new AdapterSettingsException(...)
    : throw new AdapterSettingsException(...);
// After — if/else chain
if (!int.TryParse(workers, out int parallelWorkers) || parallelWorkers < 0)
{
    throw new AdapterSettingsException(...);
}
settings.ParallelizationWorkers = parallelWorkers == 0 ? Environment.ProcessorCount : parallelWorkers;

The project guidelines explicitly state: "Avoid nested ternary operators — prefer switch statements or if/else chains." This file was modified by PR #9453 (merged 2026-06-26).

2. Eliminated Duplicate Assignments (VideoRecorderSessionHandler.cs)

The constructor (new file from PR #9377, merged 2026-06-26) set _persistMode and _granularity in both the enabled and disabled branches:

// Before — duplicate assignments in two branches
if (!_enabled)
{
    _persistMode = options.PersistMode;   // ← duplicate
    _granularity = options.Granularity;   // ← duplicate
    return;
}
ApplyCommandLineOverrides(options, commandLineOptions);
_persistMode = options.PersistMode;       // ← duplicate
_granularity = options.Granularity;       // ← duplicate
// After — single assignment after the conditional
if (_enabled)
{
    ApplyCommandLineOverrides(options, commandLineOptions);
}
_persistMode = options.PersistMode;
_granularity = options.Granularity;
if (!_enabled)
{
    return;
}

Changes Based On

Recent changes from:

Testing

  • MSTestAdapter.PlatformServices builds cleanly (0 warnings, 0 errors) for net8.0 and net9.0
  • Microsoft.Testing.Extensions.VideoRecorder builds cleanly (0 warnings, 0 errors) for net8.0, net9.0, and netstandard2.0
  • ✅ No functional changes — behavior is identical in both cases

Review Focus

Please verify:

  • The if/else refactor preserves the original parallelism:workers validation (parse failure or negative → throw, 0ProcessorCount, positive → value)
  • The constructor reorder correctly defers _recorder creation to when _enabled is true

🤖 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. · 252.5 AIC · ⌖ 23.5 AIC · ⊞ 9.3K · [◷]( · )

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 28, 2026, 1:55 PM UTC

… changes

- MSTestSettings.Configuration.cs: replace nested ternary chain for
  parallelism:workers parsing with an if/else chain; project guidelines
  explicitly forbid nested ternaries (prefer if/else or switch).
  Behavior is identical: parse failure or negative value throws, 0 maps
  to ProcessorCount, positive value is used as-is.

- VideoRecorderSessionHandler.cs: eliminate duplicate _persistMode /
  _granularity assignments in the constructor. Both branches set the same
  fields from options; extracted them to a shared assignment after the
  conditional ApplyCommandLineOverrides call.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings June 27, 2026 13:55
@Evangelink Evangelink added type/automation Created or maintained by an agentic workflow. type/tech-debt Code health, refactoring, simplification. labels Jun 27, 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 refactors in two recently changed areas to improve readability and reduce duplication while preserving existing behavior in MSTest adapter configuration parsing and the VideoRecorder MTP extension.

Changes:

  • Replaced a nested ternary chain with an explicit parse/validate if guard for mstest:parallelism:workers.
  • Removed duplicate _persistMode / _granularity assignments in VideoRecorderSessionHandler by centralizing them after the conditional override logic.
Show a summary per file
File Description
src/Adapter/MSTestAdapter.PlatformServices/MSTestSettings.Configuration.cs Simplifies parallelism:workers parsing while preserving: parse failure/negative ⇒ throw; 0 ⇒ Environment.ProcessorCount; positive ⇒ value.
src/Platform/Microsoft.Testing.Extensions.VideoRecorder/VideoRecorderSessionHandler.cs Avoids duplicated field assignments by applying CLI overrides only when enabled, then assigning _persistMode/_granularity once before the disabled early-return.

Review details

  • Files reviewed: 2/2 changed files
  • Comments generated: 0
  • Review effort level: Low

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