Social Icons

Pages

Sunday, July 14, 2019

What is Good Code?


Below are some of the key techniques for writing a good piece of code I have learnt over the years:

The code written should be organized thereby making it easy to read and understand. Following the standard Naming conventions and using sufficient naming helps in the readability and use of comments to explain the core complex logic to help the reader understand the program intentions.

Reviewing the unused code sections and removing the identified unnecessary code blocks thereby, reducing the both the program size and confusion that may develop to a new developer of the system.

Modularity is one key to be followed to achieve at most code standard. Long code blocks inside a function should be broken into smaller generic modules. By doing so, Code Reusability can be achieved, and code size can be greatly reduced.

Optimized code is the key to achieve the desired result without compromising the performance of the system.
For Example, Getting the factorial of a number can be done in many ways:
    a. Recursion - by using the recursive formula
    b. Operator - by using the Ternary operator

Before jumping into choosing the algorithm to follow, all the complexities (i.e., Time complexity, Space complexity) are analysed and the solution is chosen based on the given requirement.

Code written should be easy to maintain and changes to the system should not be a challenge. 
Dependency on an individual should be minimized and hence, documenting all the key workings of the program is a must.
For Example, for a new API method written documenting the following is a good practice:
   a. Purpose of the method
   b. Inputs
   c.Output

Good code is something that strikes the right balance between all the qualities mentioned above.


Monday, January 14, 2019

Write Cleaner Code C#


Some of the C# features that will help you write cleaner code

1. Initialize Constructor

Common way
  class BLWriteCleanerCode
    {
        public DateTime CreatedDate { get;   }
        public List<Employee> Employees { get; }

        public  BLWriteCleanerCode()
        {
            CreatedDate = DateTime.Now;
            Employees = new List<Employee>();
        }
    }

With C# 6, you can initialize the property directly
    class BLWriteCleanerCode
    {
        public DateTime CreatedDate { get; } = DateTime.Now;
        public List<Employee> Employees { get; } = new List<Employee>();

    }

2. Create Dictionary
Common way
var employees = new Dictionary<intstring>
            {
                 { 1, "John" },
                 { 2, "Mathy" }
            };

New way
      var employees = new Dictionary<intstring>
            {
                [1] = "John",
                [2] = "Mathy"
            };

3. Using String

Common way
        var emploueeData = String.Format("{0} ({1})", emp.EmployeeID, emp.EmployeeName);

New way
var emploueeData = $"{emp.EmployeeID}{emp.EmployeeName}";

4. Using Null-conditional Operator

Common way
        Employee emp;
        List<Employee> empCollection = new List<Employee>();
        if (empCollection != null)
            emp = empCollection[0];

New way
           emp = empCollection?[0];

Common way
        if (empCollection[0] != null)
           {
               if (empCollection[0].Address != null)
               {
                    address = empCollection[0].Address[0].Address1;
               }
           }
New way

address = empCollection[0]? .Address[0]?.Address1??"" ;