Live data from Hacker News

Executable Examples in Go

bitfieldconsulting.com

51–60 of 79 posts

Re: Executable Examples in Go

#51
post #50

D's had that for many years. It's not a secret! https://dlang.org/phobos/std_algorithm_searching.html#.any See the buttons below the example code. You can even edit the examples and try variations.

I was curious for exactly how long "many years" was, and it appears it was added sometime between 2016 to 2017. The last archive without it is from 2016, the next archive with it is 2017: https://web.archive.org/web/20160405080736/https://dlang.org... Now that's of course just the website feature. Maybe D had executable examples that were tested by the compiler before Go, which would still be very interesting. Howeve…

> Maybe D had executable examples that were tested by the compiler before Go

Sounds like D's ability to specify unit tests in production source code, and have them optionally executed, fits the bill for that.

https://dlang.org/spec/unittest.html

Builtin unit tests were a great leap forward for code verification.

Re: Executable Examples in Go

#52
post #2

It's not a secret; they're all over the place. The article title is "Executable examples in Go". You shouldn't editorialize titles, especially when it makes the title significantly worse. Edit: seems the article title has changed or is A/B tested so never mind I guess (can't delete since there are replies), but it's still not a "secret".

Are we looking at the same article? Its title quite clearly is “Go’s best-kept secret: executable examples” so the title here is only minorly editorialized.

It seems as though the submitter is also the article author and changed it on the page to match the HN title.

https://web.archive.org/web/20230117212610/https://bitfieldc...

This is an interesting conundrum. Clearly the new title was chosen because it sounds more alluring; I mean, it hit the front page. But also, the OP could've easily done this prior to submitting and it would be totally normal, since people adjust article and post titles all the time.

Does come off as mildly sneaky when you do it in this order, but maybe I'm just nitpicky.

Re: Executable Examples in Go

#53
post #50

Earlier quoted context omitted.

I was curious for exactly how long "many years" was, and it appears it was added sometime between 2016 to 2017. The last archive without it is from 2016, the next archive with it is 2017: https://web.archive.org/web/20160405080736/https://dlang.org... Now that's of course just the website feature. Maybe D had executable examples that were tested by the compiler before Go, which would still be very interesting. Howeve…

> Maybe D had executable examples that were tested by the compiler before Go Sounds like D's ability to specify unit tests in production source code, and have them optionally executed, fits the bill for that. https://dlang.org/spec/unittest.html Builtin unit tests were a great leap forward for code verification.

Having unit testing built-in to the programming language and tooling is a huge plus, absolutely. I don't know who originally came up with this, but I swear, it would be worth some recognition :) (And thankfully, whoever did originally come up with this idea, it has been copied a ton by the recent crop of programming language designs.)

Re: Executable Examples in Go

#54
post #53

Earlier quoted context omitted.

> Maybe D had executable examples that were tested by the compiler before Go Sounds like D's ability to specify unit tests in production source code, and have them optionally executed, fits the bill for that. https://dlang.org/spec/unittest.html Builtin unit tests were a great leap forward for code verification.

Having unit testing built-in to the programming language and tooling is a huge plus, absolutely. I don't know who originally came up with this, but I swear, it would be worth some recognition :) (And thankfully, whoever did originally come up with this idea, it has been copied a ton by the recent crop of programming language designs.)

The E language supported doctests when I came to it in the 90s: http://erights.org/elang/tools/updoc.html

It's the earliest I'm aware of.

Re: Executable Examples in Go

#55
post #30

Python also has doctest https://docs.python.org/3/library/doctest.html since ... a long time now. The documentation tools are not as advanced as Go's and as far as I know, there's no way to "run the examples" yourself. It's a great way to write tests for some functions though, to me the sweet spot is for small, without side effects functions. It's totally possible for bigger functions but then all the initialization…

Dude doc tests are the one thing I really really miss when I moved over from working on python backend to a typescript backend… Actually unittest as a whole, testing in ts/JS feels like a mess, everything is just fanned out everywhere, there has been such a lack of structure in our codebase. I’m sure there are examples of well organized testing suites, but I’ve yet to find them.

Thing is, most testing frameworks/libraries in JS gives you a lot of freedom to structure the overall testing architecture however you want, without being much picky about it. That means teams writing the tests have to carefully consider their testing architecture, treating it like production code. But most people don't consider testing code "production" code, they treat it like something they should be able to write once and then kind of forget about it.

So zero regard for architecture will lead to "everything is just fanned out everywhere" and "lack of structure", no matter what language you're using, not just JS. What you're talking about tells us more about your team's engineering practices than how messy the JS testing ecosystem is.

Re: Executable Examples in Go

#56
post #11

Elixir has a similar concept with doc tests: https://elixir-lang.org/getting-started/mix-otp/docs-tests-a... You're comments/docs for the method can have demonstration code that is executed when you run your tests, ensuring your docs remain accurate. It's a great pattern.

Julia (Documenter.jl), Python (doctest) and R have (doctest) them too.

Nim: runnableExamples.

Re: Executable Examples in Go

#57
I don't understand why this is an article and why this shows up on hacker news at all. Other languages have been doing this for a long time as well. And there is no "secret" "kept" by anyone.

Re: Executable Examples in Go

#58
post #57

I don't understand why this is an article and why this shows up on hacker news at all. Other languages have been doing this for a long time as well. And there is no "secret" "kept" by anyone.

[deleted]
Post reply on HN