Solid principles: Introduction

in #solid6 years ago

s-660x400.jpg

In this lesson, we will discuss what SOLID principles are, why you should apply them, and on what you will practice in the next lessons.

What are the SOLID principles?

The SOLID principles are five basic principles of how to write a clean code so that you can easily develop it, test it and understand it if someone else would use our code or if we would return to our code for, say, half a year.

These principles were invented by Robert Martin (The Prince’s Bob) famous American programmer.

I will just mention these principles in this lesson and briefly discuss each of them, we will process each rule in successive lessons in turn.

Description of SOLID principles

The five basic principles of the Bob’s are:

  1. Single Responsibility principle – this principle says that each class should be responsible for one particular thing.
  2. Open / Closed principle – that is open to extension and closed to modifications.
  3. Liskov Substitution Principle – in other words, this rule says that the inheriting classes must be 100% compatible with all methods and interfaces.
  4. Interface segregation principle – this rule is very simple says not to create interfaces with methods that the class does not use.
  5. Dependency Inversion principle – is a simple and important principle, all dependencies should depend on abstraction and not on a particular type.

Why is it worth know and apply the SOLID principles?

The SOLID principles are worth adhering, you should adhering them and should make the code testable, cleaner, so that it can be easier to understand, if you write the whole code you will not have a problem to understand it, but the problem will be if the programmer will be attached to your code, who has never seen your code. In some projects, all SOLID principles can not be perfectly adhered to, but if you break only one rule or will be adhered all principles it will be succes.

On what we will be practice on?

For the purpose of explaining the SOLID principles, I have prepared a console program that imitates the operation of the store, I will probably add various functionalities in some lessons so that I would explain some principle, because this program does not have interfaces, and some rules just apply to interfaces, so probably in later I will be adding interfaces or other functionalities to the lessons.

    class Products
    {
        public List<string> ProductsInBag { get; set; }
        public int NumberProductsInShop { get; set; }
        public int NumberClientsInShop { get; set; }
        public List<string> TypesProductsInBag { get; set; }
        public int NumberOrders { get; set; }
        public string FirstNameClient { get; set; }
        public string LastNameClient { get; set; }
        public string ZipCodeClient { get; set; }
        public int NumberProductsInBag { get; set; }
        public int NumberDrinksInBag { get; set; }
        public int NumberCandysInBag { get; set; }
        public double PriceAllProductsInBag { get; set; }
 
        public Products(int numberproductsinShop)
        {
            NumberProductsInShop = numberproductsinShop;
        }
 
        public void IfClientWentToShop()
        {
            NumberClientsInShop += 1;
        }
 
        public string AddProduct(double priceproduct, string product, string typeproduct)
        {
            ProductsInBag.Add(product);
            if (TypesProductsInBag.Contains(typeproduct))
            {
                Console.WriteLine("This type of product is already in the cart!");
            }
            else
            {
                TypesProductsInBag.Add(typeproduct);
            }
 
            if (typeproduct == "candys")
            {
                NumberCandysInBag += 1;
            }
            else if (typeproduct=="drink")
            {
                NumberDrinksInBag += 1;
            }
           
            PriceAllProductsInBag += priceproduct;
            return product;
        }
 
        public string GiveOrders(string firstnameclient, string lastnameclient, string zipcodeclient)
        {
            NumberOrders += 1;
            FirstNameClient = firstnameclient;
            LastNameClient = lastnameclient;
            ZipCodeClient = zipcodeclient;
 
            NumberProductsInBag = ProductsInBag.Count;
 
            return "Order placed!";
        }
 
        public double BuyProducts(double mywallet)
        {
            return mywallet - PriceAllProductsInBag;
        }
 
    }

And this is calling the methods of this class in the “Main” function:

static void Main(string[] args)
{
      double MyWallet = 30;
      Products products=new Products(30);
 
      products.ProductsInBag=new List<string>();
      products.TypesProductsInBag = new List<string>();
 
      products.IfClientWentToShop();
      products.AddProduct(3.34, "cola", "drink");
      products.AddProduct(5.13, "sprite", "drink");
      products.AddProduct(1.16, "candy", "candys");
 
      Console.WriteLine(products.GiveOrders("Sławomir", "Kowalski","81-198")+
          "\nThe number of products in the cart: " + products.NumberProductsInBag+
          "\nOrder placed by: " + products.FirstNameClient + " " + products.LastNameClient
         +"\nNumber of drinks in the cart: " + products.NumberDrinksInBag
         +"\nThe number of sweets in the cart: " + products.NumberCandysInBag
         +"\nPrice of all products: " + products.PriceAllProductsInBag);
 
     Console.WriteLine("It's in my wallet: "+products.BuyProducts(MyWallet)+" PLN");
 
     Console.ReadKey();
}

As you can see, the class and its calling looks like one word “terribly !!!”.

We will refactorize this class according to the SOLID principles, if someone continues to work on this program, it would be suicide in advance, we must change it so that it can be easily modified, tested, expanded, etc. how to do it? We will get to that in the next lessons.

This content also you can find on my blog http://devman.pl/programtech/solid-principles-introduction/

If you recognise it as useful, share it with others so that others can also use it.

Leave upvote and follow and wait for next articles :) .

Coin Marketplace

STEEM 0.32
TRX 0.11
JST 0.034
BTC 66004.40
ETH 3243.40
USDT 1.00
SBD 4.19