Live data from Hacker News

The Setup-Cleanup Problem

blog.gnoack.org

1–10 of 27 posts

Re: The Setup-Cleanup Problem

#2
What I find an interesting design choice about Go’s approach (using ‘defer’) is that they are executed at the end of the function — not the end of the current block:

https://play.golang.org/p/q5n0P-mKrmS

This means that if you were to alter a function by placing parts of its body inside a loop, you may accidentally introduce O(n) buildup of deferred statements. This means that you could also be piling up resources associated with those resources (e.g., O(n) file handles).

Because the number of ‘defer’ calls is thus unbounded, it may be the case the compiler needs to generate code to store the list of pending closures on the heap. This may already happen for bounded functions if the compiler is unable to analyze it. In those cases it may thus be faster to resort to traditional C-like error handling.

Optimizer passes for eliminating this overhead were added to Go 1.13: https://golang.org/doc/go1.13#runtime

Re: The Setup-Cleanup Problem

#3

What I find an interesting design choice about Go’s approach (using ‘defer’) is that they are executed at the end of the function — not the end of the current block: https://play.golang.org/p/q5n0P-mKrmS This means that if you were to alter a function by placing parts of its body inside a loop, you may accidentally introduce O(n) buildup of deferred statements. This means that you could also be piling up resources as…

Is it the end of the function, or the end of the scope?

There's also no reason the compiler couldn't allow the deferred call once the last reference has been utilized (though in practice the common and performance minded case probably is to just use it as an additional goroutine that the context passes execution to when it queues any return messages).

Re: The Setup-Cleanup Problem

#4
post #3

What I find an interesting design choice about Go’s approach (using ‘defer’) is that they are executed at the end of the function — not the end of the current block: https://play.golang.org/p/q5n0P-mKrmS This means that if you were to alter a function by placing parts of its body inside a loop, you may accidentally introduce O(n) buildup of deferred statements. This means that you could also be piling up resources as…

Is it the end of the function, or the end of the scope? There's also no reason the compiler couldn't allow the deferred call once the last reference has been utilized (though in practice the common and performance minded case probably is to just use it as an additional goroutine that the context passes execution to when it queues any return messages).

The end of the function: https://golang.org/ref/spec#Defer_statements

Re: The Setup-Cleanup Problem

#5
post #3

What I find an interesting design choice about Go’s approach (using ‘defer’) is that they are executed at the end of the function — not the end of the current block: https://play.golang.org/p/q5n0P-mKrmS This means that if you were to alter a function by placing parts of its body inside a loop, you may accidentally introduce O(n) buildup of deferred statements. This means that you could also be piling up resources as…

Is it the end of the function, or the end of the scope? There's also no reason the compiler couldn't allow the deferred call once the last reference has been utilized (though in practice the common and performance minded case probably is to just use it as an additional goroutine that the context passes execution to when it queues any return messages).

> There's also no reason the compiler couldn't allow the deferred call once the last reference has been utilized

There's one reason not to do this - preserving the language semantics. Rust's version of RAII drops owned objects at the end of their scope for the same reason, even though it 'could' do better especially given NLL.

Re: The Setup-Cleanup Problem

#6
When mentioning C# (not sure about any other language), I would have mentioned the using statement and IDesposable objects.

This way each object's needed cleanup is encapsulated in each objects dispose method(s).

Granted using compiles down to a try-catch-finally with the Dispose method called in the finally, but there is an elegance to it.

https://docs.microsoft.com/en-us/dotnet/csharp/language-refe...

Re: The Setup-Cleanup Problem

#7
A comparison like this would be much more informative if it used more than one layer of setup/cleanup. When you get to four or five, it really highlights how well some of these scale up (or not). Also, neither nested functions nor mini state machines seem to get a mention, which is a shame. Nested functions are a good example of an approach that gets unwieldy fast as layers are added, and mini state machines scale as well as the "kernel" style without having to endure app-snobs' sneers for using a lowly goto.

Re: The Setup-Cleanup Problem

#9

What I find an interesting design choice about Go’s approach (using ‘defer’) is that they are executed at the end of the function — not the end of the current block: https://play.golang.org/p/q5n0P-mKrmS This means that if you were to alter a function by placing parts of its body inside a loop, you may accidentally introduce O(n) buildup of deferred statements. This means that you could also be piling up resources as…

> What I find an interesting design choice about Go’s approach (using ‘defer’) is that they are executed at the end of the function

It's quite a bad and bizarre design choice actually. It's strictly inferior to the other alternative.

Zig took the correct approach with its 'defer' and 'errdefer' constructs, fixing the two flaws with golang's implementation.

Re: The Setup-Cleanup Problem

#10
It's weird to include a testing framework in it.

That just speaks to usage of any orchestration class with overridable hooks, and has everything to do with events pertaining to domain (xunit's domain being "run a series of isolated tests") and not clarifying lower-level code organization. It's sort of like the author treated onFocus and onBlur in JS DOM events as startup/cleanup. It's just entry/exit orchestration.

The editorial stuff about being unnecessary also ignores that unit tests must fully reset the fixture between every test to be isolated--one reason test doubles are nice, they can generally be reset instantly--and that one of the more popular ways to organize unit tests is around common fixture handlers, aka setup/teardown routines.

This usually makes sense if you're testing units of an otherwise cohesive module since related operations usually use related fixtures. If it doesn't make sense then, sure, you move the setup/cleanup into each test but it totally craps up being able to review them for validity, etc.

Ideally a test is a clean known fixture handed to the test, one state change, and a verification of post-state, nothing more. Anything else complicates the test to some extent or the other, and (though devs get this wrong constantly) readability is paramount in tests or you don't know you're testing the right thing six months from now. Tests have to be their own docs. That's why setup and teardown exists, to hold everything but that.

Most of the article was pretty good, though.

Just...ruby, python, C++, xunit...misguided testing advice...wtf? Felt like the author was swimming outside their lane and missed a pass in editing, frankly.

Post reply on HN