GitHub copilot — Part 1
I still remember I used a notepad/text pad to learn the java programming languages during my college years. The IDE market evolved so much and Microsoft Visual studio and Visual studio code used by developers. To add more flexibility and to improve the programmer productivity the Microsoft introduced AI pair programming concept through the GitHub copilot. I tried few scenarios and working pretty well. I installed copilot extension on visual studio and generate the code based on the comments.
Install the copilot by following this link
Getting started with GitHub Copilot in Visual Studio — GitHub Docs
For example, I tried my comment with comparing two objects.
// compare two objects
var a = new { Name = "John", Age = 30 };
var b = new { Name = "John", Age = 30 };
Console.WriteLine(a == b); // false
Console.WriteLine(b == a);
Console.WriteLine(a == b);
Console.WriteLine(a.Equals(b)); // true
Console.WriteLine(b.Equals(a));
Console.WriteLine(a.Equals(b));
The second example is also pretty simple. The code is generated by the copilot extension.
// generate prime numbers
var primes = Enumerable.Range(2, 1000).Where(n => Enumerable.Range(2, (int)Math.Sqrt(n) - 1).All(i => n % i > 0));
Console.WriteLine(string.Join(", ", primes));
Console.WriteLine();
Console.ReadLine();
Console.WriteLine();
The third one i tried with different scenario. read a message from azure service bus.
var connectionString = "Endpoint=sb://test.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
var queueName = "test";
var queue = new Queue<int>();
var client = new QueueClient(connectionString, queueName);
client.RegisterMessageHandler(async (message, token) =>
{
var body = Encoding.UTF8.GetString(message.Body);
Console.WriteLine(body);
await client.CompleteAsync(message.SystemProperties.LockToken);
}, new MessageHandlerOptions(ExceptionReceivedHandler) { MaxConcurrentCalls = 1, AutoComplete = false });
The fourth one i tried with Azure SQL
// Insert order record into azure sql database
var connectionString = "Server=tcp:server.database.windows.net,1433;Initial Catalog=database;Persist Security Info=False;User ID=user;Password=password;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;";
var sql = "INSERT INTO [dbo].[Order] ([Id], [Name], [Price]) VALUES (@Id, @Name, @Price)";
var order = new Order { Id = Guid.NewGuid(), Name = "Order 1", Price = 100 };
using (var connection = new SqlConnection(connectionString))
{
connection.Open();
using (var command = new SqlCommand(sql, connection))
{
command.Parameters.AddWithValue("@Id", order.Id);
command.Parameters.AddWithValue("@Name", order.Name);
command.Parameters.AddWithValue("@Price", order.Price);
command.ExecuteNonQuery();
}
}
The fifth one i tried with Entity Framework
var employees = new List<Employee>();
using (var context = new EmployeeContext())
{
employees = context.Employees.ToList();
}
The sixth one i tried to insert the record to azure data explorer.
var client = new Kusto.Data.Net.Client.KustoClient("https://help.kusto.windows.net");
var database = "Samples";
var table = "SampleTable";
var query = $"insert into {table} (Name, Age) values ('John', 30)";
var result = client.ExecuteControlCommand(database, query);
Console.WriteLine(result);
Console.WriteLine();
Console.ReadLine();
The seventh one i tried with real time scenario, create one interfaces and implement the interface.
// create generic interface for the Order Management
public interface IOrderManagement<T> where T : Order
{
void Add(T order);
void Remove(T order);
void Update(T order);
T Get(int id);
IEnumerable<T> GetAll();
}
// create implementation for interace IOrderManagement
public class OrderManagement<T> : IOrderManagement<T> where T : Order
{
private readonly List<T> _orders = new List<T>();
public void Add(T order)
{
_orders.Add(order);
}
public void Remove(T order)
{
_orders.Remove(order);
}
public void Update(T order)
{
var index = _orders.FindIndex(o => o.Id == order.Id);
_orders[index] = order;
}
public T Get(int id)
{
return _orders.FirstOrDefault(o => o.Id == id);
}
public IEnumerable<T> GetAll()
{
return _orders;
}
}
The code generation is pretty cool. Not only that you can ask question through the comments and get you the specific answer.
The last one i tried to generate the unit test for the order class.
// create unit test case for order
public class OrderTest
{
public void TestOrder()
{
// create new order
Order order = new Order();
// set order properties
order.OrderID = 1;
order.OrderName = "Order 1";
order.OrderDescription = "Order 1 Description";
order.OrderQuantity = 1;
order.OrderPrice = 100;
order.OrderTotal = 100;
order.OrderDate = "01/01/2019";
order.OrderStatus = "Order Status";
order.OrderPayment = "Order Payment";
order.OrderDelivery = "Order Delivery";
order.OrderDeliveryDate = "01/01/2019";
order.OrderDeliveryTime = "01:00";
order.OrderDeliveryAddress = "Order Delivery Address";
order.OrderDeliveryCity = "Order Delivery City";
order.OrderDeliveryState = "Order Delivery State";
order.OrderDeliveryZip = "Order Delivery Zip";
order.OrderDeliveryCountry = "Order Delivery Country";
order.OrderDeliveryPhone = "Order Delivery Phone";
order.OrderDeliveryEmail = "Order Delivery Email";
order.OrderDeliveryNotes = "Order Delivery Notes";
order.OrderDeliveryTracking = "Order Delivery Tracking";
order.OrderDeliveryTrackingURL = "Order Delivery Tracking URL";
order.OrderDeliveryTrackingNotes = "Order Delivery Tracking Notes";
order.OrderDeliveryTrackingDate = "01/01/2019";
order.OrderDeliveryTrackingTime = "01:00";
order.OrderDeliveryTrackingStatus = "Order Delivery Tracking Status";
order.OrderDeliveryTrackingStatusNotes = "Order Delivery Tracking Status Notes";
order.OrderDeliveryTrackingStatusDate = "01/01/2019";
order.OrderDeliveryTrackingStatusTime = "01:00";
order.OrderDeliveryTrackingStatusLocation = "Order Delivery Tracking Status Location";
order.OrderDeliveryTrackingStatusLocationNotes = "Order Delivery Tracking Status Location Notes";
order.OrderDeliveryTrackingStatusLocationDate = "01/01/2019";
order.OrderDeliveryTrackingStatusLocationTime = "01:00";
order.OrderDeliveryTrackingStatusLocationCity = "Order Delivery Tracking Status Location City";
order.OrderDeliveryTrackingStatusLocationState = "Order Delivery Tracking Status Location State";
order.OrderDeliveryTrackingStatusLocationZip = "Order Delivery Tracking Status Location Zip";
order.OrderDeliveryTrackingStatusLocationCountry = "Order Delivery Tracking Status Location Country";
order.OrderDeliveryTrackingStatus
}
In the next series, we will see to how to optimize the existing code and debugging the code through the copilot.