Monday, January 14, 2013

Unsafe VB.Net programmers

“In computer science, type safety is the extent to which a programming language discourages or prevents type errorswiki

What is type safety in programming

We need to know how the types are being stored in the memory to understand it clearly. Type safety helps the language/runtime to allocate memory properly to the types so that there will be no data loss. If the language is type safe, it should not allow to store string values in an integer variable.

Problem with unsafe code

See the below code which is called not type safe.
int iVar = 256;
byte bVar = (byte)iVar; //value will be 0 as max value can be stored in byte is 255
Console.WriteLine(bVar);



The reason is during the execution the memory allocated to byte variable named bVar is 8bits which can store maximum of 255 (1111 1111).When a value which is greater than 255 comes, it became 0 .Actually it is not zero ( 0 ), it is 1 0000 0000.But only the right 8 bits are considered as the variable type is byte. Simply speaking this can introduce unexpected behavior into our program. VB.Net is such a language where our types are not safe.

Type safety and performance in VB.Net

As I told in the above paragraph, VB.Net allows us to write code which is not type safe. Lets see a code snippet to understand the issue.

Private Function GetValue()
    Return "1"
End Function
Private Function GetCharValue()
    Return "2"c
End Function
Private Sub Unsafe()
    Dim temporaryValue = GetValue() + GetCharValue()
    Dim finalValue = temporaryValue + 2
    Console.WriteLine(finalValue)
End Sub



Don’t confused about the compilation aspects of this program. It will compile and the result is 14. The temporaryValue will hold “12” in the form of string type and finalValue will be calculated to 14 which is result of numeric addition of 12 and 2. Can the string be added to integer in the mathematical way? Is there any magic of type conversion happens?

Yes there is a magic conversion code being injected by the compiler. If you want to see that magic code just use the Reflector which decompiles the assembly into source code.The code you can see in reflector is as follows

private void Unsafe()
{
    Console.WriteLine(RuntimeHelpers.GetObjectValue(Operators.AddObject(Operators.AddObject(this.GetValue(), this.GetCharValue()), 2)));
}



The magic is performed by Operators.AddObject() method. Is this comes in free? certainly not.It has its own performance implications. If you want to see the performance implications, navigate to the source code of Operators.AddObject method using reflector.

Also just think about a situation where the GetValue function returning a string which cannot be converted to number. It will end up in runtime exception. This could have avoided if the function is type safe. ie if function specifies the return type.

How to code VB.Net as type safe

‘ Option Strict On ‘.

This is the solution to all the type safety issues in VB.Net. You can enable this at any level in your source code. File level, project level etc…Better enable option strict at the Visual Studio IDE level itself. There are some more options available along with this which help you to raise compile time errors on ‘function with no return’, ‘use of unassigned variable’ etc…Better turn everything on.

If you are working on large legacy VB.Net codebase it would be difficult to turn the option strict on immediately. But gradually you can achieve that. My current project is a good example for such a project. Hundreds of VB projects which are not type safe. Only possibility is to code new projects in type safe manner. Even though the programmers know that there are possibilities to ensure the type safety in VB.net, some of them are still like to code without option strict on.

It is the programmers who are  type unsafe than the language.

Further reading
http://www.techrepublic.com/blog/smbit/visual-studio-compiler-options-part-2-option-strict/331

1 comment:

Blogger said...

If you want your ex-girlfriend or ex-boyfriend to come crawling back to you on their knees (even if they're dating somebody else now) you have to watch this video
right away...

(VIDEO) Get your ex back with TEXT messages?