39 lines
1.1 KiB
Plaintext
39 lines
1.1 KiB
Plaintext
![]() |
include::../description.adoc[]
|
||
|
|
||
|
== Noncompliant Code Example
|
||
|
|
||
|
----
|
||
|
public class Foo {
|
||
|
public func listUsers() -> [User] {
|
||
|
var users:[User]
|
||
|
let location = "/home/mylogin/Dev/users.txt" // Non-Compliant
|
||
|
let fileContent = NSString(contentsOfFile: location, encoding: NSUTF8StringEncoding, error: nil)
|
||
|
users = parse(fileContent!)
|
||
|
return users
|
||
|
}
|
||
|
}
|
||
|
----
|
||
|
|
||
|
== Compliant Solution
|
||
|
|
||
|
----
|
||
|
public class Foo {
|
||
|
// Configuration is a class that returns customizable properties: it can be mocked to be injected during tests.
|
||
|
private var config:Configuration
|
||
|
public init(myConfig:Configuration) {
|
||
|
config = myConfig
|
||
|
}
|
||
|
public func listUsers() -> [User] {
|
||
|
var users:[User]
|
||
|
// Find here the way to get the correct folder, in this case using the Configuration object
|
||
|
let location = config.getProperty("myApplication.listingFile")
|
||
|
// and use this parameter instead of the hard coded path
|
||
|
let fileContent = NSString(contentsOfFile: location, encoding: NSUTF8StringEncoding, error: nil)
|
||
|
users = parse(fileContent!)
|
||
|
return users
|
||
|
}
|
||
|
}
|
||
|
----
|
||
|
|
||
|
include::../see.adoc[]
|