Align Your Code With Align Assignments Visual Studio Extension

In most of the product based projects it is said that variable assignment in the project must be aligned properly. It increases the readability of code and any other developer can also understand the code easily. Align assignment also reduces the bug in project due to better readability. Let’s try to understand it with an example:

We have code like:

  1. System.Collections.Generic.SortedList < intstring > 
  2. sortedList1 = new SortedList < intstring > ();  
  3. System.Text.StringBuilder sb1 = new StringBuilder();  
  4. int i = 20;  
  5. string myCommonUsableString = string.Empty;  
  6. System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection();  
  7. bool isFileProcessingHasBeenCompleted = false;  
  8. double amount = 20000.0;  
  9. Dictionary < stringstring > 
  10. myDict = new Dictionary < stringstring > (); 

 
 
 
 
And we need that the code assignment must be properly aligned like the following code:
  1.             System.Collections.Generic.SortedList<intstring> sortedList1 = new SortedList<intstring>();  
  2.             System.Text.StringBuilder sb1                                  = new StringBuilder();  
  3.             int i                                                          = 20;  
  4.             string myCommonUsableString                                    = string.Empty;  
  5.             System.Data.SqlClient.SqlConnection con                        = new System.Data.SqlClient.SqlConnection();  
  6.             bool isFileProcessingHasBeenCompleted                          = false;  
  7.             double amount                                                  = 20000.0;  
  8.             Dictionary<stringstring> myDict                              = new Dictionary<stringstring>(); 
 
 
 To do this we have 3 options:
  1. Use spacing
  2. Use Tabbing
  3. Use Visual Studio Extension “Align Assignments”.

If spacing is not considered as a good practice for code alignment we can use tabbing. But assigning alignment properly we have to manage tabbing multiple times and it is time taking.

But we can do this in a single key stroke using the Visual Studio Extension “Align Assignments”.

Using “Align Assignments”.

Go to Tools menu, then Extensions and Updates.

Search for Align Assignments. Download and install it.

extensions

In my case it has been already installed that’s why showing green tick mark. As displayed in the screenshot it has a default assigned shortcut Ctrl+Alt+].

To align assignment select all the lines which you want to align and press Ctrl+Alt+] and all the assignment will be aligned properly as displayed in the above code.

Apart from alignment of C# & VB.NET code we can also align the assignment of JavaScript and other types of file using this tool.

Generally in JavaScript we do not align assignment but if we want to do this we can do it as follows:

Unaligned JavaScript Code

  1. var aaaa = 533;  
  2. var bbbbbbbbbbbbbbbbbbb = 336;  
  3. var ccccccc = x + y;  
  4. var price1 = 5;  
  5. var price2 = 6;  
  6. var total = price1 + price2; 
 
 
 
Select all the lines and press Ctrl+Alt+] and it will format code like the following:
  1. var aaaa                = 533;  
  2. var bbbbbbbbbbbbbbbbbbb = 336;  
  3. var ccccccc             = x + y;  
  4. var price1              = 5;  
  5. var price2              = 6;  
  6. var total               = price1 + price2; 


If you want to revert back the aligned code of class later then you can use Ctrl+Shift+D. 

Up Next
    Ebook Download
    View all
    Learn
    View all