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

Prefer use of interpolated strings in PresentationUI and ReachFramework #8739

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

halgab
Copy link
Contributor

@halgab halgab commented Jan 28, 2024

Follow-up to #8519

Description

We replace as many manual string concatenations and string.Format with interpolated strings. This saves allocations in many instances, and makes for a more readable code.

Customer Impact

Less allocations

Regression

No

Testing

CI

Risk

Low. Most changes are automated using the IDE

Microsoft Reviewers: Open in CodeFlow

@ghost ghost assigned halgab Jan 28, 2024
@ghost ghost added the PR metadata: Label to tag PRs, to facilitate with triage label Jan 28, 2024
@ghost ghost requested review from dipeshmsft and singhashish-wpf January 28, 2024 20:57
@ghost ghost added the Community Contribution A label for all community Contributions label Jan 28, 2024
@halgab halgab force-pushed the interpolated-string-presui branch from d6cb976 to 999cfa6 Compare January 28, 2024 21:45
@halgab halgab marked this pull request as ready for review January 28, 2024 22:07
@halgab halgab requested a review from a team as a code owner January 28, 2024 22:07
@halgab halgab force-pushed the interpolated-string-presui branch from 999cfa6 to 22c1e96 Compare January 28, 2024 22:20
sb.AppendFormat(provider, "ContextColor {0} ", uriString);
sb.AppendFormat(provider, "{1:R}{0}", separator, color.ScA);
for (int i = 0; i < color.GetNativeColorValues().GetLength(0); ++i)
sb.AppendFormat(provider, $"ContextColor {uriString} ");
Copy link
Member

Choose a reason for hiding this comment

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

This method should be replaced with sb.Append right ?

"<LinkTarget Name=\"{0}\" />",
nameElement)
);
xmlWriter.WriteRaw($"<LinkTarget Name=\"{nameElement}\" />");
Copy link
Member

Choose a reason for hiding this comment

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

InvariantCulture missing here

uniqueUri = String.Format(System.Globalization.CultureInfo.InvariantCulture,
"{0}.fdseq",
new object[] { contentKey });
uniqueUri = $"{contentKey}.fdseq";
Copy link
Member

Choose a reason for hiding this comment

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

InvariantCulture missing here

" xmlns:" + PrintSchemaPrefixes.xsd + "=\"" + PrintSchemaNamespaces.xsd + "\"" +
" version=\"1\">" +
"</" + PrintSchemaPrefixes.Framework + ":" + PrintSchemaTags.Framework.PrintTicketRoot + ">";
internal const string EmptyPrintTicket = $"<?xml version=\"1.0\" encoding=\"UTF-8\"?><{PrintSchemaPrefixes.Framework}:{PrintSchemaTags.Framework.PrintTicketRoot} xmlns:{PrintSchemaPrefixes.Framework}=\"{PrintSchemaNamespaces.Framework}\" xmlns:{PrintSchemaPrefixes.StandardKeywordSet}=\"{PrintSchemaNamespaces.StandardKeywordSet}\" xmlns:{PrintSchemaPrefixes.xsi}=\"{PrintSchemaNamespaces.xsi}\" xmlns:{PrintSchemaPrefixes.xsd}=\"{PrintSchemaNamespaces.xsd}\" version=\"1\"></{PrintSchemaPrefixes.Framework}:{PrintSchemaTags.Framework.PrintTicketRoot}>";
Copy link
Member

Choose a reason for hiding this comment

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

It would be better to use string.Concat or the original way here. IMO, this string is too long for using interpolation.

string resourceName = "PUIRMStatus"
+ Enum.GetName(typeof(RightsManagementStatus), status)
+ "BrushKey";
string resourceName = $"PUIRMStatus{Enum.GetName(status)}BrushKey";
Copy link
Member

Choose a reason for hiding this comment

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

Is this right ? Enum.GetName ?

string resourceName = "PUISignatureStatus"
+ Enum.GetName(typeof(SignatureStatus), sigStatus)
+ "BrushKey";
string resourceName = $"PUISignatureStatus{Enum.GetName(sigStatus)}BrushKey";
Copy link
Member

Choose a reason for hiding this comment

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

Enum.GetName(typeof(SignatureStatus), sigStatus) ?

DisplayList.LeftPad(transparentCluster[i].DebugBounds, 0),
DisplayList.LeftPad(transparentCluster[i].DebugPrimitives, 0),
transparentCluster[i].DebugRasterize);
Console.WriteLine($"Cluster {i + 1}: {DisplayList.LeftPad(transparentCluster[i].DebugBounds, 0)} {DisplayList.LeftPad(transparentCluster[i].DebugPrimitives, 0)} {transparentCluster[i].DebugRasterize}");
Copy link
Member

Choose a reason for hiding this comment

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

This interpolation is too long, can we revert this back as original ?

@@ -176,8 +176,7 @@ public CanvasImageableArea ImageableArea
/// <returns>A string that represents this imageable size capability object.</returns>
public override string ToString()
{
return "ImageableSizeWidth=" + ImageableSizeWidth + ", ImageableSizeHeight=" + ImageableSizeHeight + " " +
((ImageableArea != null) ? ImageableArea.ToString() : "[ImageableArea: null]");
return $"ImageableSizeWidth={ImageableSizeWidth}, ImageableSizeHeight={ImageableSizeHeight} {((ImageableArea != null) ? ImageableArea.ToString() : "[ImageableArea: null]")}";
Copy link
Member

Choose a reason for hiding this comment

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

Can we split in two lines ? Too long interpolation string.

PTUtility.GetTextFromResource("FormatException.XMLNotWellFormed"),
errorMsg),
"printTicket");
throw new ArgumentException($"{PrintSchemaTags.Framework.PrintTicketRoot} {PTUtility.GetTextFromResource("FormatException.XMLNotWellFormed")} {errorMsg}",
Copy link
Member

Choose a reason for hiding this comment

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

I prefer the previous way here. The current change results in too long interpolation. Same suggestion for the change below and FallbackPTProvider.cs

_addressTemplate,
mailtoUri.GetComponents(UriComponents.UserInfo, UriFormat.Unescaped),
mailtoUri.GetComponents(UriComponents.Host, UriFormat.Unescaped));
address = $"{mailtoUri.GetComponents(UriComponents.UserInfo, UriFormat.Unescaped)}@{mailtoUri.GetComponents(UriComponents.Host, UriFormat.Unescaped)}";
Copy link
Member

Choose a reason for hiding this comment

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

Can we revert this back to the original way ? Same reasoning, too long string.

@dipeshmsft
Copy link
Member

@halgab, can you take a look at the above PR and make the necessary changes ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Community Contribution A label for all community Contributions PR metadata: Label to tag PRs, to facilitate with triage
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants