Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DI aproach for class constants. #23

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
let indianaPi: Int

init(buttonPadding: CGFloat = 20.0,
indianaPi: Int = 3) {
self.buttonPadding = buttonPadding
self.indianaPi = indianaPi
}
}

class MyClassName {
private let options MyClassNameOptions

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be 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
Expand Down