×

Smok Code's video: What is RAII in Software Development

@What is RAII in Software Development?
Resource Acquisition is Initialisation - simple, easy, everybody knows what that means right? Right? It has to do something with resources, and… how we prepare them for use? Well not really. This acronym isn’t great to explain what the concept is about, but we’ll get right into the nitty-gritty details, and I’ll tell you all about it. ++ Right. RAII is about proper management of resources: in normal flow and in case of exceptions. Let’s start by defining what resources are in this context. We’re most worried about resources when they’re big, or costly, so often you’ll find examples mentioning database and file handles or big chunks allocated memory. Truth is - that even a single little bit can be expensive if it’s mismanaged enough times. So our resources are also objects that get created, or even such an ephemeral thing like locks held on mutexes. Now why do we worry about managing these things? Simply because when one bit of code is using a limited resource - another can’t. Imagine a situation where you forget to release an exclusive file handle after you’re done with it and now you’re trying to open it again - you’re in for a good time. Same thing happens with locks or memory that isn’t freed back to the system. Many languages have dealt with memory management by use of a garbage collector mechanism, which is responsible for finding unused objects and destroying them automatically. However things like locks, database connections and file handles are still limited and we need to deal with that. This can get especially tricky when exceptions come into play. Here is a scenario: you open two files to write from one to another. First opens fine, but the second throws an exception - now you have to remember to release the first handle. Languages like java or C # will allow you to do it with try-catch-finally, you’ll put your cleanup code in the finally block. What about the languages that don’t have this mechanism, like C++? What about situations where you have multiple exit paths from a function - before every return you’ll have to explicitly free your resources, and we all know how forgetful you are ;) Here’s where the RAII idiom is really shining. It is primarily used in C++ because of the language's memory management model, but you can adapt it in other languages too. RAII is about wrapping the used resources in an object that allocates the resource in constructor, and frees it in destructor. Because in C++ destructors are called immediately when an object goes out of scope - you’re sure that no matter how your function exits it will call the destructor and release that lock, memory or file. You’re covered in case of unhandled exceptions, rethrows, multiple returns, and reaching the end of the function. RAII is a powerful mechanism that will take resource management off your back. C++ has multiple wrapper objects that use this behaviour - most commonly seen is … a string. Inside this class there is a pointer that can store long strings on the heap, and you can be sure that when a string object goes out of scope - memory from that pointer will be freed. Advanced scenarios include things like lock guards for mutexes - more great fun. RAII works perfectly in C++, but can be seen in Perl, VisualBasic or even garbage-collected C #. All you need is a mechanism to control the lifespan of an object. Now that you know it all, subscribe, and I’ll see you in the next one, cheers!

32

14
Smok Code
Subscribers
15K
Total Post
87
Total Views
316.4K
Avg. Views
5.6K
View Profile
This video was published on 2020-11-09 20:30:30 GMT by @Smok on Youtube. Smok Code has total 15K subscribers on Youtube and has a total of 87 video.This video has received 32 Likes which are lower than the average likes that Smok Code gets . @Smok receives an average views of 5.6K per video on Youtube.This video has received 14 comments which are lower than the average comments that Smok Code gets . Overall the views for this video was lower than the average for the profile.Smok Code #programming #tech #softwaredevelopment #cleancode #c++ Right. # #. has been used frequently in this Post.

Other post by @Smok