Skip to content

Fix 'StringBuilder' initialization - #3458

Merged
Dongbo Wang (daxian-dbw) merged 3 commits into
PowerShell:masterfrom
iSazonov:fix-stringbuilder
Mar 31, 2017
Merged

Dongbo Wang (daxian-dbw) merged 3 commits into
PowerShell:masterfrom
iSazonov:fix-stringbuilder

Conversation

@iSazonov

Copy link
Copy Markdown
Collaborator

Close #3457

if (errors.Count > 0)
{
var allErrors = new StringBuilder('\n');
// Bug: don't use '\n' ('char' convert implicitly to 'ushort' and 'int' and initialize 'StringBuilder' size)

@daxian-dbw Dongbo Wang (daxian-dbw) Mar 30, 2017 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you change Bug: to Note: please? Bug: usually indicates there is a potential bug here that needs to be revisited.

if (errors.Count > 0)
{
var allErrors = new StringBuilder('\n');
// Note: don't use '\n' ('char' convert implicitly to 'ushort' and 'int' and initialize 'StringBuilder' size)

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.

While the comment is interesting, I think it's a distraction from understanding the code.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This should only protect against reverting.
(Feel free to approve the 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.

I still don't think it's helpful - education of weird aspects of an api don't really belong in random places in code unless it's extremely likely that you'll hit the problem again.

In this case, I think it's unlikely.

@iSazonov Ilya (iSazonov) Mar 31, 2017 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

What is the preferred solution do you see here? Remove the comment?
Complete solution I believe should be in CoreFX or in the compiler.
Also we can do here:

var allErrors = new StringBuilder(); 
allErrors.Append('\n'); 

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.

I think removing the comment is sufficient as it's mostly obvious that a string argument initializes the instance.

If you'd like to make other improvements though - it's good practice to pass the estimated capacity when constructing a StringBuilder to minimize extra allocations, but only if it's easy to estimate (e.g. use a constant, avoid computing the "estimate").

And calling Append with a char might be slightly better than passing a string to the constructor, but honestly the performance here doesn't really matter.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Jason Shirk (@lzybkr) Thanks for great comment!
Here we cannot use a constant, the capacity is ~(1+ 2 * errors.Count).

Do it make sense to open CoreFX issue to add new constructor for StringBuilder ?
StringBuilder new(char value)

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.

To be clear, a reasonable estimate is probably better than the default, and constant, or easily computed value, e.g. count*80 our something like that is reasonable. But again, it really doesn't matter here.

As for a CoreFX issue - it's up to you. I feel like they might discuss it, but reject it in favor of fewer APIs because it should if you properly test, you'd notice your character is missing.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

@daxian-dbw
Dongbo Wang (daxian-dbw) merged commit 544e466 into PowerShell:master Mar 31, 2017
@iSazonov
Ilya (iSazonov) deleted the fix-stringbuilder branch April 3, 2017 03:19
Thatgfsj (Thatgfsj) pushed a commit to Thatgfsj-contribs/PowerShell that referenced this pull request Aug 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants