this post was submitted on 06 Jan 2025
1 points (100.0% liked)
Hacker News
2315 readers
277 users here now
Posts from the RSS Feed of HackerNews.
The feed sometimes contains ads and posts that have been removed by the mod team at HN.
founded 11 months ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
TBH I am not a fan of defer. It requires you to remember to use it and to know what things need cleanup code to be called. I have done quite a few code reviews for go code at places I have worked in the past and people were always forgetting to close open file or sockets or other things that needed it resulting in quite a few memory leaks in production code. I got good at spotting these in reviews and even became a bit too proactive in pointing them out by highlighting things that looked like they needed to be closed but did not actually have a close method on them... You just cannot tell from the code alone and need to look up the documentation for everything that has a Open method on it.
So I much perfer rusts drop semantics or C++s RAII. Once something goes out of scope or otherwise freed it should automatically be cleaned up and not have to make the programmer remember to do so beforehand.
Now in a language like C which does not and likely wont have drop semantics or RAII then it might make sense as it is better then nothing at all. But I don't understand that arguments for putting it in C++ at all.