Take Up Code / 219: C++ RAII Sounds Complicated But Simplifies Your Code.
- Resource allocation is initialization or RAII for short may be hard to pronounce but will help you write better and simpler code.
- Publishing date
- 2018-01-22 05:49
- Wahid Tanner author
Resource allocation is initialization or RAII for short may be hard to pronounce but will help you write better and simpler code.
There’s a tradeoff with RAII just like anything else. But the problems it helps you solve are well worth the extra code needed. And most of the time, you don’t have to write this extra code. You just need to be aware of the situation and know how to use RAII to your advantage. I’ve talked about this briefly in episodes 55 and 127. Listen to them for more information.
In order to make full use of RAII, you’ll need a language like C++. Languages with garbage collection are not going to use RAII to its full extent. That’s because you really need a language that defines the lifetime of objects and especially the exact moment that their destructors will be called in order to make full use of RAII. Listen to episodes 17 through 22 for more information about object-oriented programming, constructors, and destructors.
And listen to this episode for an example that should help explain why RAII is an important technique to help us simplify our code so that we don’t have to manually clean up resources when we’re done.