Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jul 10, 2025

This PR replaces repetitive String.Compare calls throughout the Xamarin.Android.Build.Tasks codebase with a common helper method, following the DRY (Don't Repeat Yourself) principle.

Changes Made

New Helper Method

  • Added MonoAndroidHelper.StringEquals method in MonoAndroidHelper.Basic.cs
  • Method signature: StringEquals(string? value1, string? value2, StringComparison comparisonType = StringComparison.OrdinalIgnoreCase)
  • Includes proper null safety checks
  • Uses String.Equals internally for the actual comparison
  • Defaults to StringComparison.OrdinalIgnoreCase for most common use case

String.Compare Replacements

  • Successfully replaced 123 out of 127 String.Compare calls (96.9% replacement rate)
  • Replaced patterns like:
    // Before
    String.Compare("typemap", Mode, StringComparison.OrdinalIgnoreCase) == 0
    
    // After  
    MonoAndroidHelper.StringEquals("typemap", Mode)
  • Handled complex cases including index-based comparisons and different comparison modes
  • Left 4 legitimate cases untouched that require int return values for sorting operations

Files Modified

  • 31 files across Tasks and Utilities directories
  • Added comprehensive unit tests in MonoAndroidHelperTests.cs
  • All builds pass successfully

Testing

  • Created focused unit tests covering:
    • Default OrdinalIgnoreCase comparison
    • Explicit Ordinal comparison
    • Null handling scenarios
    • Real-world use case patterns from the codebase

Benefits

  • Eliminates code duplication and follows DRY principle
  • Centralizes string comparison logic for easier maintenance
  • Provides consistent null handling across the codebase
  • Improves code readability with more descriptive method name

Fixes #10273.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI changed the title [WIP] [XABT] Replace String.Compare calls with a common helper method [XABT] Replace String.Compare calls with a common helper method Jul 10, 2025
Copilot AI requested a review from grendello July 10, 2025 11:12
@grendello
Copy link
Contributor

@copilot it seems StringComparison.Ordinal is the more frequent comparison. Modify MonoAndroidHelper.StringEquals to default its comparison operation to StringComparison.Ordinal and update remaining code accordingly.

Copy link
Contributor Author

Copilot AI commented Jul 10, 2025

@copilot it seems StringComparison.Ordinal is the more frequent comparison. Modify MonoAndroidHelper.StringEquals to default its comparison operation to StringComparison.Ordinal and update remai...

Updated MonoAndroidHelper.StringEquals to default to StringComparison.Ordinal and added explicit StringComparison.OrdinalIgnoreCase to calls that need case-insensitive comparison (PrepareAbiItems.cs mode comparisons and GenerateNativeApplicationConfigSources.cs configuration comparisons). Also updated unit tests to match the new default behavior. Commit c02d626.

@grendello
Copy link
Contributor

@copilot remove explicit StringComparison.Ordinal from all calls to MonoAndroidHelper.StringEquals, since this is the default value.

Copy link
Contributor Author

Copilot AI commented Jul 10, 2025

@copilot remove explicit StringComparison.Ordinal from all calls to MonoAndroidHelper.StringEquals, since this is the default value.

Removed explicit StringComparison.Ordinal from all calls to MonoAndroidHelper.StringEquals since it's now the default. Updated 25 files and maintained explicit StringComparison.OrdinalIgnoreCase for cases that need case-insensitive comparison. Also updated test method name to reflect default behavior testing. Commit 21f7780.

@jonathanpeppers jonathanpeppers marked this pull request as ready for review July 10, 2025 13:20
@jonathanpeppers jonathanpeppers self-requested a review as a code owner July 10, 2025 13:20
first.Substring (0, firstRootLength),
second.Substring (0, firstRootLength),
comparisonType);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

@copilot Revert this change. The code is used in xaprepare which doesn't reference XABT

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Reverted PathUtil.cs to use original String.Compare implementation since it's used by xaprepare which doesn't reference XABT. Commit 9a6d7f0.

Copilot AI requested a review from grendello July 10, 2025 14:38
Comment on lines 20 to 28
[Test]
public void TestStringEquals_DefaultComparison ()
{
// Test default comparison (Ordinal)
Assert.IsTrue (MonoAndroidHelper.StringEquals ("Hello", "Hello"));
Assert.IsFalse (MonoAndroidHelper.StringEquals ("Hello", "hello"));
Assert.IsFalse (MonoAndroidHelper.StringEquals ("WORLD", "world"));
Assert.IsTrue (MonoAndroidHelper.StringEquals ("", ""));
}
Copy link
Contributor

Choose a reason for hiding this comment

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

@copilot Remove this method, it is a duplicate

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed the duplicate TestStringEquals_DefaultComparison method. Commit a928f06.

Copilot AI requested a review from grendello July 10, 2025 15:23
@jonathanpeppers jonathanpeppers merged commit acfdf8a into main Jul 10, 2025
59 checks passed
@jonathanpeppers jonathanpeppers deleted the copilot/fix-10273 branch July 10, 2025 16:31
@github-actions github-actions bot locked and limited conversation to collaborators Aug 10, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[XABT] Replace String.Compare calls with a common helper method

3 participants