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

Documentation prefer-string-slice #2323

Open
xDivisionByZerox opened this issue Apr 25, 2024 · 3 comments
Open

Documentation prefer-string-slice #2323

xDivisionByZerox opened this issue Apr 25, 2024 · 3 comments

Comments

@xDivisionByZerox
Copy link

Description

The rule prefer-string-slice claims in it's documentation:

String#substr() and String#substring() are the two lesser known legacy ways to slice a string. It's better to use String#slice() as it's a more popular option [...]

While I can find proof for that statement for String#substr(), I can't seem to find any for String#substring(). Especially the "more popular" part is confusing to me. Was there any survey made about this? I was not able to find any. If there was, there should be a link to give more weight to this argument.
Unlike others, this rule feels very arbitrary in its reasoning why one should use it. I'd suggest a more thoughtful explanation here.

One suggestion, that came up in a discussion with a colleague, is to double down on the "Array counterpart" point:

[...] that has a consistent Array counterpart.

The argument should be that strings are simply arrays of characters and thus can be treated like them (spread, index accessor, ...). This makes it much easier to make good arguments in favor of using this rule in projects. This could lead to an increase of adaptability (although I doubt it).

Additional Info

I'm not against this rule as is. My reasoning for this issue comes from a (previously mentioned) discussion with a colleague who questioned the integration of the rule. I've sent him the rules documentation as they usually have pretty good descriptions of why you should (or shouldn't) adapt them. They pointed out the missing source for statements made which I had to agree with.

@sindresorhus
Copy link
Owner

While I didn't add the note about popularity and I don't have any proof of this. From my experience of reading a lot of open source code, .slice() is definitely more common. But I do agree we should have some actual proof of this.

@sindresorhus
Copy link
Owner

Another benefit we can mention is that .slice() supports a negative value for the first argument.

@xDivisionByZerox
Copy link
Author

Thank you for your fast answer. Are you accepting PRs to discuss the exact phrasings there or do you prefer to keep the discussion in the issue?

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

No branches or pull requests

2 participants