5 ms·
> Basically no one should use strings.Compare Others have already talked about the performance aspect but I'm just baffled at the comment basically saying nobo
by quelltext 4y ago
> Basically no one should use strings.Compare
Others have already talked about the performance aspect but I'm just baffled at the comment basically saying nobody should use this function anyway.
I expect the need for 3-way compares isn't that uncommon, why tell people not to use it?
It's great to have this idea that the compiler should optimize all comparison situations but a) it doesn't yet and b) people still want a 3-way compare function anyway.
Basically it's saying "don't use a function at all", or even "write your own copies of this function". Even if the compiler one day becomes smart enough to optimize you still end up with tons of code duplication if people followed the advice.
And for people who care about performance it means they'll use a 3rd party library or implement their own "optimizations" that might not be effective or even buggy.
Literally nobody is benefiting from this.
What's the point of providing this function but actually thinking nobody should use it, in a comment no less instead of documentation?
Now, there may be other reasons, e.g. the author thinking 3-way compare is a bad pattern in the first place. If argued well, maybe I could agree. But that argument isn't made here.
I'm also not saying they needed to optimize this off the bat. A comment saying "we don't think there is a big demand yet, will optimize when we see the demand" would've been acceptable.
- thayne 4y ago> you still end up with tons of code duplication if people followed the advice. Code duplication is the go way
- skybrian 4y agoThe comment is confusing, but the idea seems to be that instead of calling this function, you should inline the code - that is, just write the comparisons yourself. You don't need a function call. I guess this is for stylistic reasons, but I don't know why anyone would feel strongly about doing it one way or the other.
- quelltext 4y agoYes, I mentioned that that's basically what it says. But why would you tell people to actively duplicate code? It's like "I don't get what this type of function does and have to look it up all the time, better to have everyone inline this so it's clearer". To be fair, I agree that seeing bad examples of its use shown by one of the other folks here, some could have been avoided by them being forced to inline, but funnily those examples still exist despite the desires of the function author. Nothing was achieved here.
- barsonme 4y agoI think manual three-way comparisons are very clear. But so is strings.Compare. For better or worse, copying code is a pretty normal thing to do in Go. See "A little copying is better than a little dependency." [0] [0]: https://www.youtube.com/watch?v=PAAkCSZUG1c&t=9m28s&themeRefresh=1 https://www.youtube.com/watch?v=PAAkCSZUG1c&t=9m28s&themeRef...
- codeflo 4y agoI even somewhat agree with that principle, but it don’t think it applies to using the standard library, which is (a) not little and (b) not a new dependency you add to the project. If that’s the justification to keep an implementation bad on purpose (which I’m not sure is the actual intention, but is at least that’s what the referenced comment claims), then I’m not even sure I speak the same language as the people who made that decision.
- somekyle2 4y agoThis is a part of Go's rather odd perspective that I appreciate. I've worked in codebases with library helpers for everything that mostly served to turn two clear lines into a function call. Once such functions exist, folks feel obligated to use them; after all, why duplicate code? So now, what would've been a couple dozen lines of self-contained straightforward code has 3 imports and 5 functions you need to be familiar with to understand it. It also makes compilation more expensive. I'm not saying library functions are bad or anything like that, but there's value to keeping the typical vocabulary of code small, of not adding new dependencies to solve trivial problems. Especially in a standard library that is widely used and you expect to be maintained for years. Providing functions that solve problems that are impossible or tricky in the language is important; providing `Plus(a, b int) int` that wraps `+` makes the library worse. I think there's a good argument for providing Compare() and Abs() and other fairly trivial functions even if it is perhaps less effort to not use them in most cases, but for a stdlib I can appreciate the logic of leaving out what isn't providing clear value.
- xmprt 4y ago
- barsonme 4y agoGo doesn't have many features of other languages. There are several competing goals, including keeping the language small, not hiding complexity, etc. Manually writing the three-way comparison fits in with these goals. If slices were comparable then I'd wager that bytes.Compare would not exist.
- saghm 4y agoWhat sticks out to me is the part saying "the compiler should be changed". It's weird enough that this isn't even a doc comment but a comment inside the function (making it harder for people using the function to notice), but even as someone who thinks the passive voice is often unfairly maligned, the phrasing immediately brings to mind the question "who should change the compiler?" Is the Go standard library not maintained by the same group of people as the compiler, or is this comment just a doubly obfuscated "TODO"?
- yencabulator 4y agoIt's not a TODO yet, it 's a note that says if you think this would be a good idea, do that instead. It's a potential TODO-to-be, waiting for the need.
- saghm 4y agoIs that better? That would mean that they have a method in their API that they don't think anyone should use but haven't deprecated it or documented it that way and have no plans to do anything about it.
- yencabulator 4y agostrings.Compare is mostly used in trivial demonstration programs, and is there because bytes.Compare is there. It makes https://pkg.go.dev/sort#Find https://pkg.go.dev/sort#Find documentation simpler. Deprecating strings.Compare would make sort documentation worse. Real code tends to not be that trivial, and that's why real code most likely shouldn't be using strings.Compare.
- zenexer 4y agoI don’t think that’s true. I believe they want you to explicitly define how you want you compare strings. Comparing Unicode strings isn’t as straightforward as comparing individual bytes or code points. For example, there are multiple Unicode strings that will yield the character “ï”. If you use a naïve comparison function, you can’t be sure that it will behave as expected when it encounters the word “naïve”.
- donatj 4y agoYou’re looking for Collator.CompareString https://pkg.go.dev/golang.org/x/text/collate#Collator.CompareString https://pkg.go.dev/golang.org/x/text/collate#Collator.Compar...
- sltkr 4y ago> I believe they want you to explicitly define how you want you compare strings. If that were true, they wouldn't have made strings comparable in the first place, and they wouldn't recommend that callers inline the implementation (they'd recommend calling some locale-dependent function instead). An explicit comparison like "naïve" == "naïve" or "naïve" < "naïve" isn't any more clear about how the comparison is performed than Compare("naïve", "naïve") would be.
- snotrockets 4y ago
- diceduckmonk 4y ago> it is designed to be friendly to enterprises who have to produce code Our two person engineering team has found this to be the ultimate form of user-friendliness as our goal is to pragmatically deliver software.
- _AzMoo 4y agoI think what they're saying is that you need more code in Go, which is inherently unfriendly to developers, to produce an equivalent output in other languages. And to a certain extent that is true, but it disregards the intangible benefits of Go, such as it's balance between simplicity and the ability to make it perform.
- snotrockets 4y agoNo. I'm rephrasing the design intent for the language, as outlined in various talks. For example, here: https://go.dev/talks/2012/splash.article https://go.dev/talks/2012/splash.article
- llanowarelves 4y agoIt is like Demo deprecating fs.exists().[1] [1]https://github.com/denoland/deno_std/discussions/2102 https://github.com/denoland/deno_std/discussions/2102
- deleted 4y ago[deleted]