Data Type in Programming!!
Here I am sharing some of my early experiences in Programming. When we write code for any application, generally we concentrate on Business Logic and different features available to meet the Business Logic. Whatever applications we are working on, we are ultimately playing with data .The placeholder for the Data is the variables/fields/properties, etc. When we are declaring data type for a variable, generally we don’t give much emphasis at that point. Here some points we need to take care
My Experience No 1:
I declared a variable as “int” data type for a PatientId. It worked fine in local, but when that code went to LIVE, the application gave exception because in LIVE PatientId is too long to accommodate in int data type.
So before declaring any variable we need to consider all the situations, application scope, environment .Even one code may be working fine today, after 1/ 10/20 days/months/years it will fail due to just due to data type declaration mistake.
Think more; write less is the policy for better programming.
My Experience No 2:
I declared a variable as int data type which was a placeholder for a result status (meaning, based on the result status that variable varies from 0/1/2/3. It can contain only these four values).
“int” data type is 4 byte integer in C#. But I can declare this variable as “sbyte” which represents a 1 byte integer. Since that variable contains only 1 or 2 or 3 or 4, declaring the variable as “int” is wastage of memory space.
Before declaring any variable we need to just think of the memory space needed for that variable, our requirement, any other good alternative for this variable.
To do programming efficiently in any language, we should have a very sound knowledge on data type. Most of the cases we found problem just due to our ignorance on data type and memory management.
My experience No 3:
I had to create a query dynamically .For this I declared a variable as string data type. From the database I was retrieving the main statement, all conditions and appending to that string variable. In C#, string is immutable. So every time whenever any string operation is done, a new string will be created in memory. Imagine how much memory I was wasting for a single query creation. String Builder is present in C# so that I can create my query dynamically using that without any memory wastage.
Sound Knowledge of Data Type is needed for writing a very efficient application.
My Experience No 4:
In one of my applications, I was retrieving data (in Database this column was varchar) from Database and converting that into integer using Convert.ToInt32. After one year, suddenly client complained that that application is not working properly. I tried to find out the problem and finally found that that column contains some non-numeric value. So when my application tried to convert to that integer, an exception was thrown. Just to avoid any exception we can use Int32.TryParse instead of using Convert.
So when we are developing any application, we should have a watch on the data type of the Database also.
My Experience No 5:
In the initial days, I was taking so much time when writing any code for my application. One day I tried to find out the reason. I asked so many questions to myself Lack of good knowledge in C# OOPS concept? Ans – NO Lack of good knowledge in syntax? Ans – NO Lack of ADO.NET knowledge? Ans – NO Lack of finding the logic for the solution? Ans – NO
Lack of File Operation concept? Ans – NO
When I finally asked “Am I quite confident in Data Types that are present in C# and SQL Server 2005?” ,I stopped there and started preparation for Data type.
So we should have a very strong knowledge on these below points
Different Data Types Memory Management for Different Data Types Conversion between different Data Type
Casting, etc.
Most victories (in life or in war) were obtained simply because the simple, basic things were done right. :))