On Tue, Oct 6, 2020 at 10:28 AM [email protected] <[email protected]> wrote:
>
> IMO, the bar function is cleaner and more readable than the foo function.
> How do you think?
>
> func foo() {
> if vs := f(); len(vs) == 0 {
> } else {
> for _, v := range vs {
> }
> }
>
> if vs := f(); len(vs) == 0 {
> } else {
> switch {
> case len(bs) == 0:
> case len(bs) == 1:
> default:
> }
> }
> }
>
> // vs.
>
> func bar() {
> if vs := f(); len(vs) == 0 {
> } else for _, v := range vs {
> }
>
> if vs := f(); len(vs) == 0 {
> } else switch {
> case len(vs) == 0:
> case len(vs) == 1:
> default:
> }
> }
Well, why stop there? Why not
if vs := f(); len(vs) == 0 {
} else f2()
if vs := f(); len(vs) == 0 {
} else x = y
There is a simple rule:
if <condition> <block> [else <block>]
In fact, for a while in the very early days of Go, that was the only
rule. But experience showed that people naturally want to write
if/else if/else, and having to add extra braces was frustrating, and
they tended to stack up. So the rule was extended to (roughly):
if <condition> <block> [else if <condition> <block> [else <block>]]
There's no obvious need to extend the rule in any other way. It's not
something that people are running into.
Ian
--
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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/golang-nuts/CAOyqgcW1brUebx3M-r3DZTJ5TZ-96XcmZCgpYgxfQ1Oh8auHuA%40mail.gmail.com.