4 ms·
reposted from r/golang: > Have is an indentation based language Nooope. That kills half the benefit of gofmt - fixing indentation. if foo: bar() Did I
by NateDad 10y ago
reposted from r/golang:
> Have is an indentation based language
Nooope.
That kills half the benefit of gofmt - fixing indentation.
if foo:
bar()
Did I forget a "pass" or did I forget to indent bar? You can't tell, so you just have to punt.
It also prevents fixing up copy and paste:
if foo:
--paste start--
if tooMuchIndentation():
something()
--paste end--
previouslyInsideIf()
What would your formatter do here? Did I mean for the last line to be inside the new if, or outside of it? You can't tell, so you have to make an assumption, and it'll be wrong half the time.
And that's not to mention all the other benefits of braces, like easy visual indication where blocks start and stop, having anonymous blocks (without if true), etc.
Also, requiring methods to be placed inside of structs (or, presumably other named types like type foo int) means that for large files, it won't be immediately clear what type a method is for.
Halfway down the 1000 line file network.go you have
func *String() string: return self.Name()
What type is this declared on? There's no way to know without scrolling up to the beginning of the type declaration. Also it means that almost all your code is uselessly indented once for no reason.
- wowoc 10y agoWell, that's a more general problem with indentation based languages, but Python users seem to be happy. It's a tradeoff, code that's (arguably) more pleasant to read, for a bit more work while writing it. It's true that gofmt-like tool would be less beneficial, but I think that most of the things you've mentioned could be helped with editor/IDE support, e.g. indent-aware paste, block navigation and so on.
- nilliams 10y agoYep this, basically the things gpp mentioned just aren't problems day-to-day for Python developers. And the 'halfway down the 1000 line file' one was just plain silly.
- YesThatTom2 10y agoI would expect havefmt would read the indenting using the Python indenting rules (which cover situations where, for example, one line is 10 spaces and the next line is 5 spaces and 1 tab). It would then write it out to be uniformly 2 space indents (if you are a Google employee) or 4 space indents otherwise. Yes, go has the benefit that you can do pathologically bad formatting and let gofmt fix it all. I love to type my Go code and rarely press RETURN, knowing gofmt will fix it. However, I also miss the Python indenting system.