Wednesday, 15 April 2020

2 Effective ways to debug your code | Code Revisited


We all have experienced the situation, where our program behaves different to what we actually want from it. In most cases, it is the simple bugs that we have missed during the development. And yet, it gets untraceable. Rather than staring at the program, thinking of what exactly went wrong, what you can simply do is debug it.


Here are 2 effective ways to debug your program.

Visualize the Flow

In order to apply this method in your development life cycle, you should be in a state to know what exactly you want the program to perform.

With this in place, it not only helps you understand the core logic, but also helps you to quickly catch up the exact bug that is restricting your program to perform the task you want.

Once you have a clear picture of your requirement, you convert that into the code that will do the task for you. This makes it easier for you to visualize the code, and look for possible points where you might have gone wrong.

Often times, it is actually problem with the object set. Of which NullReferenceException is the most – for me. So, remember visualizing your code, to figure out the most-likely points where you are setting up an object, or setting up data into an object.

Debugger

Possibly the most time consuming method when it comes to finding the error in the program. In case you are not familiar with the code, but you are assigned to trace out the issue it is causing to the system, then you can simply use the debugger.

Because, everything is in there, the code holds everything you are looking for, and it is just that you need to create or trace ways to find it.

Get the breakpoints in place, and run the program. As you get going with the breakpoints, subsequently hover over the objects to see what it holds currently.

This method proves to be efficient, but it consumes a lot of time, and is recommended to those who are not familiar with that piece of code.

But you have many more ways to debug a code, of which I believe these two are the most effective. If you would like to add your favorite way of finding the issues, or suggest to add other ways to this, please share with us in the comments.

Tuesday, 14 April 2020

Best way to insert Bulk data – Array Data Binding | Code Revisited

Finding out ways to insert bulk data into the Oracle Database table, I came across this effective way to insert bulk data into the Oracle table. Of course, there are many ways to insert, but as a programmer, the main objective is performance, aka time consumption.

Array Binding in Oracle

It is the easiest way to Insert/Update bulk data with more parameters, which you can easily build in C#.

After setting up an Object of OracleConnection class and opening connection of Oracle database.

You can setup Oracle Connection as:
OracleConnection conn = new OracleConnection(“UserId=XXAAXX, Password=12XX12; Data Source=Ora”);
conn.Open();

You can create an instance of the OracleCommand with the association of connection object.
OracleCommand cmd = conn.CreateCommand();

Then, you can simply enter the SQL command for Insert/Update using the 
CommandText option.

conn.CommandText = “INSERT INTO STUDENT_DETAILS VALUES (:rollNo, :firstName, ;lastName)”

Create arrays of values to insert into the columns in the table.
Add Parameter by associating it with the command instance.

cmd.Parameters.Add(“rollNo”, OracleDbType.Integer);
cmd.Parameters.Add(“firstName”, OracleDbType.VarChar);
cmd.Parameters.Add(“lastName”, OracleDbType.VarChar);

Set those parameters as:

cmd[“rollNo”].Value = rollNumArr;
cmd[“firstName"].Value = firstNameArr;
cmd[“lastName”].Value = lastNameArr;

Given that, you need to have all the values set up in the array to insert into the table.

After setting up the connection and parameter, its time for the execution.

Before going forward, you need to know the count of records to insert so that you can insert all the records in one go. For that you can bind the count of any of the Array input.

cmd.ArrayBindCount = rollNumArr.Count;

Now, its time for the execution. To be on a safer you can use the try catch block to execute, as sometime it leads to oracle exception of invalid parameter, or invalid number.

cmd.ExecuteArray();

Note: If this doesn’t work, mention the count of Array input inside the brackets like cmd.ExecuteArray(5); if the count is 5.

It is important to dispose the command, before closing the connection. You can simply enter the dispose() with the command object.

cmd.Dispose();

Finally, you need to close the connection.

conn.Close();

With the help of Array Binding, you can insert data in Bulk in a much faster way than any other methods. It can insert more than 1 lakh records within seconds.

If you have any questions or any suggestion to this article, tell us in the comment section.

Sunday, 12 April 2020

Best way to Find the Execution time of your code - C#Revisited


You want to find out exactly where it takes time? Believe it or not it is the most difficult point when it comes to the code you have written, unless it is not from Stack Overflow, which is very rare among us.

I have this simple solution to analyze the code based on the execution time of each section of code, thanks to such great features I get in c#.

C# includes the Stopwatch class in the System.Diagnostics namespace, which can be used to accurately measure the time taken for code execution. You don't need to use DateTime and calculate the time manually.

The following example measures the time taken for the execution of the for loop using StopWatch.

Example: Stopwatch

var execTime = new System.Diagnostics.Stopwatch();
           
execTime.Start();

for (int i = 0; i < 1000; i++)
{
    Console.Write(i);
}

execTime.Stop();

Console.WriteLine($"Execution Time: {execTime.ElapsedMilliseconds} ms");

Output:
Execution Time: 100 ms

In the above example, first we create an instance of the Stopwatch class. The Start() method starts measuring time for executing the code until we call the Stop() method. The ElapsedMilliseconds property gets the total time measured by the current instance in milliseconds. Here, it will return the time taken in executing the for loop. (Please note that output showed is arbitrary.)

You can use the StartNew method to initialize an instance of Stopwatch. Also, you can start the counter immediately without creating an instance of Stopwatch using the new keyword.

Example: Stopwatch.StartNew()

var execTime = System.Diagnostics.Stopwatch.StartNew();
           
for (int i = 0; i < 1000; i++)
{
    Console.Write(i);
}

execTime.Stop();

Console.WriteLine($"Execution Time: { execTime.ElapsedMilliseconds} ms");

Output:
Execution Time: 100 ms

The following example measures the total time taken for executing different code segments.
var execTime = System.Diagnostics.Stopwatch.StartNew();
           
for (int i = 0; i < 1000; i++)
{
    Console.Write(i);
}

execTime.Stop();
           

if (!execTime.IsRunning)
    execTime.Start();
           
for (int j = 0; j < 100; j++)
{
    Console.Write(j);
}

execTime.Stop();
Console.WriteLine($"Total Execution Time: {execTime.ElapsedMilliseconds} ms");

Output:
Total Execution Time: 130 ms

In the above example, the IsRunning() method checks whether a stopwatch is stopped or not (checks if the Stop() method has been called or not). If it is true then the Stop() method has not been called and the stopwatch is still running. If it is false, then the stopwatch is stopped. If a stopwatch is not running, then we can restart it and it will continue measuring time from where it stopped. Thus, we can measure the total execution time taken by different code segments.

The following example measures the time taken for executing each code segment.

var execTime = System.Diagnostics.Stopwatch.StartNew();
           
for (int i = 0; i < 1000; i++)
{
    Console.Write(i);
}

execTime.Stop();

Console.WriteLine($"Loop 1 Execution Time: {execTime.ElapsedMilliseconds} ms");


if (!execTime.IsRunning)
    execTime.Restart();
           
for (int j = 0; j < 100; j++)
{
    Console.Write(j);
}

execTime.Stop();
Console.WriteLine($"Loop 2 Execution Time: {execTime.ElapsedMilliseconds} ms");

Output:

Loop 1 Execution Time: 100 ms
Loop 2 Execution Time: 30 ms

In the above example, the Restart() method resets time to zero and starts measuring again using the same instance of Stopwatch. In this way, we can measure the execution time of different code segments using the same instance. We don't need to create a separate instance for each code segment.