Wednesday, 20 April 2022

Choose your SQL Database option on Azure

 Microsoft Azure provides various option for SQL Database on Azure platform and it is often confusing what to choose when. Here I’ll be trying to answer the questions and help you to choose the right SQL database on Azure.

  1. SQL Server on Azure VMs
  2. SQL Server Managed instances:
    - Single Instance
    - Instance Pool
  3. Azure SQL:
    - Single Database
    - Elastic Pool

Tuesday, 19 April 2022

Clean Code is an Art

 Good Code vs Bad Code, “A code, understood by the machine but not human is an example of Bad code even it is giving the right output whereas a code, understood by machine and human both is the Good code”. So I would say, An expert may write code but only an artists (Code Artist) can write good code. Remember “writing a clean & good code is nothing less than an Art”.

int c = 0; \\counter value
DateTime dt;
int counter = 0; 
DateTime systemGeneratedDate;
class Customer
{
}
is better thanclass CustomerDetails
{
}
public DateTime GetPlannedScheduledDate()
{
return plannedScheduledDate;
}
is better than the below function here:public DateTime GetDate()
{
return plannedScheduledDate;
}
//Bad comments example//Comment:Checking if an account is currently active
if(!account.IsDeleted && account.AvailableBalance > 500 && account.lastTransactionDate > DateTime.Now.AddMonths(-3))
{
//code logic
}
if(account.checkIfAccountActive())
{
//code logic
}
private bool checkIfAccountActive()
{
return !account.IsDeleted && account.AvailableBalance > 500 && account.lastTransactionDate > DateTime.Now.AddMonths(-3);
}
//This is our best attempt to get a race condition by creating large number of threads. 
for (int counter = 0; counter < 2500; counter ++)
{
//Code which creates a thread with every loop here.
}
  1. Peer Code Review: This is best techniques to clean your code. The more persons review your code, you get more ideas to make your code better. It is a human tangency to miss small things when you are on a problem to solve but those small things may affect the code readability, performance or security. Hence this is where Peer Code Review helps.
  2. Keep yourself updated: Whatever technology or language you use, it is very important to keep yourself updated so that you can use the latest technology or features to make your code better & better. Hence thumb rule here, keep learning.
  3. Use libraries (nuget packages): There are many useful open source libraries available to do which you might want to do so I recommend to use them instead of reinventing the wheel by your own. i.e. for data operations in .net there is a library called Dapper which is very useful”, so I recommend to use this instead of writing the whole piece from connection management to data management etc.
    At the same time, Be careful also while choosing the libraries and you must read the licenses or the terms & conditions.

Exception Handling Techniques & Tricks C#

 Exception handling could turn to costly operation in your code if you don’t pay much attention to it.

  1. Use Catch only if needed as it is costly operation, if possible use if instead.
    i.e. Divide by zero exception. why not do a check for zero before divide operation if possible.
try
{
var result = myVal/someParam;
}
catch(DivideByZeroException ex)
{
//do what you want
}
above code can be refactored like below to avoid the costly catch operation.if(someParam != 0)
{
var result = myVal/someParam;
}
else
{
//do what you wanted to do in the catch block
}
try
{
//code
}
catch(Exception)
{
}
return something;
try
{
//code
}
catch(Exception ex)
{
throw ex;
}
return something;
ORtry
{
//code
}
catch(Exception)
{
throw;
}
return something;
//Bad Code
try
{
//code
}
catch(Exception ex)
{
//do something
throw ex;
}
return something;
//better Code
try
{
//code
}
catch(Exception ex)
{
//do something
throw;
}
return something;
//Bad code
try
{
//some code
try //inner try catch 1
{
//some code
}
catch(Exception ex)
{
throw ex;
}
try //inner try catch 2
{
//some code
}
catch(Exception ex)
{
throw;
}
}
catch(Exception ex)
{
//code to handle the exception
}
{
method1();
method2();
}
private void method1()
{
try //inner try catch 1
{
//some code
}
catch(Exception ex)
{
//Handle exception and do not throw back to caller.
}
}
private void method2()
{
try //inner try catch 1
{
//some code
}
catch(Exception ex)
{
//Handle exception and do not throw back to caller.
}
}
try
{
method1();
method2();
}
catch(NullException ex)
{
//Handle exception. If needed catch can be classified well based on the exception type behavior of method1 and method 2.
}
catch(Exception ex)
{
//Handle exception
}
private void method1()
{
//code
}
private void method2()
{
//code
}
{
var result = method1();
method2();
}
private int method1()
{
int result;
try //inner try catch 1
{
//some code
}
catch(Exception ex)
{
//Handle exception and do not throw back to caller.
}
finally
{
result = 0;
}
return result;
}
private void method2()
{
try //inner try catch 1
{
//some code
}
catch(Exception ex)
{
//Handle exception and do not throw back to caller.
}
finally
{
//clean your mess-ups, like closing the open connection, fallback mechanism etc.
}
}
//Bad Practice
public Result PerformAction()
{
try
{
}
catch(Exception ex)
{
throw;
}
}
//Good Practice
public Result PerformAction()
{
try
{
}
catch(Exception ex)
{
return new CustomException("An internal error occured");
}
}
public class CustomException : Exception
{
}

Wednesday, 6 April 2022

Anti-corruption Layer pattern a life saver

 Anti-corruption layer pattern allows communication between one system or subsystem to another system or sub system without corrupting each other system domain or model or the objectives.

When to use this pattern

Exception Handling Techniques & Tricks C#

 Exception handling could turn to costly operation in your code if you don’t pay much attention to it.

try
{
var result = myVal/someParam;
}
catch(DivideByZeroException ex)
{
//do what you want
}
above code can be refactored like below to avoid the costly catch operation.if(someParam != 0)
{
var result = myVal/someParam;
}
else
{
//do what you wanted to do in the catch block
}
try
{
//code
}
catch(Exception)
{
}
return something;
try
{
//code
}
catch(Exception ex)
{
throw ex;
}
return something;
ORtry
{
//code
}
catch(Exception)
{
throw;
}
return something;
//Bad Code
try
{
//code
}
catch(Exception ex)
{
//do something
throw ex;
}
return something;
//better Code
try
{
//code
}
catch(Exception ex)
{
//do something
throw;
}
return something;
//Bad code
try
{
//some code
try //inner try catch 1
{
//some code
}
catch(Exception ex)
{
throw ex;
}
try //inner try catch 2
{
//some code
}
catch(Exception ex)
{
throw;
}
}
catch(Exception ex)
{
//code to handle the exception
}
{
method1();
method2();
}
private void method1()
{
try //inner try catch 1
{
//some code
}
catch(Exception ex)
{
//Handle exception and do not throw back to caller.
}
}
private void method2()
{
try //inner try catch 1
{
//some code
}
catch(Exception ex)
{
//Handle exception and do not throw back to caller.
}
}
try
{
method1();
method2();
}
catch(NullException ex)
{
//Handle exception. If needed catch can be classified well based on the exception type behavior of method1 and method 2.
}
catch(Exception ex)
{
//Handle exception
}
private void method1()
{
//code
}
private void method2()
{
//code
}
{
var result = method1();
method2();
}
private int method1()
{
int result;
try //inner try catch 1
{
//some code
}
catch(Exception ex)
{
//Handle exception and do not throw back to caller.
}
finally
{
result = 0;
}
return result;
}
private void method2()
{
try //inner try catch 1
{
//some code
}
catch(Exception ex)
{
//Handle exception and do not throw back to caller.
}
finally
{
//clean your mess-ups, like closing the open connection, fallback mechanism etc.
}
}
//Bad Practice
public Result PerformAction()
{
try
{
}
catch(Exception ex)
{
throw;
}
}
//Good Practice
public Result PerformAction()
{
try
{
}
catch(Exception ex)
{
return new CustomException("An internal error occured");
}
}
public class CustomException : Exception
{
}