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:<p><a href="https://play.golang.org/p/q5n0P-mKrmS" rel="nofollow">https://play.golang.org/p/q5n0P-mKrmS</a><p>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).<p>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.<p>Optimizer passes for eliminating this overhead were added to Go 1.13: <a href="https://golang.org/doc/go1.13#runtime" rel="nofollow">https://golang.org/doc/go1.13#runtime</a>
When mentioning C# (not sure about any other language), I would have mentioned the using statement and IDesposable objects.<p>This way each object's needed cleanup is encapsulated in each objects dispose method(s).<p>Granted using compiles down to a try-catch-finally with the Dispose method called in the finally, but there is an elegance to it.<p><a href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-statement" rel="nofollow">https://docs.microsoft.com/en-us/dotnet/csharp/language-refe...</a>
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.
The C# version is lacking the using pattern.
<a href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-statement" rel="nofollow">https://docs.microsoft.com/en-us/dotnet/csharp/language-refe...</a>
Another very interesting take on this from Andrei Alexandrescu:<p><a href="https://youtu.be/WjTrfoiB0MQ" rel="nofollow">https://youtu.be/WjTrfoiB0MQ</a><p><a href="https://github.com/CppCon/CppCon2015/blob/master/Presentations/Declarative%20Control%20Flow/Declarative%20Control%20Flow%20-%20Andrei%20Alexandrescu%20-%20CppCon%202015.pdf" rel="nofollow">https://github.com/CppCon/CppCon2015/blob/master/Presentatio...</a>
Since Java 7, try with resources handles this in a much cleaner way than the example.<p><a href="https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html" rel="nofollow">https://docs.oracle.com/javase/tutorial/essential/exceptions...</a>
It's weird to include a testing framework in it.<p>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.<p>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.<p>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.<p>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.<p>Most of the article was pretty good, though.<p>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.
There's also linear types, where you only give things capable of closing a resource a type that consumes a linear type.<p>There's also monadic resources, which you can view as a combination of RAII and what the author calls higher order functions.<p>Finally there's also monadic regions (as distinct from resources), which take advantage of higher rank types.
There's also Haskell's "bracket" (<a href="https://wiki.haskell.org/Bracket_pattern" rel="nofollow">https://wiki.haskell.org/Bracket_pattern</a>) which is similar to python's "with" statement but with plain functions instead of context managers.