Open/closed principle

Previously, we had a look at the single responsibility principle. Hand in hand with this is the open/closed principle.

Bertrand Meyer stated that software entities (classes, modules, functions, and so on):

  • Should be open for extension
  • Should be closed for modification

What exactly does this mean? Let's take the PlayerStatistics class as an example. Inside this class, you know that we have a method to calculate the strike rate of a particular player. This is included in the class because it inherits from the Statistics abstract class. That is correct, but the fact that the CalculateStrikeRate(Player player) method caters for two player types (all-rounders and batsmen) is already a hint of a problem.

Let's assume that ...

Get C# 7 and .NET Core 2.0 Blueprints now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.