From 62ad89a5fadd7238b48dcbbb495ff725ec31e352 Mon Sep 17 00:00:00 2001 From: Alexander Sivura Date: Fri, 30 Mar 2018 15:15:10 -0700 Subject: [PATCH 1/2] DI aproach for class constants. --- README.md | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index aaf1f62..7a5fb80 100644 --- a/README.md +++ b/README.md @@ -187,15 +187,36 @@ class URLFinder { } ``` -* **2.5** All constants that are instance-independent should be `static`. All such `static` constants should be placed in a marked section of their `class`, `struct`, or `enum`. For classes with many constants, you should group constants that have similar or the same prefixes, suffixes and/or use cases. +* **2.5** Use configuration classes instead of class constants and pass them as constructor dependency injection. All properties of configuration classes must be immutable and have default values in constructor. It helps to reuse classes without code modification. +For configuration classes with many constants, you should group constants that have similar or the same prefixes, suffixes and/or use cases as inline classes. ```swift -// PREFERRED + +// PREFERRED +class MyClassNameOptions { + let buttonPadding: CGFloat = 20.0 + let indianaPi: Int + + init(buttonPadding: CGFloat = 20.0, + indianaPi: Int = 3) { + self.buttonPadding = buttonPadding + self.indianaPi = indianaPi + } +} + +class MyClassName { + private let options MyClassNameOptions + init(options: MyClassNameOptions = MyClassNameOptions()) { + self.options = options + } +} + + +// NOT PREFERRED class MyClassName { // MARK: - Constants static let buttonPadding: CGFloat = 20.0 static let indianaPi = 3 - static let shared = MyClassName() } // NOT PREFERRED From 86a6e56eba6090ad70410519e98a3e698a07af54 Mon Sep 17 00:00:00 2001 From: Alexander Sivura Date: Fri, 30 Mar 2018 15:26:02 -0700 Subject: [PATCH 2/2] Fix typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7a5fb80..21d82e0 100644 --- a/README.md +++ b/README.md @@ -194,7 +194,7 @@ For configuration classes with many constants, you should group constants that h // PREFERRED class MyClassNameOptions { - let buttonPadding: CGFloat = 20.0 + let buttonPadding: CGFloat let indianaPi: Int init(buttonPadding: CGFloat = 20.0,