05 April, 2009

Role of Type System in Software Development (Technical)

If some one asks what is C++, I would say better C with primary emphasis on type safety. So what is the significance of "Type System" in program development? I will try to explain...
During implementation of software, programmers usually overlook warnings thrown by the compilers that are against to the usual conversion rules. Unfortunately programmers will tend to suppress forcibly few warnings. A sort of type overriding will cause critical errors. Let us analyze two such kinds of idioms.

Brief Description of Type System
A type is set of values and associated operations, where as an Abstract Data Type represents generic mathematical model. An ADT will have its own definition in each programming language. For example the 'int' is a primitive type defined by compiler which allows addition, subtraction, multiplication and division on such variables. When an 'int' type is mixed with scalar variables (e.g. pointers) the kind of operations will be different, like pointer addition and subtraction. (multiplication and division are not valid) And, present day compilers are providing user defined operations on any of data type with the help of operator overloading.

Enumerations in C and C++
In C the enumeration constants (like INACTIVE given below, not enum variable) are just integers or can be converted to integers easily. After one or two stages of parsing, the compiler replaces the enumerated constants with their equivalent integer representations. And hence assignment of integer to enumerated type variable is a valid programming construct.

In C++, even though the literal INACTIVE is constant, it will have an associated type information during parsing. And an implicit conversion of a user defined type (like enum) to another type (like int) is not allowed. This will help us in making better software.

How the type system will help us?
As an example, observe the following code snippet,

typedef enum SystemStateTag
} SystemState;


The type SystemState represents state of a system in enumerated constants INACTIVE, OPERATIONAL and FAILURE. The compiler imposes enumeration type on any SystemState variable. Let us analyze the following code in terms of C and C++ compilation.

int calculateState();
SystemState sysState = calculateState();

In the above statement, the assignment

sysState = calculateState();

causes an implicit or explicit conversion of int to enum based on the type of compiler used.

As per ANSI C, the conversion from an int to enum is valid and implicit (no need of casting), and as per ANSI C++ the conversion is an error, requires an explicit casting. Some compilers (e.g. Borland C++ 5.02) treat the conversion as warning by default, and generates error message when compiled as per ANSI standard (try using –A option in Borland).

Since we are assigning the return value of function calculateState() to sysState, it is possible that the function may return values other than 0 (INACTIVE), 1 (OPERATIONAL) or 2 (FAILURE), in which case the system will be malfunctioning.

Perhaps this error would be trapped during system testing time and at this stage the correction effort will be more. By imposing C++ strict compiler type checking we can push such errors to compile time. In C, using defensive programming designs, like use of switch cases along with default statement, we can catch such errors at run time, but it would be too late.

Errors due to implicit conversions
The following code compares sysState against OPERATIONAL and the result is of type Boolean (can be of 0 or 1).

if(OPERATIONAL == sysState)
// process command

If the programmer tends to compare as shown below, is valid in C or C++

if(sysState = OPERATIONAL)
// process command

A C/C++ compiler implicitly converts ‘the return value of assignment statement (What it mean?)’ in ’if’ to Boolean type. It is the limitation of C++ to support legacy code written in C, it can be made fail safe by using defensive programming idioms as shown in the snippet2, because the statement OPERATIONAL = sysState is not valid in C/C++. This was an error that caused loss of $400 million during space craft take off.

Modern programming languages like C# imposes strict type checking, and will not allow any implicit conversions. Code snippet3 will not be allowed in C# or Java.
Especially Engineers making safety critical systems need to be aware of in depth knowledge in type system.

04 April, 2009

Utility Theory

Have you ever thought what will happen if the currency value falls or raises? What is the importance of Economics?
(Yet to Update :))

18 February, 2009

No One Perfect, Quarrels are Natural

18th Feb, 2009

Ohh.. After exactly an year, posting new message…

I received the following mail message from my friend, something to learn from it… just go through

Monica married Hitesh this day. At the end of the wedding party,
Monica's mother gave her a newly opened bank saving passbook.
With Rs.1000 deposit amount.

Mother: 'Monica, take this passbook. Keep it as a record of your marriage life.

When there's something happy and memorable happened in your new life,
Put some money in. Write down what it's about next to the line.
The more memorable the event is, the more money you can put in.
I've done the first one for you today. Do the same with Hitesh.
When you look back after years, you can know how much happiness you've had.'

Monica shared this with Hitesh when getting home.
They both thought it was a great idea and were anxious to know when the second deposit can be made.

This was what they did after certain time:

  • 7 Feb: Rs.100, first birthday celebration for Hitesh after marriage
  • 1 Mar: Rs.300, salary raise for Monica
  • 20 Mar: Rs.200, vacation trip to Bali
  • 15 Apr: Rs.2000, Monica got pregnant
  • 1 Jun: Rs.1000, Hitesh got promoted
..... and so on...

However, after years, they started fighting and arguing for trivial things.
They didn't talk much. They regretted that they had married the most nasty people in the world....
No more love...Kind of typical nowadays, huh?
One day Monica talked to her Mother:

'Mom, we can't stand it anymore. We agree to divorce. I can't imagine how I decided to marry this guy!!!'

Mother: 'Sure, girl, that's no big deal. Just do whatever you want if you really can't stand it. But before that, do one thing first. Remember the saving passbook I gave you on your wedding day? Take out all money and spend it first. You shouldn't keep any record of such a poor marriage.'

Monica thought it was true. So she went to the bank, waiting at the queue and planning to cancel the account.

While she was waiting, she took a look at the passbook record. She looked, and looked, and looked. Then the memory of all the previous joy and happiness just came up her mind. Her eyes were then filled with tears. She left and went home.

When she was home, she handed the passbook to Hitesh, asked him to spend the money before getting divorce. The next day, Hitesh gave the passbook back to Monica. She found a new deposit of Rs.5000. And a line next to the record:

'This is the day I notice how much I've loved you thru out all these years. How much happiness you've brought me.'

They hugged and cried, putting the passbook back to the safe.
Do you know how much money they had saved when they retired? I did not ask. I believe the money did not matter any more after they had gone thru all the good years in their life.

"When you fall, in any way, Don't see the place where you fell, Instead see the place from where you slipped.

“No one perfect, accept your partner as they are. There is no ABSOLUTE"