gofmt: one line if statement

6,235 views
Skip to first unread message

Tong Sun

unread,
Jan 6, 2018, 11:09:41 AM1/6/18
to golang-nuts
Hi, 

[This is just a thought, no flaming please if you don't agree]

Since `gofmt` supports one line func() definition, how about maintaining one line if statement as well if the user did so? 

This way, many of the simple three line code can be simplified to one:

I.e., from 

 if !condition {
   
return
 
}


to just 

 if !condition { return }


provided that the user uses the one line if format in the first place. 

Thus, a typical sample code can be dramatically simplified, because so many three-line error checkings can now be, 

if err != nil { panic("something wrong") }

Thoughts? 


Axel Wagner

unread,
Jan 6, 2018, 12:34:07 PM1/6/18
to Tong Sun, golang-nuts
I feel there is a significant difference between the two: func-literals are expressions, while if-statements are… statements :) i.e. I'd rather be in favor of reformatting func-literals, but I believe the fact that they are expressions makes that not a great idea, as they are often used in arguments to function calls and the like and you probably don't want to forcibly split those.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Tim Heckman

unread,
Jan 7, 2018, 7:10:50 AM1/7/18
to golang-nuts
I think that this is a good question. I do question whether your code examples are more or less simple when they are a single line. I'm worried they may be more compact, which hurts the readability, while still retaining the same complexity.

Being that you should be running `gofmt` before commiting, isn't this a bit of a non-issue? You can write a single line, and then `gofmt` will take care of the rest for you. You can use the brevity when you're developing, but gain the readability again after `gofmt` works its magic.

To explain further, I find that multi-line if-blocks are more readable. I originally formatted my func-literals on a single line, because I had come from a Ruby background. Over the years I've changed my preference and actual prefer the multi-line approach for my functions.

My editor also has a nice little completion where I can type `iferr`, followed by a hotkey, and I get the block formatted out for me. This makes it even less of an issue for me personally.

If-anything, my preference would be for `gofmt` to forbid single-line func literals and to make them multi-line always.

Cheers!
-Tim

matthe...@gmail.com

unread,
Jan 7, 2018, 10:02:44 AM1/7/18
to golang-nuts
I like the C if one-liner without curly braces but that doesn't apply to Go since the if condition isn't in parenthesis.

if (exists == false) i++;

I've never liked the non-braces version of if when across multiple lines. In JavaScript I've started always writing out if like in Go and will probably do the same in C languages in the future.

For Go having the curly braces on one line doesn't seem more readable to me.

Matt

Jeff Goldberg

unread,
Jan 7, 2018, 6:44:47 PM1/7/18
to golang-nuts
Yes, but ...

First the "yes" part.

I find myself writing one line if statements, and then finding myself annoyed at gofmt. When I write a one line if statement, there is a presentational reason for it.

But

But such formatting would have to be an all or nothing thing if we wanted consistency instead of developing a bunch of competing styles. There will be case where there is only one statement within the conditional block where doing it on one line isn't what we want. So we've got three choices for the formatter.

1. Always put single statement if blocks on one line
2. Always do things as we do them now.
3. Leave it to the user.

Number 3 leaves us with inconsistencies. And one of the reasons for gofmt is to eliminate many style inconsistencies. So I think that (3) just is not an option. That leaves us with a choice between (1) and (2). And with such a choice, I think that 2 is the far better option, as the cases where (2) is "wrong" are mildly irritating, while cases in which we do (1) where it really isn't appropriate will be god-awful.

So I'm happy to be mildly irritated when gofmt breaks up my single line ifs when I consider the alternatives.

Cheers,

-j

gavin...@gmail.com

unread,
Jan 7, 2018, 8:58:05 PM1/7/18
to golang-nuts
At the moment func declarations leave it for the user, depending on whether the original was written on one line or not.

gofmt will change:

    func hi(){fmt.Println("Hi.")}
    func bye(){
    fmt.Println("Bye.")}

into:

    func hi() { fmt.Println("Hi.") }
    func bye() {
        fmt.Println("Bye.")
    }

So if statements could format the same way func declarations do.

Malcolm Gorman

unread,
Jul 5, 2019, 5:55:04 AM7/5/19
to golang-nuts
Excellent idea!

Occasionally an error message needs to be created or elaborated, requiring a multi-line if statement body.

But the common situation is where the error is simply returned to the calling function using a single-statement if statement.

If these single-statement if statements could be:

if err != nil { return <any return arguments>, err }

the visual appearance of code would become far more compact and easier to read, and we wouldn't need any special new syntax.

Jan Mercl

unread,
Jul 5, 2019, 6:21:40 AM7/5/19
to Malcolm Gorman, golang-nuts
On Fri, Jul 5, 2019 at 11:55 AM Malcolm Gorman <malcolm...@gmail.com> wrote:

> If these single-statement if statements could be:
>
> if err != nil { return <any return arguments>, err }
>
> the visual appearance of code would become far more compact and easier to read, and we wouldn't need any special new syntax.

I prefer the opposite. I even add an empty line after the if-block
when it ends in a return.

if foo() {
bar()
}
if baz() {
return qux()
}

fmt.Prinln(42)

Example in real and recent code:
https://gitlab.com/cznic/gocc/blob/aef018deec791efc6b97da0f3369f6d7ca059aee/main.go#L230
Reply all
Reply to author
Forward
0 new messages