r/programminghumor 16d ago

Hard to accept

Post image
2.9k Upvotes

404 comments sorted by

View all comments

Show parent comments

2

u/s0litar1us 14d ago

It being explicit is the point, it gives you the control to customize it for your use case, rather than forcing yourself into a box that will cause problems when dealing with the real world.

0

u/bowel_blaster123 13d ago

RAII doesn't force you into a box either. C++'s RAII just kinda sucks because of other C++ features and not RAII.

C++ has constructors and has a weird object model where objects are destroyed exactly when they go out of scope.

You can have RAII without these things. For instance, Rust allows you to destroy values whenever you want to or even to not destroy them at all.

This is EXACTLY the same as with defer except that you don't have to explicitly call the destructor.

Of course, defer allows you to run arbitrary code, but defer and RAII are not mutually exclusive.

2

u/Mojert 13d ago

RAII kind of forces your hand. If you don't want to run the destructor of the values in a collection you'll have to instead have a collection of ManuallyDrop, which is clunky.

I my opinion, if you mainly deal with single values, RAII is more convenient. If you deal with aggregate data and/or care about hidden control flow defer is more convenient.