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

quickfix: suggest using slices.Concat when possible #1552

Open
ccoVeille opened this issue May 30, 2024 · 5 comments
Open

quickfix: suggest using slices.Concat when possible #1552

ccoVeille opened this issue May 30, 2024 · 5 comments

Comments

@ccoVeille
Copy link

golang 1.22 came with slices.Concat

we could now suggest using it instead of the good old way we had until go 1.22 append + elipsis

slice1 := []int{1, 2, 3}
slice2 := []int{4, 5, 6}

// Concatenating slices
concatenated := append(slice1, slice2...)

// ...

we can now use

slice1 := []int{1, 2, 3}
slice2 := []int{4, 5, 6}

// Concatenating slices
concatenated := slices.Concat(slice1, slice2)

// ...
@ccoVeille ccoVeille added the needs-triage Newly filed issue that needs triage label May 30, 2024
@arp242
Copy link
Contributor

arp242 commented May 30, 2024

Personally I wouldn't say that slices.Concat() is strictly better for just two slices:

concat := slices.Concat(slice1, slice2)

concat := append(slice1, slice2...)

Both are easy to read, and both will have the same performance.

However, with three or more slices it's a different story:

concat := slices.Concat(slice1, slice2, slice3)

concat := append(slice1, slice2...)
concat = append(concat, slice3...)
// Or:
concat := append(append(slice1, slice2...), slice3...)

Those are much harder to read. And slices.Concat will typically be faster (whether that matters is of course a different story, but in some cases it likely will).

So I'd say that it should only suggest this for 3 or more slices.

@ccoVeille
Copy link
Author

So I'd say that it should only suggest this for 3 or more slices.

I would say using slices.Concat would somehow promote the usage and curiosity of people to look at

I'm fine with saying it would be suggested only after 3 slices concatenated.

But I have to say. I know the ellipsis when using append confuses some juniore or people coming from other language.
Many expect using append(slice1, slice2) to perform what slices.Concat does. But that's another story. Because the suggestion would only occur on code already using the ellipsis.

@arp242
Copy link
Contributor

arp242 commented May 30, 2024

Well, maybe, but that seems mostly a stylistic preference than anything else. And also:

% rg -g '*.go' -F 'append(' ~/code | grep -F '...' | wc -l
528

And that's just my own projects; doesn't includes stuff from other projects I work on (I put those in ~/src rather than ~/code).

I'm not looking forward to replacing that with something that's basically just identical.

@dominikh
Copy link
Owner

For the time being I would only add a quickfix that can replace both the 2-slices and n-slices cases. I agree that we don't want to flag all existing code. I also don't want to differentiate between 2 slices and 3+ slices.

Improving existing code would be better suited to go fix (or a tool similar to it.) However, one could also make the argument that mass rewriting of existing code is too noisy and that code should be updated when it gets touched.

@dominikh dominikh changed the title [feature request] suggest using slices.Concat when possible quickfix: suggest using slices.Concat when possible May 30, 2024
@dominikh dominikh added new-check and removed needs-triage Newly filed issue that needs triage labels May 30, 2024
@ccoVeille
Copy link
Author

For the time being I would only add a quickfix that can replace both the 2-slices and n-slices cases. I agree that we don't want to flag all existing code. I also don't want to differentiate between 2 slices and 3+ slices.

Improving existing code would be better suited to go fix (or a tool similar to it.) However, one could also make the argument that mass rewriting of existing code is too noisy and that code should be updated when it gets touched.

I'm fine with this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants