Function finishedDownload
has 26 lines of code (exceeds 25 allowed). Consider refactoring. Open
func finishedDownload() {
var tentativeCachePath: String?
if video.source.isCachable {
tentativeCachePath = VideoCache.cachePath(forVideo: video)
Function connectionDidFinishLoading
has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring. Open
func connectionDidFinishLoading(_ connection: NSURLConnection) {
queue.async { () -> Void in
debugLog("connectionDidFinishLoading")
guard let stream = self.streamForConnection(connection) else {
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Function connection
has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring. Open
func connection(_ connection: NSURLConnection, didReceive response: URLResponse) {
guard let stream = streamForConnection(connection) else {
errorLog("No matching stream for connection: \(connection) with response: \(response)")
return
}
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Force casts should be avoided Open
let httpResponse = response as! HTTPURLResponse
- Read upRead up
- Exclude checks
forced-type-cast
Avoid using the forced form of the type cast operator (as!
) because Swift is not able to determine at compile time if the type conversion will succeed. In the event of an unsuccessful conversion, a runtime error will be triggered. The conditional form of the type cast operator (as?
) is safer and should be used when possible.
Preferred
if let movie = item as? Movie {
print("Movie: '\(movie.name)', dir. \(movie.director)")
}
Not Preferred
let movie = item as! Movie
print("Movie: '\(movie.name)', dir. \(movie.director)")
Identical blocks of code found in 2 locations. Consider refactoring. Open
func startOffsetFromResponse(_ response: URLResponse) -> Int? {
// get range response
var regex: NSRegularExpression!
do {
// Check to see if the server returned a valid byte-range
- Read upRead up
Duplicated Code
Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:
Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.
When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).
Tuning
This issue has a mass of 240.
We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.
The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.
If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.
See codeclimate-duplication
's documentation for more information about tuning the mass threshold in your .codeclimate.yml
.
Refactorings
- Extract Method
- Extract Class
- Form Template Method
- Introduce Null Object
- Pull Up Method
- Pull Up Field
- Substitute Algorithm
Further Reading
- Don't Repeat Yourself on the C2 Wiki
- Duplicated Code on SourceMaking
- Refactoring: Improving the Design of Existing Code by Martin Fowler. Duplicated Code, p76
Similar blocks of code found in 2 locations. Consider refactoring. Open
func connection(_ connection: NSURLConnection, didFailWithError error: Error) {
errorLog("Couldn't download video: \(error.localizedDescription)")
queue.async { () -> Void in
self.failedDownload("Connection fail: \(error.localizedDescription)")
}
- Read upRead up
Duplicated Code
Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:
Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.
When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).
Tuning
This issue has a mass of 52.
We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.
The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.
If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.
See codeclimate-duplication
's documentation for more information about tuning the mass threshold in your .codeclimate.yml
.
Refactorings
- Extract Method
- Extract Class
- Form Template Method
- Introduce Null Object
- Pull Up Method
- Pull Up Field
- Substitute Algorithm
Further Reading
- Don't Repeat Yourself on the C2 Wiki
- Duplicated Code on SourceMaking
- Refactoring: Improving the Design of Existing Code by Martin Fowler. Duplicated Code, p76
Similar blocks of code found in 2 locations. Consider refactoring. Open
func connection(_ connection: NSURLConnection, didReceive challenge: URLAuthenticationChallenge) {
errorLog("Didn't expect authentication challenge while downloading videos!")
queue.async { () -> Void in
self.failedDownload("Connection fail: Received authentication request!")
}
- Read upRead up
Duplicated Code
Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:
Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.
When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).
Tuning
This issue has a mass of 52.
We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.
The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.
If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.
See codeclimate-duplication
's documentation for more information about tuning the mass threshold in your .codeclimate.yml
.
Refactorings
- Extract Method
- Extract Class
- Form Template Method
- Introduce Null Object
- Pull Up Method
- Pull Up Field
- Substitute Algorithm
Further Reading
- Don't Repeat Yourself on the C2 Wiki
- Duplicated Code on SourceMaking
- Refactoring: Improving the Design of Existing Code by Martin Fowler. Duplicated Code, p76
Single-line comment should start with whitespace Open
//print("deinit VideoDownload")
- Read upRead up
- Exclude checks
comment-whitespace
Prefer at least one whitespace character after a comment opening symbol (//
, ///
, /*
, or /**
) and at least one whitespace character before a comment closing symbol (*/
).
Preferred
// This is a comment
/// This is a documentation comment
/* This is a
multi-line comment */
/* This is a
multi-line comment
*/
/** This is a
documentation multi-line
comment
*/
Not Preferred
//This is a comment
///This is a documentation comment
/*This is a
multi-line comment*/
/**This is a multi-line
documentation comment */
Statements should not terminate with a semicolon Open
let pace = 0.2; // pace stream creation a little bit
- Read upRead up
- Exclude checks
terminating-semicolon
Swift does not require a semicolon after each statement in your code unless you wish to combine multiple statements on a single line. Do not write multiple statements on a single line separated with semicolons.
Imports
Preferred
import Foundation
Not Preferred
import Foundation;
Enums and enum cases
Preferred
enum CompassPoint {
case North
case South
case East
case West
}
Not Preferred
enum CompassPoint {
case North;
case South;
case East;
case West;
};
Protocols
Preferred
protocol SomeProtocol {
var SomeMethod: String { get }
func SomeMethod()
func SomeMethod(f: Int)
func SomeMethod(bar: String, baz: Double)
}
Not Preferred
protocol SomeProtocol {
var SomeMethod: String { get };
func SomeMethod();
func SomeMethod(f: Int);
func SomeMethod(bar: String, baz: Double);
};
Extensions
Preferred
extension SomeType {
}
Not Preferred
extension SomeType {
};
Structs
Preferred
struct DemoStruct {
var x: String // variables
}
Not Preferred
struct DemoStruct {
var x: String // variables
};
Classes
Preferred
class SomeClass {
let b = 2 // constants
}
Not Preferred
class SomeClass {
let b = 2 // constants
};
Loops
Preferred
// while loop
while true {
}
// for loop
for ; ; {
}
// repeat while
repeat {
} while true
Not Preferred
// while loop
while true {
};
// for loop
for ; ; {
};
// repeat while
repeat {
} while true;
TODO comments should be formatted either as <todo: description> or <todo description></todo></todo:> Open
let streamCount = 1 // TODO
- Read upRead up
- Exclude checks
todo-syntax
TODO comments should be defined separately using non-nested single line comments. They should adhere to the <TODO: description>
or <TODO(developer-name): description>
syntax. Empty TODO comments will be flagged.
Preferred
// TODO: <insert mandatory todo comment>
// TODO(dev-name): <insert mandatory todo comment></insert></insert>
Not Preferred
// TODO:
/// TODO: Documentation comments should not have TODOs
//// TODO: Nested comments should not have TODOs
// //TODO: Nested comments should not have TODOs
// TODO: Nested comments should not have TODOs // some comment
//// TODO: Nested comments should not have TODOs
Function should have at least one blank line after it Open
}
- Read upRead up
- Exclude checks
function-whitespace
Every function and method declaration should have one blank line before and after itself. An exception to this rule are functions that are declared at the start of a file (only need one blank line after their declaration) or at the end of a file (only need one blank line before their declaration). Comments immediately before a function declaration (no blank lines between them and the function) are considered to be part of the declaration.
Preferred
func function1() {
var text = 1
var text = 2
}
function1()
// a comment
func function2() {
// something goes here
}
struct SomeStruct {
func function3() {
// something goes here
}
func function4() {
// something else goes here
};
}
func function5() {
// something goes here
}
Not Preferred
func function1() {
var text = 1
var text = 2
}
function1()
// a comment
func function2() {
// something goes here
}
struct SomeStruct {
func function3() {
// something goes here
}
func function4() {
// something else goes here
};
}
func function5() {
// something goes here
}