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

dev: rename function parameter i to issue #4460

Merged
merged 1 commit into from
Mar 5, 2024

Conversation

alexandear
Copy link
Member

@alexandear alexandear commented Mar 5, 2024

The PR renames the identifier i to enhance readability and maintainability.

It mostly renames i *result.Issue to issue *result.Issue. Other changes include newI to newIssue, copyI to copyIssue.

@ldez
Copy link
Member

ldez commented Mar 5, 2024

I will decline this PR, i is the right name for the receiver.

@ldez ldez closed this Mar 5, 2024
@ldez ldez added the declined label Mar 5, 2024
@bombsimon
Copy link
Member

I think the changes that was receiving arguments and variables makes sense. Single letter variables for receiving type is best practices but not as arbitrary variable in the code or as receiving argument afaik?

@ldez
Copy link
Member

ldez commented Mar 5, 2024

Not sure to understand 🤔

One letter for a receiver is best practice.

The problem is one letter as a variable. (i -> index)

I don't understand your message.

@ldez ldez reopened this Mar 5, 2024
@ldez ldez added feedback required Requires additional feedback and removed declined labels Mar 5, 2024
@bombsimon
Copy link
Member

bombsimon commented Mar 5, 2024

I guess it's subjective but for me:

// Receiving type, more common over `issue`
func (i Issue) Fn() {}

// Not the receiving type, more common than `i`
func Fn(issue Issue) {}

Example from stdlib (not the best source always but hey), they use f and buf, not f and b in func (f *File) Read(buf []byte).

I guess it depends on the type as well, you don't have much context over the type []byte but you do over Issue. I still just think it's good practice with somewhat descriptive variable names in code and as arguments but that's probably a personal preference.

Here's an article mentioning the same topic: https://medium.com/@TonyBologni/go-bits-variable-names-fa8240284edc. I notice that it actually refers to the wiki (that's now moved) that somewhat suggest otherwise so maybe I'm wrong here.

@ldez
Copy link
Member

ldez commented Mar 5, 2024

🤔 do you mean that the variable i (from the for) should be renamed to index?

func (i *Issues) Validate() error {
	for i, rule := range i.ExcludeRules {
		if err := rule.Validate(); err != nil {
			return fmt.Errorf("error in exclude rule #%d: %w", i, err)
		}

@ldez
Copy link
Member

ldez commented Mar 5, 2024

https://go.dev/wiki/CodeReviewComments#receiver-names

https://go.dev/wiki/CodeReviewComments#variable-names

There is a "fight" between the 2 rules from my POV about i as receiver names and index.

@alexandear
Copy link
Member Author

alexandear commented Mar 5, 2024

I agree that i is acceptable as a receiver name. I can revert all the changes made to the receiver and only rename parameters: i to issue, newI to newIssue, and copyI to copyIssue. Would that be acceptable?

@ldez
Copy link
Member

ldez commented Mar 5, 2024

I understand the root of my confusion: PR contains several changes.

  • argument names
  • receiver names
  • variable names

I was focused on the receiver names is

@bombsimon
Copy link
Member

I guess I'm just not consistent here so maybe dismiss my argument here. In general I wouldn't rename i in a for loop because it's such a well known pattern however in this case I probably would to avoid shadowing. Partly this is a reason for me commenting because I think if you can avoid i you might as well do that to avoid confusion.

Maybe this is not a rule that you can always apply, I wouldn't think func addTwo(n int) { return n + 2 } would be weird because it's such a small and simple function. The reason I commented was because I think a lot of the changes is in a place where i is used in a bigger scope and context and having a better name is helping readability. F.ex. newI := *i vs. newIssue := *issue is an improvement to me.

@ldez
Copy link
Member

ldez commented Mar 5, 2024

Sorry it's my fault for misreading the PR.
I was misled by the description.

@ldez
Copy link
Member

ldez commented Mar 5, 2024

@alexandear can you rebase your PR to fix the conflict?

@ldez ldez added enhancement New feature or improvement topic: cleanup Related to code, process, or doc cleanup and removed feedback required Requires additional feedback labels Mar 5, 2024
@ldez
Copy link
Member

ldez commented Mar 5, 2024

I'm still not convinced by the receiver name is (it's weird because is is a word related to boolean).

I think we can keep the other 2 letters receiver but is causes me trouble 😄

@alexandear alexandear changed the title dev: refactor to avoid 'i' as a variable/receiver/parameter name dev: rename function parameter i to issue Mar 5, 2024
@alexandear
Copy link
Member Author

Rebased and left only changes where i renamed to issue.

Copy link
Member

@ldez ldez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sorry again for misreading the PR.

@ldez ldez added this to the next milestone Mar 5, 2024
@ldez ldez merged commit 808c06d into golangci:master Mar 5, 2024
12 checks passed
@alexandear alexandear deleted the dev-refactor-avoid-i-name branch April 11, 2024 14:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or improvement topic: cleanup Related to code, process, or doc cleanup
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants