Skip to content

[duplicate-code] Duplicate Code: Polyfill File Structure Boilerplate #8591

@Evangelink

Description

@Evangelink

🔍 Duplicate Code Detected: Polyfill File Structure Boilerplate

Analysis of commit ed7ecb2

Summary

The newly added polyfill files in src/Polyfills/ contain repetitive structural boilerplate with identical file headers, conditional compilation directives, using statements, and #else clauses. This pattern is duplicated across 23 files with over 400 lines of repeated boilerplate code.

Duplication Details

Pattern: Polyfill File Structure

  • Severity: High

  • Occurrences: 23 files

  • Total Duplicate Lines: ~400+ lines

  • Locations:

    • src/Polyfills/CallerArgumentExpressionAttribute.cs (lines 1-7, 33-35)
    • src/Polyfills/CompilerFeatureRequiredAttribute.cs (lines 1-7, 48-50)
    • src/Polyfills/ExperimentalAttribute.cs (lines 1-7, 53-55)
    • src/Polyfills/IsExternalInit.cs (lines 1-7, 18-20)
    • src/Polyfills/ModuleInitializerAttribute.cs (lines 1-10, 22-24)
    • src/Polyfills/RequiredMemberAttribute.cs (lines 1-12, 25-27)
    • src/Polyfills/InterpolatedStringHandlerAttribute.cs (lines 1-6, 18-20)
    • src/Polyfills/InterpolatedStringHandlerArgumentAttribute.cs (lines 1-6, 30-32)
    • src/Polyfills/StackTraceHiddenAttribute.cs (lines 1-6, 14-16)
    • src/Polyfills/UnconditionalSuppressMessageAttribute.cs (lines 1-8, 92-94)
    • src/Polyfills/DynamicallyAccessedMembersAttribute.cs (lines 1-9, 60-62)
    • src/Polyfills/PlatformAttributes.cs (lines 1-8, 194-202)
    • Plus 11 more files with similar patterns
  • Code Sample:

// Repeated in 23 files:
// <auto-generated />

#pragma warning disable

#if !NETCOREAPP

using Microsoft.CodeAnalysis;

namespace System.Runtime.CompilerServices; // or other namespaces

// ... actual polyfill implementation ...

#else
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(...))]
#endif

Impact Analysis

  • Maintainability: Any change to the file structure pattern (e.g., updating the conditional compilation directive, changing the auto-generated marker, or adjusting the pragma directives) requires modifying 23 separate files.
  • Bug Risk: Inconsistent application of the pattern across files can lead to build errors or unexpected behavior in different target frameworks.
  • Code Bloat: Approximately 400+ lines of repeated boilerplate code that could be eliminated or reduced through abstraction.
  • Consistency: Currently, there are minor variations in the pattern (e.g., some files include #nullable enable, some have different using statement orders), indicating the pattern is not centrally managed.

Refactoring Recommendations

1. T4 Template or Source Generator Approach (Recommended)

  • Create a T4 template or Roslyn source generator that generates the polyfill files with consistent structure
  • Define polyfill metadata in a central configuration file (e.g., JSON or XML)
  • Generate all polyfill files during build from the template
  • Estimated effort: 8-12 hours
  • Benefits:
    • Single source of truth for file structure
    • Guaranteed consistency across all polyfills
    • Easy to update all files by changing the template
    • Reduces manual errors

2. Shared .props File for Common Directives

  • Extract common MSBuild properties and conditional compilation symbols to a shared .props file
  • Use MSBuild targets to inject common file headers
  • Estimated effort: 4-6 hours
  • Benefits:
    • Centralizes compilation settings
    • Reduces some boilerplate
  • Limitations:
    • Cannot eliminate in-file boilerplate completely

3. Base Class/Helper for Common Patterns

  • For polyfills that share common structure, consider using a base class or helper methods
  • Estimated effort: 6-8 hours
  • Benefits:
    • Reduces code duplication
  • Limitations:
    • May not be applicable to all polyfill types (attributes, static classes, etc.)
    • Could complicate type-forwarding logic

Implementation Checklist

  • Review duplication findings with team
  • Decide on refactoring approach (T4 template vs. source generator vs. hybrid)
  • Create template or generator infrastructure
  • Migrate existing polyfill files to use the new approach
  • Update build scripts to generate polyfills during build
  • Verify all target frameworks build correctly
  • Update documentation for adding new polyfills
  • Consider adding automated checks to prevent manual polyfill additions

Analysis Metadata

  • Analyzed Files: 48 C# source files
  • Detection Method: Semantic code analysis with pattern matching
  • Commit: ed7ecb2
  • Analysis Date: 2026-05-26T05:35:57Z
  • Repository: microsoft/testfx

Generated by Duplicate Code Detector · ● 1.5M ·

Add this agentic workflows to your repo

To install this agentic workflow, run

gh aw add githubnext/agentics/workflows/duplicate-code-detector.md@main
  • expires on May 28, 2026, 5:43 AM UTC

Metadata

Metadata

Labels

type/automationCreated or maintained by an agentic workflow.type/tech-debtCode health, refactoring, simplification.

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions