Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do programmers of other languages, besides C++, use, know or understand RAII?

I've noticed RAII has been getting lots of attention on Stackoverflow, but in my circles (mostly C++) RAII is so obvious its like asking what's a class or a destructor.

So I'm really curious if that's because I'm surrounded daily, by hard-core C++ programmers, and RAII just isn't that well known in general (including C++), or if all this questioning on Stackoverflow is due to the fact that I'm now in contact with programmers that didn't grow up with C++, and in other languages people just don't use/know about RAII?

like image 675
Robert Gould Avatar asked Oct 03 '08 04:10

Robert Gould


People also ask

What languages have RAII?

RAII is associated most prominently with C++ where it originated, but also D, Ada, Vala, and Rust. People are noticing below that some other languages may have RAII as well.

What is the use of RAII in C++ programming?

Resource Acquisition Is Initialization or RAII, is a C++ programming technique which binds the life cycle of a resource that must be acquired before use (allocated heap memory, thread of execution, open socket, open file, locked mutex, disk space, database connection—anything that exists in limited supply) to the ...

Is it easy to learn other programming languages if I know C?

C is certainly the basis of a lot of other languages - but there are plenty of great programmers with good jobs who never learned it. Absolutely. C isn't that hard to learn actually. There aren't a lot of concepts to understand.

Should you learn C as your first programming language?

While C is one of the more difficult languages to learn, it's still an excellent first language pick up because almost all programming languages are implemented in it. This means that once you learn C, it'll be simple to learn more languages like C++ and C#.


2 Answers

There are plenty of reasons why RAII isn't better known. First, the name isn't particularly obvious. If I didn't already know what RAII was, I'd certainly never guess it from the name. (Resource acquisition is initialization? What does that have to do with the destructor or cleanup, which is what really characterizes RAII?)

Another is that it doesn't work as well in languages without deterministic cleanup.

In C++, we know exactly when the destructor is called, we know the order in which destructors are called, and we can define them to do anything we like.

In most modern languages, everything is garbage-collected, which makes RAII trickier to implement. There's no reason why it wouldn't be possible to add RAII-extensions to, say, C#, but it's not as obvious as it is in C++. But as others have mentioned, Perl and other languages support RAII despite being garbage collected.

That said, it is still possible to create your own RAII-styled wrapper in C# or other languages. I did it in C# a while ago. I had to write something to ensure that a database connection was closed immediately after use, a task which any C++ programmer would see as an obvious candidate for RAII. Of course we could wrap everything in using-statements whenever we used a db connection, but that's just messy and error-prone.

My solution was to write a helper function which took a delegate as argument, and then when called, opened a database connection, and inside a using-statement, passed it to the delegate function, pseudocode:

T RAIIWrapper<T>(Func<DbConnection, T> f){   using (var db = new DbConnection()){     return f(db);   } } 

Still not as nice or obvious as C++-RAII, but it achieved roughly the same thing. Whenever we need a DbConnection, we have to call this helper function which guarantees that it'll be closed afterwards.

like image 151
jalf Avatar answered Oct 14 '22 05:10

jalf


I use C++ RAII all the time, but I've also developed in Visual Basic 6 for a long time, and RAII has always been a widely-used concept there (although I've never heard anyone call it that).

In fact, many VB6 programs rely on RAII quite heavily. One of the more curious uses that I've repeatedly seen is the following small class:

' WaitCursor.cls '
Private m_OldCursor As MousePointerConstants

Public Sub Class_Inititialize()
    m_OldCursor = Screen.MousePointer
    Screen.MousePointer = vbHourGlass
End Sub

Public Sub Class_Terminate()
    Screen.MousePointer = m_OldCursor
End Sub

Usage:

Public Sub MyButton_Click()
    Dim WC As New WaitCursor

    ' … Time-consuming operation. '
End Sub

Once the time-consuming operation terminates, the original cursor gets restored automatically.

like image 21
Konrad Rudolph Avatar answered Oct 14 '22 06:10

Konrad Rudolph