Pages

Monday, September 26, 2011

How to use FXCop?

  1. Install FXCop. Setup can be found here: C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\FXCop\FxCopSetup.exe
  2. Go to All Programs > Microsoft FXCop > Microsoft FXCop version
  3. Create a new project. File >  New project
  4. Right click on the project and add .exe as target
  5. Click "Analize" (Project > Analize or F5)
  6. The result will be displayed in Active window


Tuesday, September 20, 2011

Join Hints in SQL: Invade the query optimizer!

You can use join hints to tell the query optimizer how you want join operation to be performed. Depending on your preference, it will come up with an execution plan.

There are four types of join hints, namely LOOP, HASH, MERGE and REMOTE.

You may need to decide which join hint would optimize your query based on the type and size of data you are processing.

How ever, it is advisible not to use this feature frequently, unless you have a significant performance improvement, as SQL Server tries it's best to use the best possible join hint by default, mostly it is LOOP.

I have given some execution plans related to different Join hint types below:

SQL Joins @ a glance!

Friday, September 16, 2011

When we should use REF keyword to pass a reference type argument?

If a value type (ex: int) is passed to a method, by default a copy of the value is being passed as argument and any change applied to that variable will not be applied to the original variable. If you want those changes to be there in original variable, you need to pass the argument using REF keyword.

Any programmer would be familiar with this scenario.

Do we need to pass a reference type argument to a method using REF keyword?

It depends on the type of change you are willing to do in your method.

If we only want to change the state of the object the REF keyword is not required.
But if you want to assign a new value to the variable in method and if it should represent in original variable, you should use REF keyword.

Why?

In C#, everytihng is passed by value. Even if it is a reference type, copy of the reference is passed to the variable by default. However, if you use REF keyword, the original reference will be passed to the method, thus representing all the changes you do in the method variable in the original variable.

Example:

public class Student
      int ID {get; set;}

Student stud1 = new Student(1);

// Assign a new object (Stud2) with ID = 2 - assign object to a new object

changeObjectByVal ( stud1);  // stud1 ID value remains as 1
changeObjectByRef ( ref stud1);  // stud1 ID value will be 2

// Change ID value to 3 - changing the object's state

changeObjectAttrByVal (stud1);  //  stud1 ID value will be 3
changeObjectAttrByRef (stud1);  //  stud1 ID value will be 3

Wednesday, September 14, 2011

Case insensitive string comparison

What will happen if you compare following two strings using equality operator (==)?

string a = "jayani";
string b = "Jayani"

a == b - False
It will say that two strings are not the same.

But what if you want to compare them in a case insensitive manner?
You can use String.Compare method for that.

String.Compare(a, b, true); // This will return zero which indicates that strings are similar.

Third parameter asks to do a case insensitive comparison.

Tuesday, September 13, 2011

My first day with LINQ! :)

Today I got to know about LINQ - Language Integration Query, in the same way Marie Curie found Radium (I think I can remember the correct story..May be not her!)

I had two arrays in my hand and I wanted to compare them and see whether they are equel. But I can't use the object's Equels method as it compares the memory location rather than  the value as Array is a reference type.

I could easily write a for-loop to achieve this but I wanted something new other than the old-fashioned for-loop...

There, I found LINQ!!! (Well.. I know I did not invent that, but Radium is also something that already existed rite? :P)

I could use LINQ join to achieve this. Here's the query I used:

private static bool CompareArrayOrder(string[] arr1, string[] arr2){

var q = from a in arr1
join b in arr2 on a equals b
select a;
return(arr1.Length == arr2.Length && q.Count() == arr1.Length);}

LINQ is something that you can map data with your objects such as arrays.

So, why use LINQ instead of for loop?

For the time being, what I know is, it ROCKS and COOLER than using for loops!
Other than that it makes your code simpler and less cumbersome thus making it easier to read.

Does StringBuilder always better than String in string concatenation?

No.

Why?

Use StringBuilder if you are creating a long string using a loop, when the number of string are high.
Do not use StringBuilder for small number of strings becuase, it makes the code less manageable and StringBuilder contains more logic than string, when it comes to processing the string.

Monday, September 12, 2011

Avoid using unnecessary objects

Why?
  • It is NEVER free.
  • To avoid unnecessary memory allocation
  • Avoid frequent garbage collection

Thursday, September 8, 2011

Circular dependancies in Visual Studio

If you add project A as a reference in project B, you can't add project B as a reference in project A if both projects are in same solution.
Because, Visual Studio explicitly disable circular dependancies among projects in a solution.


Error you get when you create a circular dependancy in a Visual Studio solution

It is a bad practice to allow circular dependancies between .dlls, because in the above scenario, if you change A you need to rebuild B and since B is changed you need to rebuild A and vise versa, thus creating an infinite loop which can cause a memory leak in the server.

Template parameters in SSMS 2008

You can add templates to varios features in SQL such as stored procedures, tables etc.

Ex:

If you want to replace the values which actual values,
  1. Go to Query in menu bar
  2. Select Specify Values for Template Parameters (short cut keys: ctrl+shift+m)
  3. The available parameters for the template will be displayed.
  4. Specify values in Value column
  5. Click OK.

    
    Specify values for template parameters in SSMS 2008
    

Why Trim() function ignore specified characters?

Trim() function in String will trim the preceding and trailing characters of a given character array until it finds a character which is not there in the given array.

Example:
char[] t = new char[] { 1, 2, 3}

trim("12external319test23");

result is "external319test".

So, if it is posible to have spaces, commas etc. as leading or trailing characters in addition to the defined character set, add them also to the character array, before you call Trim() function.

Tuesday, September 6, 2011

Expressions and precedence in AST

When it comes to expressions, the higher priority items are being called by lower priotrity items. So, higher priority items reside in leaf level of the AST and they get executed first. Once all the sub trees get executed, parent trees will execute. First, ANTLR will look for the lowest priority item, which is in the top level and then it executes the higher priority item which is down the line, the following a top-down parsing strategy.

Lexer rules

Lexer will generate a collection of meaningful tokens using a mere charater stream.

Following statement represents a single line comment.

COMMENT : '--' (~('\r' '\n'))* NLfrag { $channel = HIDDEN; } ;

fragment NLfrag : ('\r' '\n'? '\n') ;  
  • COMMENT - An individual token
  •    '--' - Starting point of the single line comment - a single Match() statement
  •    (~('\r' '\n'))* - Comment can be followed by zero of more times of new line characters, tab characters etc. - A loop
  •    $channel = HIDDEN - Do not show this token to parser! -
  • NLfrag - Calls another token - recursively calls mNLfrag() function
  • '\n'? - Newline is optional

How to change the compatibility level of a database

If any feature is not supported in your current database compatibility level, you will get a syntax error saying you can change it in SSMS.
  1. In SSMS 2008, go to the database in object explorer under Databases folder.
  2. Right click the database you want to change the compatibility level.
  3. In Properties, Options select the option you want in Compatibility level drop down.

Sunday, September 4, 2011

Difference between UNION and UNION ALL

Both UNION and UNION ALL statements can be used in SELECT clause to retrieve a set of records which is in a given column in both tables.

For example, consider the following example:

















Here, you can see that there are two tables with a column which is of same data type, StudentName and EmployeeName.

Say we want to find out both the students and employees in this organization, we can use UNION clause and it will return a distinct set of student and employees. If a person is a student as well as an employee, his/her name will appear only once.

But if you use UNION ALL, that person's name will appear twice.

See the results below:

Thursday, September 1, 2011

Difference between COUNT and COUNT_BIG

COUNT will return an int value where as COUNT_BIG will return a big int value.

Difference between COUNT(*) and COUNT(1)

COUNT (*) will return rows related to all the columns with null values. COUNT(1) will return the row count of the first column without null values.