CSE Roadmap

Core tier · about 3 hours · 7 parts

Part 6 of 7 · 1 min read

The cost

Abstraction is not free.

When not to apply it

HF is direct about the limit. Its answer to "can every part follow OCP" is usually no. Opening a design costs time and effort, and each extra layer of abstraction makes the code harder to read. The advice is to spend that effort where change is most likely.

A practical rule for interviews: wait for the first sign. With one customer type, a plain if is fine. When the second type appears, notice. When the third arrives, refactor to the interface form. Code written for changes that never come is wasted, a point module 7 of this level returns to.

Another guide is to ask the interviewer. If they say that more payment methods may follow later, that is the place to draw an interface. If they say "only cash", keep it simple.

Common mistake

Adding interfaces and factories for every class "in case". This makes small programs hard to read and does not protect against changes that actually happen.

Interview angle

Answer in your head first, then open it.

What is the open-closed principle?

A class should accept new behaviour by adding code, not by editing existing code that works.

Show an example.

A price calculator with a list of rule objects, where a new rule is a new class.

Is it always worth applying?

No. Apply it where change is likely, because every abstraction adds reading cost.

Words you will meet

Word Meaning
Extension Adding new behaviour by writing new code
Modification Editing code that already exists
Switch smell A branching chain that grows with each new kind
Abstraction A name or interface that hides what varies behind it
Rule object A small class that holds one case of a decision