Function rewritePacket
has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring. Open
public func rewritePacket(packet: IPMutablePacket) -> IPMutablePacket? {
// Support only TCP as for now
guard packet.proto == .TCP else {
return nil
}
- 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 rewritePacket
has 27 lines of code (exceeds 25 allowed). Consider refactoring. Open
public func rewritePacket(packet: IPMutablePacket) -> IPMutablePacket? {
// Support only TCP as for now
guard packet.proto == .TCP else {
return nil
}
Function readAndProcessPackets
has 26 lines of code (exceeds 25 allowed). Consider refactoring. Open
func readAndProcessPackets() {
NetworkInterface.TunnelProvider.packetFlow.readPacketsWithCompletionHandler() {
var outputPackets = [IPMutablePacket]()
let packets = $0.0.map { data in
IPMutablePacket(payload: data)
Function readAndProcessPackets
has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring. Open
func readAndProcessPackets() {
NetworkInterface.TunnelProvider.packetFlow.readPacketsWithCompletionHandler() {
var outputPackets = [IPMutablePacket]()
let packets = $0.0.map { data in
IPMutablePacket(payload: data)
- 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
Avoid too many return
statements within this function. Open
return packet
Avoid too many return
statements within this function. Open
return nil
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
}
Empty parentheses following method call with trailing closure argument should be removed Open
NetworkInterface.TunnelProvider.packetFlow.readPacketsWithCompletionHandler() {
- Read upRead up
- Exclude checks
redundant-parentheses
Control flow constructs (if
, else if
, switch
, for
, while
, repeat-while
, and guard
statements), Exception handling constructs (throw
, and do/catch
statements), and Initializers (array
, dictionary
, initializer patterns
) should not be enclosed in parentheses.
Additionally, method calls with no parameters and a trailing closure should not have empty parentheses following the method name.
Control flow constructs
- if, else if statement
Preferred
if SomeCondition {
} else if SomeOtherCondition {
}
Not Preferred
if (SomeCondition) {
} else if (SomeOtherCondition) {
}
- switch statement
Preferred
switch SomeData {
default:
break
}
Not Preferred
switch (SomeData) {
default:
break
}
- for loop
Preferred
for var i = 0; i < 10; i+=1 {
}
Not Preferred
for (var i = 0; i < 10; i+=1) {
}
- while loop
Preferred
while SomeCondition {
}
Not Preferred
while (SomeCondition) {
}
- repeat-while loop
Preferred
repeat {
} while SomeCondition
Not Preferred
repeat {
} while (SomeCondition)
- guard clause
Preferred
guard true else { }
Not Preferred
guard (true) else { }
Exception handling constructs
- do/catch statement
Preferred
do {
} catch SomeException {
}
Not Preferred
do {
} catch (SomeException) {
}
- throw statement
Preferred
throw SomeException
Not Preferred
throw (SomeException)
Initializers
- array items
Preferred
var shoppingList: [String] = ["Eggs", "Milk"]
Not Preferred
var shoppingList: [String] = [("Eggs"), ("Milk")]
- dictionary items
Preferred
var airports: [String: String] = ["YYZ": "Toronto Pearson", "DUB": "Dublin"]
Not Preferred
var airports: [String: String] = [("YYZ"): ("Toronto Pearson"), ("DUB"): ("Dublin")]
- initializer patterns
Preferred
var x: Int = 2
var y: String = "Sleekbyte"
var x = 2
Not Preferred
var x: Int = (2)
var y: String = ("Sleekbyte")
var x = (2)
Method calls
Preferred
items.map {
item in item.transform()
}
Not Preferred
items.map() {
item in item.transform()
}