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

cmd/gofmt: should handle function parameter indentation properly #11423

Closed
mattp16 opened this issue Jun 26, 2015 · 3 comments
Closed

cmd/gofmt: should handle function parameter indentation properly #11423

mattp16 opened this issue Jun 26, 2015 · 3 comments

Comments

@mattp16
Copy link

mattp16 commented Jun 26, 2015

Instead of this:

func DoSomething(
  arg1 string,
  arg2 int64,
  arg3 float64,
  arg4 bool) bool {
  var1 := 0
  var2 := ""
  ...
}

It should do this:

func DoSomething(
    arg1 string,
    arg2 int64,
    arg3 float64,
    arg4 bool) bool {
  var1 := 0
  var2 := ""
  ...
}

And yes, I know with this toy example I could just put all the arguments on one line. But sometimes when using external type names which can be rather long, it is unavoidable.

@griesemer
Copy link
Contributor

I recommend

func DoSomething(
    arg1 string,
    arg2 int64,
    arg3 float64,
    arg4 bool
) bool {
    var1 := 0
    var2 := ""
    ...
}

Generally, indentation is done with tabs only. One could use two tabs in your suggestion but that seems odd (only place where indentation is changed in steps larger than 1). Using spaces is after a tab is unprecedented in gofmt.

Unfortunate.

@mattp16
Copy link
Author

mattp16 commented Jun 26, 2015

Sorry if my use of space in my examples was confusing ... that was merely trying to control how the result would show, since tabs don't always show up the same in different editors/browsers/etc. I understand indentation is done with tabs only. I was not suggesting using spaces instead. I was just suggesting that function parameters to be indented more than the function body. Why do you think that seems odd? I believe this is very standard practice for most programming languages. Examples:

https://google-styleguide.googlecode.com/svn/trunk/cppguide.html#Function_Declarations_and_Definitions
http://www.oracle.com/technetwork/java/javase/documentation/codeconventions-136091.html#262

@griesemer
Copy link
Contributor

I'm aware it's standard practice elsewhere. It's also standard practice to never indent with tabs - or in other words: "standard practice elsewhere" is not a compelling argument...

The formatting suggestion I offered does work and avoids the irregularity here - think of the parameter list as opening a scope: indeed, the parameters are part of the function's body scope, which is "temporarily opened", and then closed again (hence the outdent) for the result type, and the opened again (indent for body). It's very regular. It also works for named results, and it scales:

func DoSomething(
    arg1 string,
    arg2 int64,
    arg3 float64,
    arg4 bool,
) {
   res1 bool,
   res2 string,
   err error,
} {
    var1 := 0
    var2 := ""
    ...
}

By scaling I mean for instance:

func DoSomething(
    arg1 string,
    arg2 func(
        x int,
        y float64,
    ) bool,
    arg3 float64,
    arg4 bool,
) {
   res1 bool,
   res2 string,
   err error,
} {
    var1 := 0
    var2 := ""
    ...
}

Leaving closed for now.

@mikioh mikioh changed the title gofmt should handle function parameter indentation properly cmd/gofmt: should handle function parameter indentation properly Jun 26, 2015
@golang golang locked and limited conversation to collaborators Jun 25, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants