From d14bb924b1b220af23c17b30883baa2fa4fd8e09 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Mon, 18 Sep 2023 23:07:30 +0100 Subject: [PATCH] daily-email: add 2023-09-12 Don't inject too many dependencies --- src/content/daily-email/2023-09-12.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 src/content/daily-email/2023-09-12.md diff --git a/src/content/daily-email/2023-09-12.md b/src/content/daily-email/2023-09-12.md new file mode 100644 index 00000000..9a1c5d1b --- /dev/null +++ b/src/content/daily-email/2023-09-12.md @@ -0,0 +1,22 @@ +--- +title: > + Don't inject too many dependencies +pubDate: 2023-09-12 +permalink: > + archive/2023/09/12/dont-inject-too-many-dependencies +tags: + - software-development + - clean-code +--- + +While dependency injection is a good practice - i.e., passing dependencies into a class, usually via a constructor method - you want to be aware of how many dependencies you inject into each class. + +There's no hard and fast rule, but I usually notice when I get to three dependencies and rarely inject more than four or five into a class. + +Having too many dependencies suggests that the class is doing too much and has too many responsibilities and that another class may be needed. + +## Here's the thing + +Having smaller and simpler classes makes them easier to read, maintain and review. Ideally, each class should only have one responsibility, so it adheres to the Single Responsibility Principle (the "S" in SOLID). + +Creating classes is cheap, so why not split one large and difficult-to-maintain class with many dependencies into multiple smaller and easier-to-work-with ones?