Readability of cmp.Diff function
amirrmonfared opened this issue ยท 7 comments
I believe we can make the readability of the Diff function output very much by just adding (-want +got)
I don't think this is going to change. This was discussed in the early days of cmp
deployment, and people could not agree whether got
or want
comes first. People were relatively evenly split between both. In the lack of a uniform consensus, we went with an approach that avoids choosing a path.
Feel free to create a package that simply wraps cmp.Diff
and injects the headers as you see fit.
For the record, I was in support of (-got +want)
, but there were a number of notable people on the Go team who instead argued for (-want +got)
. There are valid arguments for both:
- For
(-got +want)
, the argument is that it matches the conventional ordering of "got" appearing before "want". - For
(-want +got)
, the argument is that it displays a diff of how the expected value was changed into the current value (i.e., what changed relative to the expected value).
The Go style guide remains ambiguous about what direction to use. Although, it seems to suggest want, got
, but also seems to permit got, want
.
And also, while "got" and "want" make sense in tests, Diff
output isn't necessarily used only in tests.
Perhaps there should be a test-specific diff API with named parameters:
diff := cmp.C{
Want: want,
Got: got,
}.Diff(opts)
very tricky, looks like it is not settled;
- https://google.github.io/styleguide/go/best-practices#local-variables-in-tests
example of cmp.Equal(got,want) - https://pkg.go.dev/github.com/google/go-cmp/cmp#Equal
the docs of cmp.Equal(want, got) [we use this in our codebase but people are so confused which is which]
When it comes to cmp.Equal
it doesn't matter since the cmp
guarantees symmetric property of equality.
I found cmp.Diff example in google styling guide, I think it can be enforced pretty well if not, should be suggested on README.md perhaps, I will also check out other testing libraries and come back with my findings for - + or + -
https://google.github.io/styleguide/go/best-practices#tests
but you are absolutely right equality shouldn't care about want and got, it takes 2 numbers and return bool
perhaps at the end of the day + - and - + doesn't matter at all, just like yaml or yml where you stick one and in that repo everyone uses that side (aka eventual consistency)