samedi 10 mars 2018

Solid OCP violation consequences

Clearing things on the beggining:

  • I understand how to apply OCP to code below (virtual class, DI and so on, there are tons of resources on web about that)

  • I know arguments why I need to apply OCP (3 that mainly occur on web are listed below)

  • I think I am missing the point because I do not have big enough commercial experience.

What I don't understand in OCP are the arguments/consequences (mentioned below) found in all articles regarding OCP

Violating OCP is bad because:

1. You need to write test for old class - it is bad, better to write test for new class.

I do not understand why it is bad?

In both cases I need to write new test. Either for new class or for old class.

2. You need to redeploy application If there are two clients: A (which do not need new functionality) and B (who wants new functionality)

2.1 When I am violating OCP by modifying base class.

client A - do not need to redeploy - because he don't need new functionality

client B - need to redeploy - to get new functionality

2.2 When I am not violating OCP by adding derived class.

client A - do not need to redeploy - because he don't need new functionality

client B - need to redeploy - to get new functionality

I do not understand why I need to redeploy whole application? In both cases it is the same situation- client A do not need to redeploy, client B need to get redeployed application.

3. You can introduce a mistake to the working system But I can also do that when I will put new functionality in it, can't I?

Can you help me understand the meaning of these points above? I was using following example from website

public enum InvoiceType
{
    Final,Proposed
};

public class Invoice
{
    public InvoiceType InvoiceType { get; set; }

    public double GetDiscount(double amount,InvoiceType invoiceType)
    {
        double finalAmount = 0;
        if (invoiceType == InvoiceType.Final)
        {
            finalAmount = amount - 100;
        }
        else if(invoiceType == InvoiceType.Proposed)
        {
            finalAmount = amount - 50;
        }
        return finalAmount;
    }

}

Aucun commentaire:

Enregistrer un commentaire