Which SOLID principle states that a class should have only one reason to change?

Prepare for the FAST Enterprises IC Interview. Enhance your skills with flashcards and multiple-choice questions. Each question provides hints and detailed explanations. Excel in your interview!

Multiple Choice

Which SOLID principle states that a class should have only one reason to change?

Explanation:
The main idea here is to keep a class focused on a single area of change. The Single Responsibility Principle says a class should have only one reason to change, meaning it should encapsulate just one concern or responsibility. When a class handles multiple concerns—like business rules and data persistence—a change in one area can cascade into others, making the code harder to maintain and more prone to bugs. For example, a class that both applies discount logic and saves orders to a database mixes two distinct responsibilities. If the discount rules change, you’d have to modify this class; if the storage mechanism changes, you’d modify it again. Separating these concerns into a discount service and a repository or data-access class keeps each class small and changes isolated, preventing ripple effects. Other SOLID principles address different ideas. Open/Closed is about being open to extending behavior without modifying existing code. Liskov Substitution concerns ensuring derived types can stand in for base types without altering correctness. Interface Segregation focuses on keeping interfaces small and specific so clients only depend on what they actually use. The statement in question aligns most closely with keeping a class to one reason to change, which is why the Single Responsibility Principle is the best fit.

The main idea here is to keep a class focused on a single area of change. The Single Responsibility Principle says a class should have only one reason to change, meaning it should encapsulate just one concern or responsibility. When a class handles multiple concerns—like business rules and data persistence—a change in one area can cascade into others, making the code harder to maintain and more prone to bugs.

For example, a class that both applies discount logic and saves orders to a database mixes two distinct responsibilities. If the discount rules change, you’d have to modify this class; if the storage mechanism changes, you’d modify it again. Separating these concerns into a discount service and a repository or data-access class keeps each class small and changes isolated, preventing ripple effects.

Other SOLID principles address different ideas. Open/Closed is about being open to extending behavior without modifying existing code. Liskov Substitution concerns ensuring derived types can stand in for base types without altering correctness. Interface Segregation focuses on keeping interfaces small and specific so clients only depend on what they actually use. The statement in question aligns most closely with keeping a class to one reason to change, which is why the Single Responsibility Principle is the best fit.

Subscribe

Get the latest from Passetra

You can unsubscribe at any time. Read our privacy policy