โ† Back to list

(O)pen/Closed Principle: Add Features Without Fear ๐Ÿšช

๐Ÿ’ญ Ever updated a class for a โ€œtiny changeโ€โ€ฆ and suddenly broke five unrelated features?

Musa Ndlala ยท 2025-08-29 08:47 ยท 9 claps ยท 2.3 min read paywalled
#salesforce-productivity #salesforce-development #salesforce-solid #salesforce-apex #salesforceklever
Open on Medium โ†—
Wiki topics: CRM ยท Email & CRM โฑ๏ธ ยท Productivity

(O)pen/Closed Principle: Add Features Without Fear ๐Ÿšช

๐Ÿ’ญ Ever updated a class for a โ€œtiny changeโ€โ€ฆ and suddenly broke five unrelated features?

Author Created Image

Author Created Image

Yeah, thatโ€™s why we need the Open/Closed Principle (OCP) โ€” the โ€œOโ€ in SOLID. No more drowning in if statements. Lets not even speak of the else if statement(How to nuke the classes run time with one line of code).

๐Ÿง  My Learning Strategy: Stay Curious, Stay Sharp

What is OCP? When Would You Use OCP? Why Would You Use OCP? Where Would You Use OCP? Who Benefits? How Would You Use OCP?

What is OCP?

Classes, modules, and functions should be open for extension but closed for modification.

Translation? Once a piece of code is written and tested, stop poking and prodding it. Instead, design it so you can add new functionality without changing existing code.

When Would You Use OCP?

  • When business rules change more often than Salesforce releases.
  • When adding a new feature currently means opening up old classes and hoping for the best.
  • When your project is large enough that one tiny change could ripple across multiple modules.

Why Would You Use OCP?

  • Prevents Regression Bugs โ€” If itโ€™s not edited, it canโ€™t break.
  • Improves Maintainability โ€” New features live in new classes, away from proven code.
  • Encourages Reusable Design Patterns โ€” Strategy, Factory, and Plug-in patterns love OCP.
  • Speeds Up Development โ€” No more digging through legacy code to cram in new logic.

Where Would You Use OCP?

  • Apex Services โ€” Pricing engines, discount calculators, tax logic.
  • Trigger Frameworks โ€” Add a new handler for new logic, no rewrites.
  • Validation & Workflow Engines โ€” New rules? Plug โ€™em in.
  • Lightning Web Components โ€” Config-driven or slot-based behavior extensions.

Who Benefits?

  • Developers โ†’ Less risk when adding features.
  • Testers โ†’ Only new code needs new tests.
  • Businesses โ†’ Faster delivery with fewer nasty surprises.

How Would You Use OCP?

Interface + Strategy Pattern Example

public interface DiscountStrategy {
    Decimal applyDiscount(Decimal amount);
}

// Existing Strategy
public class PercentageDiscount implements DiscountStrategy {
    public Decimal applyDiscount(Decimal amount) {
        return amount * 0.9; // 10% discount
    }
}

// New Strategy โ€“ No edits to old code
public class FixedAmountDiscount implements DiscountStrategy {
    public Decimal applyDiscount(Decimal amount) {
        return amount - 50; // R50 off
    }
}

public class DiscountService {
    private DiscountStrategy strategy;

    public DiscountService(DiscountStrategy strategy) {
        this.strategy = strategy;
    }

    public Decimal calculateFinalAmount(Decimal amount) {
        return strategy.applyDiscount(amount);
    }
}

Usage

DiscountService service = new DiscountService(new PercentageDiscount());
System.debug(service.calculateFinalAmount(1000)); // 900

service = new DiscountService(new FixedAmountDiscount());
System.debug(service.calculateFinalAmount(1000)); // 950

Gotchas โš ๏ธ

  • โ€œBut this means more classes!โ€ Correct. But clean, focused classes beat spaghetti code every time.
  • Over-Engineering Alert ๐Ÿšจ Not every method needs to be an extension point. Start with areas that change often.

๐Ÿ’ญ Final Thought

OCP keeps your code calm when the business isnโ€™t. Add features, not bugs. Extend, donโ€™t edit. Thatโ€™s the #SalesforceKleverโ„ข way. ๐Ÿš€

Side note: If you would like me to write up a demonstrates on how the else if is not your friend, my friend. Say the word, as in actually type the word=>โ€˜wordโ€™.

Author Created Image

Author Created Image


๋ฉ”ํƒ€๋ฐ์ดํ„ฐ
post_id
aa1cf76987cb
slug
o-pen-closed-principle-add-features-without-fear-aa1cf76987cb
url
https://medium.com/@kmniroi/o-pen-closed-principle-add-features-without-fear-aa1cf76987cb
canonical_url
https://medium.com/@kmniroi/o-pen-closed-principle-add-features-without-fear-aa1cf76987cb
author_url
https://medium.com/@kmniroi
status
ok
fetched_at
2026-07-09 08:02:55