News for hackers. Stuff that matters.
Re:Deadcode: Finding unreachable functions in Go by jstanley on Tuesday January 23, 2024 @01:43PM (#39107696)
Cool but please don't make it a compile error!
Re:Deadcode: Finding unreachable functions in Go by codeflo on Tuesday January 23, 2024 @02:01PM (#39107977)
I still find Go too tolerant on this. It should scan your Git history and stash as well, and fail to compile if it finds any unused code there.
Re:Deadcode: Finding unreachable functions in Go by SuperCuber on Tuesday January 23, 2024 @02:44PM (#39108659)
That'd still be a bit too tolerant for my tastes. It should also scan your future commits.
Re:Deadcode: Finding unreachable functions in Go by viscanti on Tuesday January 23, 2024 @02:57PM (#39108866)
That'd still be a bit too tolerant for my tastes. It should have some kind of reinforcement signal that's stronger than just preventing the code from compiling. Some kind of shock therapy to help users learn and internalize things might be more appropriate here.
Re:Deadcode: Finding unreachable functions in Go by lainga on Tuesday January 23, 2024 @03:04PM (#39108957)

    go vet -basilisk ./...
Re:Deadcode: Finding unreachable functions in Go by yobert on Tuesday January 23, 2024 @03:00PM (#39108909)
Really it needs to scan my coworkers commits as well. Can't be too careful.
Re:Deadcode: Finding unreachable functions in Go by jerf on Tuesday January 23, 2024 @02:48PM (#39108727)
Ah, I know the programming language for you: https://news.ycombinator.com/item?id=5002597
Re:Deadcode: Finding unreachable functions in Go by mseepgood on Tuesday January 23, 2024 @03:15PM (#39109113)
Could you please expand on this? Why would local Git stashes relate to code quality?
Re:Deadcode: Finding unreachable functions in Go by erik_seaberg on Tuesday January 23, 2024 @03:55PM (#39109666)
That comment might not be entirely serious.
Re:Deadcode: Finding unreachable functions in Go by dewey on Tuesday January 23, 2024 @02:30PM (#39108430)
Why not? What do you gain by keeping unused code in the codebase?
Re:Deadcode: Finding unreachable functions in Go by 1zq on Tuesday January 23, 2024 @03:42PM (#39109478)
I definitely don't want to keep unused code in version control, but for debugging or for working on things iteratively as the sibling comment said, my Go code usually ends up peppered with

  if 1==2 { ...some code to temporarily disable ... }
"Just comment it out" is not always enough because when I want to quickly toggle the code on and off multiple times in a single session, it's not possible to do by only commenting/uncommenting that single block.

Sometimes the code that's commented out contains the only reference to an imported package, and by commenting it out the autoformatter helpfully removes the import from the top of the file, so then uncommenting the line is not enough -- I need to go back and re-add the import.

Or I may want to comment out this line

  x = f(a, b)
but that makes a and b in turn become unused as well, and I need to travel up to wherever they're defined and comment them out too, and so on with anything else that became thus unused in that chain.

And this is all suboptimal because the warning is also important when debugging. I do want to get warned about the unused code! Just let me compile.

Re:Deadcode: Finding unreachable functions in Go by dewey on Tuesday January 23, 2024 @04:03PM (#39109787)
> Sometimes the code that's commented out contains the only reference to an imported package, and by commenting it out the autoformatter helpfully removes the import from the top of the file, so then uncommenting the line is not enough -- I need to go back and re-add the import.

The same tool that removes your imports also adds them so there usually isn’t an extra step.

Re:Deadcode: Finding unreachable functions in Go by nulld3v on Tuesday January 23, 2024 @09:19PM (#39112835)
It often breaks stuff for me when I import two packages with the same name so I use a named import:

``` import "alpha/stuff" import bstuff "beta/stuff"

stuff.buyStuff() bstuff.stuffAnimal() ```

Once the autoformatter sees the named import is unused it removes it. Then when I uncomment my code again the autoformatter doesn't remember that it's supposed to use a named import. So in my example it wouldn't know "bstuff" is actually "beta/stuff".

Re:Deadcode: Finding unreachable functions in Go by lynndotpy on Tuesday January 23, 2024 @02:43PM (#39108636)
When I was doing Genuary in Rust, I started with a template that I would add some helper functions too that I could anticipate I would need for the art. Easy ones like "xy_to_polar".

I would do the first-time compile on this template and then add things iteratively.

Go would be a nonstarter for this experimental/artistic code, because I would usually have dead code in my "toolbox" to iterate and experiment with until I liked the end product.

Re:Deadcode: Finding unreachable functions in Go by dewey on Tuesday January 23, 2024 @02:45PM (#39108676)
In reality you just comment out the block, in day-to-day work it really is a non-issue in my experience.
Re:Deadcode: Finding unreachable functions in Go by lynndotpy on Saturday January 27, 2024 @09:39AM (#39155907)
I get what you're saying, but I don't share your experiences.

My reality is that I don't comment and uncomment lines, and I don't want to start. Commenting out a block of code takes more time and it's still unused code.

It really feels like a "Go is designed around problems Google had within Google" design choice.

Re:Deadcode: Finding unreachable functions in Go by radicalbyte on Tuesday January 23, 2024 @01:59PM (#39107946)
It would break reflection surely? It's nice to have as a configuration / flag though as many project will be able to require it.
Re:Deadcode: Finding unreachable functions in Go by erik_seaberg on Tuesday January 23, 2024 @02:27PM (#39108382)
Last I looked, reflection was missing a lot of things like looking up a function by name.
Re:Deadcode: Finding unreachable functions in Go by enneff on Tuesday January 23, 2024 @03:35PM (#39109377)
Go’s reflect package is not “missing” that feature, it is intentionally omitted as it defeats static analysis. In all my years writing Go (14 now), including some tricky metaprogramming, I have never needed to look up a function by name.
Re:Deadcode: Finding unreachable functions in Go by erik_seaberg on Tuesday January 23, 2024 @04:07PM (#39109845)
It creates busywork and ongoing maintenance issues in writing an interpreter. E.g., https://pkg.go.dev/text/template requires every possible function to be manually registered, and that’s not a good use of time.
Re:Deadcode: Finding unreachable functions in Go by zimpenfish on Tuesday January 23, 2024 @02:50PM (#39108757)
`MethodByName`[0] allows you to find a method on a struct but I'm assuming you must mean a generic package-level function rather than a method?

[0] https://pkg.go.dev/reflect#Value.MethodByName

Re:Deadcode: Finding unreachable functions in Go by erik_seaberg on Tuesday January 23, 2024 @03:51PM (#39109616)
Yeah, you can look up a method but only if you already have the receiver’s type (which you also can’t look up by name, or by listing a package).
Re:Deadcode: Finding unreachable functions in Go by zimpenfish on Tuesday January 23, 2024 @05:19PM (#39110761)
You don't need the receiver's type because you're calling `MethodByName` on a `reflect.Value` which you can construct from any receiver.

https://go.dev/play/p/hg-ugl7i0ei demonstrates this, I think?

Re:Deadcode: Finding unreachable functions in Go by AnimalMuppet on Tuesday January 23, 2024 @01:50PM (#39107803)
Well, unused imports are a compile error, so there's precedent.
Re:Deadcode: Finding unreachable functions in Go by jstanley on Tuesday January 23, 2024 @02:16PM (#39108199)
Yes, unreachable code after a "return" in a function is also a compile error. Very frustrating when I just want to nop out part of a function for debugging something else and have to keep fighting the compiler until I find a way that is acceptable.
Re:Deadcode: Finding unreachable functions in Go by starttoaster on Tuesday January 23, 2024 @04:08PM (#39109866)
If you find yourself doing this often, maybe a feature flag pattern using environment variables would end up being a bit nicer looking than throwing a bunch of unreachable code into your code base.
Re:Deadcode: Finding unreachable functions in Go by fooster on Tuesday January 23, 2024 @02:26PM (#39108356)
Huh? Its a warning, not an error.

https://go.dev/play/p/8GazBQv5xAb

Re:Deadcode: Finding unreachable functions in Go by jstanley on Tuesday January 23, 2024 @02:45PM (#39108678)
My mistake!
Re:Deadcode: Finding unreachable functions in Go by packetlost on Tuesday January 23, 2024 @02:36PM (#39108535)
If you're finding you're doing this often, perhaps your functions are too large