Leaking Implementation Details Through Public APIs
You'll learn how exposing internal structure in your public API breaks encapsulation and tightly couples consumers to your implementation.
What This Means
When you design an object or module, you create a boundary between what it does (its public interface) and how it does it (its internal implementation). Leaking implementation details means your public API reveals or depends on internal structures, forcing users to know and care about things they shouldn't.
This creates tight coupling: when you change how something works internally, all the code using your API breaks.
Real-World Analogy
Think of a coffee machine. A good API is pressing "brew." A leaky API would require you to know the exact pump pressure, bean grinder RPM, and water temperature timing. If the manufacturer upgrades the internal pump, your usage instructions break.
Common Ways This Happens
Returning internal data structures directly:
class UserManager {
#users = [];
// BAD: exposes internal array
getUsers() {
return this.#users; // Callers can mutate this!
}
}
Requiring knowledge of internal implementation:
// BAD: API forces you to know about internal _cache property
userManager._cache.forEach(user => console.log(user.name));
// GOOD: API hides internal details
userManager.forEach(user => console.log(user.name));
Exposing internal class instances in the API: If your public methods return objects that reference internal dependencies, changing those dependencies breaks all consumers.
Why It Matters
When you leak implementation details, you lose the freedom to refactor. Every internal change becomes a breaking change for everyone using your code.
Key Takeaway: Design your public API to expose capabilities, not structures. Hide internal details so you can change how things work without breaking code that depends on what they do.