To us, here and now, it appears thus.

rants, ramblings and other things that make life worth living…

Is Type-Safety an Illusion?

leave a comment »

Today, I had an interesting conversation with a friend of mine, a self-professed Java geek and some one who is a hard sell for dynamic languages like python. And our conversation came back to the same topic, again and again – type safety. From what I know of Java, it seems more or less highly type safe unless there is some sort of ugly reinterpret_cast like construct. But, coming back to my home ground of C++, thinking about it, C++ is not really type-safe, In more cases than others, type-safety is nothing more than an illusion. C++, being a multi-paradigm language has a whole host of powerful and extremely useful features, which also makes shooting oneself in their foot extremely easy.

Here is my top three really useful features, which also are the big type-safety pitfalls.

  1. Variadics : Variadics is a really handy feature in C++, but its current syntax is a dark abyss for getting into type-safety hell. The way currently one handles variadics in C++ is this unholy mess of macros all starting with “va_”. These macros not only manipulate the stack directly, but return a memory image of the object. So, unless you are very sure of what type you are getting your hands dirty with, you are going to come up with serious bugs. The case here is even worse than python, where when you do the wrong operation on the wrong type, you get a run time exception. Which in my opinion is more type safe than the current scenario in C++.
  2. reinterpret_cast : reinterpret_cast is another potential pitfall that I’ve come across, especially when I am prototyping in C++. Sometimes, its just easy to do a reinterpret_cast and forget it. Its really handy when you are trying to develop with some one else’s code. That damn singleton class which needs one tiny extra bit of functionality that you absolutely definitely require, but you can’t extend it. So, what does one do? An ugly reinterpret_cast. And you’ve given yourself a golden pass to type-safety hell.
  3. void * pointers : These are the worst offenders. I am sure, everyone who has programmed in C++ have had enough pains with knowing how bad these little bastards can make your life. I won’t add to the woes, but just let me mourn along in silence here.

Signing Off,
Vishnu Vyas.

Advertisements

Written by vishnuvyas

November 12, 2007 at 10:41 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: