-
Notifications
You must be signed in to change notification settings - Fork 18k
strings: maybe strings.Compare should be optimized #50167
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
Comments
The comment in the code explains why not |
I'm aware of that comment. But I think it should be optimized, as many projects use the function often. |
@seankhliao please reopen this issue. |
Many projects writing poor code doesn't mean we should optimize it, especially when we're actively trying to discourage it. |
Such code in many projects is not poor at all. |
If you think they are poor, please show the not-poor way. |
The comment explicitly says that we don't want to encourage people to use |
I understand this. But there are situations in which In other words, I don't think the comment is reasonable.
More specifically, there are many uses cases shown in https://sourcegraph.com/search?q=context:global+switch+strings.Compare+lang:Go+&patternType=literal are like: func foo(x, y string) {
switch strings.Compare(x, y) {
case -1:
// do something 1
case 0:
// do something 2
case 1:
// do something 3
}
} I think this is a common use case. |
Instead of worrying about func foo(x, y string) {
switch {
case x < y:
// do something 1
case x == y:
// do something 2
default:
// do something 3
}
} |
So for some cases, even if the operator way is not efficient, it is still recommended to use this way, by making the other way also inefficient deliberately? |
I would say, instead, focus on making the more readable approach efficient. Don't focus on making the less readable approach efficient. We aren't making |
Readability might be a subjective word. Personally, I don't think there is a readability difference between the two ways. The two ways are not overlapped fully. There are situations in which using |
And personally, the role of recommending the operator way to the |
And the third, the current docs of the |
One advantage of the |
I will close this issue, for I found a similar one: #31187 But I think close the issue for using |
OK, it is closed already. |
Some notes:
Some conflict with the internal comment of BTW, there are more uses of this function out of |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What did you do?
https://sourcegraph.com/search?q=context:global+switch+strings.Compare+lang:Go+&patternType=literal
What did you expect to see?
The
strings.Compare
should do one comparison for all cases.What did you see instead?
The
strings.Compare
does two comparisons for some cases.https://github.com/golang/go/blob/go1.17.5/src/strings/compare.go#L13
The text was updated successfully, but these errors were encountered: