Clean Code

  •  Any fool can write code that a computer can understand but good programmers write code that human can understand
  • Easy to read , maintain , extend and change by any developer.
  • Software is intangible -difficult to describe. you can create application in a different ways. The better the code is at beginning , the better it is to modify later and scale your application.
				
					
static bool CompareTwoValue(bool a, bool b)
{
    if (a == b)
    {
        return true;
    }
    else
    {
        return false;
    }
}
				
			
				
					var res = CompareTwoValue(true, true);

static bool CompareTwoValue(bool a, bool b) => a == b;
				
			
  • Your code must express what exactly it does.
  • Good method name, class name, property name etc.
  • Your code must express what exactly it does.
  • Beauty lies in the beholder of eyes….simplicity is in the eye of beholder.
				
					using System.Globalization;

Console.WriteLine(KISS.GetMonth2(3));
Console.WriteLine(KISS.GetMonth1(3));
Console.WriteLine(KISS.GetMonth(3));


public static class KISS
{
    public static string GetMonth(int month)
    {
        switch (month)
        {
            case 1: return "Jan";
            case 2: return "Feb";
            case 3: return "March";
            case 4: return "April";
            case 5: return "May";
            case 6: return "June";
            case 7: return "July";
            case 8: return "Aug";
            case 9: return "Sep";
            case 10: return "Oct";
            case 11: return "Nov";
            case 12: return "Dec";
            default: throw new InvalidOperationException("Kindly enter correct month");
        }
    }

    public static string GetMonth1(int month)
    {
        if (month < 1 || month > 12)
            throw new InvalidOperationException("Enter correct month");
        string[] months = { "Jan", "Feb", "Mar", "Apr", "May", "June", "July" };
        return months[month - 1];
    }
    public static string GetMonth2(int month) => new DateTimeFormatInfo().GetMonthName(month);

}
				
			
  • Every piece of knowledge must have a single , unambiguous, authoritative representation within a system. 
  • Avoid creating unnecessary functionalities
  • Do not try to solve problems that do not exist, just because you think you will need it.
  • S – Single Responsibility Principle
  • O – Open Closed
  • L – Liskov Substitution Principle
  • I – Interface Segregation
  • D – Dependency Inversion
  • Design your types according to their functionality rather than nature.
  • Inheritance makes your codes inflexible to later modifications.
  • Composition – Class references one ore more objects of other classes.
    • Allows you to model has a association between objects.

https://stackoverflow.com/questions/49002/prefer-composition-over-inheritance

  • Separates an application into units.
  • Achieved by using modularization, encapsulation and arrangement in layers.

Leave a Comment