Avoid unused method receiver names #4

Opendmitshur opened this issue 2 years ago
dmitshur commented 2 years ago · edited

Do this:

func (foo) method() {
	...
}

Don't do this:

func (f foo) method() {
	...
}

If f is unused. It's more readable because it's clear that fields or methods of foo are not used in method.

Write Preview Markdown
willnorris commented 2 years ago

That's interesting, I didn't even realize that was valid syntax :). I've always prefered underscore to make it explicit that I'm not using the receiver:

func (_ foo) method() {
	...
}
Write Preview Markdown
dmitshur commented 2 years ago

@willnorris I actually used to also prefer the (_ foo) syntax, for the same reason you said. It's a more explicit way of saying that "I'm literally not using the receiver at all."

However, I later found evidence that it's more idiomatic to skip the _ altogether, so I simplified my style to follow suit. I should dig it up and add it to the references section. :)

Write Preview Markdown
tedyoung commented 2 years ago

In my limited exposure, I haven't seen this usage. An example (or pointer to actual usage) would be helpful.

Write Preview Markdown
dmitshur commented 2 years ago

@tedyoung This is one of the few suggestions that I came up with on my own, rather than from spotting an existing pattern. The motivation for doing this is as described:

It's more readable because it's clear that fields or methods of foo are not used in method.

So the reason for doing this is to help increase code readability, it's not for consistency with existing code. I'm sure this is used in some codebases but not others. I don't have links right now.

Write Preview Markdown
thomshutt commented 2 years ago · edited

When would you do this as opposed to not attaching it to the receiver at all?

Write Preview Markdown
dmitshur commented 2 years ago · edited

@thomshutt Both are valid, it depends on the situation.

Changing a method to a function is a bigger change that could break some other code, whereas not including the receiver (when it's unused) is a smaller change that would never affect the behavior.

If the receiver is completely unused, that's definitely a sign that maybe it's a good idea to turn it into a function rather than method. But sometimes you might want to keep it a method for various reasons. Perhaps to continue to implement an interface.

Write Preview Markdown
tleen commented 1 year ago · edited

I have to say "_" is a great choice because in other contexts it is the Go way of saying "I have to have this thing, but I don't need this thing". One look at a "_" in Go and you know the deal.

Write Preview Markdown
tleen commented 1 year ago · edited

(double post)

Write Preview Markdown
ilius commented 5 months ago

Is there a tool that finds all unused method receivers in a file/package?

Write Preview Markdown
dmitshur commented 5 months ago · edited

@ilius, not to my knowledge. It can be implemented at the AST-level, no type information needed.

It might be a good fit into either unused by Dominik Honnef or unparam by @mvdan. Or perhaps it should be a separate tool.

Write Preview Markdown
to comment.