Live data from Hacker News

Go Analysis Framework: modular static analysis by go team

pkg.go.dev

91–100 of 100 posts

Re: Go Analysis Framework: modular static analysis by go team

#91

Earlier quoted context omitted.

If you have ``` a, err := f() b, err := g() if err != nil {} c:=a+b ``` The language will happily build and run, even though it should prevent to let you shoot in the foot.

Intentionally ignoring errors or explicitly not handling them are logic errors. No programming language shall protect you from them. In a little blunt form: This machine has no brain, use yours.

We can have the same argument about memory unsafe languages: "it should be the programmer that checks that the memory it's accessed in a safe way and not the machine"... and yet after many years and legions of programmers not being able to stop themselves creating memory leaks, we created memory safe languages and we advocate for their usage.

I hope it's clear that what was proposed above is just a toy problem to show the issue. In complex parts of the code, unfortunately, these errors sometimes happen. That's a fact.

A programming language should actually try to protect you from some logic errors. This is why have we programming languages in the first place. Otherwise why bother with garbage collection, types etc? Should we just use assembly then for maximal flexibilty and putting all the burden on the programmer?

Re: Go Analysis Framework: modular static analysis by go team

#92
post #74

Earlier quoted context omitted.

Kubernetes is a behemoth, but the Docker (Moby) source code is actually pretty easy to get into (if you're already a Docker user and understand how containers work)

If you think Docker code is easy to get into then you haven’t seen clean code.

The point is in Go it's pretty hard to write code so ugly that it'll be hard to read. I'm actually quite confident I could quickly onboard on almost any Go codebase (Kubernetes might be an exception). When I used to work with Java, I very rarely even tried to read the source code of my dependencies because it was unreadable.

Re: Go Analysis Framework: modular static analysis by go team

#93
post #20
post #13

So what is context? This isn't new and why the submission ?

Someone in the thread about the Ruff update ingenuously said they wished Go had something like Ruff, being unaware of this framework. In that thread, someone seemed to take it as a sign that many people who might care to know about this don't yet, so they made a dedicated post for it.

Thanks

Re: Go Analysis Framework: modular static analysis by go team

#94

Earlier quoted context omitted.

In practice you use Go with a linter, so this is not a problem in practice. I have never seen a missed error. The problem is painstakingly and manually having to build the stack trace, and then unwrapping it few layers above if you ever need to check what error it was.

You don't need a linter. Practically Go code will not compile without declaring and using the "err" returning from a function call. I have written a fresh example at https://files.bayindirh.io/misc/error_example.go . Give it a Go. ;) OTOH, gopls is a great LSP, though, and it warns you about this immediately.

What if the method only returns an error, like file.Close()?

Re: Go Analysis Framework: modular static analysis by go team

#95
post #59

Earlier quoted context omitted.

Oh yeah totally agree. I don't understand why someone would want to write `slices.Contains(s, needle)` when you can write this beautiful poem like a Shakespeare in VSCode: found := false for _, v := range s { if v == needle { found = true break } } Oh and I totally want to build a stack trace manually. It's like doing cardio to me. if err != nil { return fmt.Errorf("my function name but in spaces: %w", err); } This i…

https://pkg.go.dev/slices#Contains ? Also, if error wrapping hurts you so much (I don't use it), just implement a project-specific error that works how you want. This could be something that is JSON serializable, that captures a line number at each return site, etc. It will take like 10 minutes to get your project's errors working exactly how you want. My projects usually do - log.SetFlags(log.LstdFlags | log.Lshortf…

I know about slices module. But GP is not happy about generics apparently.

I think your error solution is good. But still it's a poor imitation of Exceptions wherein you have to capture the stack trace manually, possibly incuring a perf hit (haven't measured), and write handling code in every layer. Made worse by the fact that people return desperate errors for even precondition violations which should be panics.

Re: Go Analysis Framework: modular static analysis by go team

#96
post #48

Earlier quoted context omitted.

Oh yeah totally agree. I don't understand why someone would want to write `slices.Contains(s, needle)` when you can write this beautiful poem like a Shakespeare in VSCode: found := false for _, v := range s { if v == needle { found = true break } } Oh and I totally want to build a stack trace manually. It's like doing cardio to me. if err != nil { return fmt.Errorf("my function name but in spaces: %w", err); } This i…

This comment could make the same point and be even more effective if wasn't written as a snarky retort in violation of several of the guidelines of the site ( https://news.ycombinator.com/newsguidelines.html )

[deleted]

Re: Go Analysis Framework: modular static analysis by go team

#97
post #79

Earlier quoted context omitted.

I agree with you that it would have been nice with Result and option types in Go. But as someone else pointed out, unhandled errors are very rare in practice in Go because every Go project tends to use static code analysis tools that catches this. When people talking about things blowing up during runtime I can’t help but think that they can’t be serious Go users. On every build I generally run vet, lint (revive), st…

I'm actually not all that fussed about Result or Option types. I don't mind Go's approach to error handling. I think it should just have language/analysis features built it to ensure that you do it 'correctly', so things don't blow up if you do not remember a part of the system ("this value is sometimes nil") or you forget to do something. The fact that external tools exist that "every Go project tends to use" to fil…

You rarely get anything for free.

Look at exceptions in Java, for instance. In theory the mechanism is there to guarantee that errors will be handled. In practice there are multiple schools of thought — several of which purposefully circumvent this mechanism by throwing runtime exceptions because they think checked exceptions just leads to a lot of unnecessary work.

It has been too long since I used Java to remember all the arguments for and against. But I do remember trying multiple approaches and realizing that only using checked exceptions turned out to be a bit too inflexible. That there were legitimate reasons to use runtime exceptions.

Go’s type system is probably “good enough”. Sure, I’d love to have option types and results. And it would have been nice to move some of the things in vet, lint, staticcheck into the compiler. But in _practice_ separating them is actually beneficial. Because it allows you to trade build speed for security. For instance when you are making a tiny change and you just want to make sure it still builds. That’s a _practical_ tradeoff you could not have made if the compiler were always strict.

One thing I wish Go would have adopted is Javadoc-like comments with explicit markup to document parameters, return values and possibly panics. Those were _more_ useful in practice than exceptions in Java because you could force people to be more deliberate when designing “contracts”. When I did Java we used to have the build fail if classes and methods did not document all params, return values, exceptions etc.

I also taught people to write unit tests while _only_ looking at the Javadoc for what they were testing and notating at the code. Even after years of doing this, I’d regularly find that implementations didn’t behave as documented when writing tests. This made people care about interfaces/seams/APIs/contracts and spend less time obsessing over having the compiler save them from sloppiness. It put the developer in the driving seat rather than have them run behind the compiler and just nudging things into a state where it compiles. It helped people think more about why something failed from a design point of view rather than put all their faith in the tooling.

I think that’s the biggest mistake of Go: it doesn’t care about documentation and to the extent it does, it introduces really, really stupid, pointless formal rules that does nothing to help developers.

Re: Go Analysis Framework: modular static analysis by go team

#98

Earlier quoted context omitted.

Oh yeah totally agree. I don't understand why someone would want to write `slices.Contains(s, needle)` when you can write this beautiful poem like a Shakespeare in VSCode: found := false for _, v := range s { if v == needle { found = true break } } Oh and I totally want to build a stack trace manually. It's like doing cardio to me. if err != nil { return fmt.Errorf("my function name but in spaces: %w", err); } This i…

> Because having any language facilities for error handling is harmful. No, making error handling verbose mandatory is meant to make developers do mental cardio and be mindful of what they are doing. You can either keep developers mindful or punish them after they make a mistake by shouting at them and make them waste their time during re-compiling. P.S.: Yes, I love Go's error handling mechanics, which makes handlin…

> which makes handling errors mandatory

Unpopular take, but I personally also believe that unchecked exceptions in Java and other languages like that are an issue - because it makes too easy to miss where things could go wrong, instead of making you visibly handle the potential known failure points, or indicate to callers what you're not handling.

Re: Go Analysis Framework: modular static analysis by go team

#99
post #32

Earlier quoted context omitted.

100% with you on every point. It's the only language I've worked in that lets me read other codebases without an hour or two of "wut?" happening, so they did something right!

Have you tried to read the Kubernetes or Docker codebases?

Yes, I find the K8s codebase pretty easy to read actually, but I am biased since I work with K8s and the codebase makes a lot of sense when you understand what everything does already.

Re: Go Analysis Framework: modular static analysis by go team

#100

Earlier quoted context omitted.

> Because having any language facilities for error handling is harmful. No, making error handling verbose mandatory is meant to make developers do mental cardio and be mindful of what they are doing. You can either keep developers mindful or punish them after they make a mistake by shouting at them and make them waste their time during re-compiling. P.S.: Yes, I love Go's error handling mechanics, which makes handlin…

> which makes handling errors mandatory Unpopular take, but I personally also believe that unchecked exceptions in Java and other languages like that are an issue - because it makes too easy to miss where things could go wrong, instead of making you visibly handle the potential known failure points, or indicate to callers what you're not handling.

That's actually very popular with pretty much anyone who's dealt with errors happening in programs... which I would assume is most devs after some time.
Post reply on HN