Junior — Middle
Which SOLID principle could be violated when using an optional function within a protocol?
sobes.tech AI
Answer from AI
Using an optional function within a protocol can violate the Interface Segregation Principle (ISP) from SOLID.
ISP states that clients should not depend on methods they do not use. If a protocol has an optional function, objects implementing this protocol may be forced to implement or consider methods they do not need, which violates this principle.
For example, if a protocol contains both mandatory and optional methods, and a class only uses the mandatory ones but still needs to support the optional ones (even with empty implementations), it complicates the design and reduces its clarity.