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

Group adjacent short variable declarations #306

Closed
djdv opened this issue Jun 20, 2024 · 1 comment
Closed

Group adjacent short variable declarations #306

djdv opened this issue Jun 20, 2024 · 1 comment

Comments

@djdv
Copy link

djdv commented Jun 20, 2024

I'd like to see a parallel to the "Single var declarations should not be grouped with parentheses" rule.
Where

foo := 1
bar := 2
baz := 3

and potentially

var foo = 1
var bar = 2
var baz = 3

becomes

var (
	foo = 1
	bar = 2
	baz = 3
)

The rationale is to have the formatter do the inverse of what it already does.

During editing, if I have a block of multiple declarations, then reduce it to one, the formatter will collapse it to the short form. But if I add declarations back into the same area of code, I have to reverse that process manually.


I don't think this is something people would want to have enabled all the time, nor would they by default, so I'm gonna say it should depend on #251 even if it gets accepted and someone decides to implement it.

Also, I want to acknowledge that this rule might be a lot tricker to implement than the existing one. And that most code written with adjacent shortform declarations is likely done so intentionally.
Admittedly this is usually only something I notice when I'm refactoring code too rapidly without thinking things through first 😅, but I figured it might be worth bringing up.

@mvdan
Copy link
Owner

mvdan commented Jul 19, 2024

We already do this for top-level declarations. Hence I assume you mean that you want the grouping to happen for local declarations too.

I fear this would be too invasive. It's fairly common to need to declare multiple short variables with := inside a function, and while I agree that vertical alignment is nice, forcing the user to need two extra lines for the opening and closing of the group is not great.

For example, take this func from Go's src/io/io.go:

func (discard) ReadFrom(r Reader) (n int64, err error) {
	bufp := blackHolePool.Get().(*[]byte)
	readSize := 0
	for {
		readSize, err = r.Read(*bufp)
		n += int64(readSize)
		if err != nil {
			blackHolePool.Put(bufp)
			if err == EOF {
				return n, nil
			}
			return
		}
	}
}

You suggest that we format it as:

func (discard) ReadFrom(r Reader) (n int64, err error) {
	var (
		bufp     = blackHolePool.Get().(*[]byte)
		readSize = 0
	)
	for {
		readSize, err = r.Read(*bufp)
		n += int64(readSize)
		if err != nil {
			blackHolePool.Put(bufp)
			if err == EOF {
				return n, nil
			}
			return
		}
	}
}

Personally, I think that is mildly worse. The formatting rules need to be clear wins so that the formatting is consistent across developers and the git diff churn is worth it. I'm afraid this wouldn't meet that bar.

@mvdan mvdan closed this as not planned Won't fix, can't repro, duplicate, stale Jul 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants