Function Body Length

Function bodies should not span too many lines

  • Identifier: function_body_length
  • Enabled by default: Yes
  • Supports autocorrection: No
  • Kind: metrics
  • Analyzer rule: No
  • Minimum Swift compiler version: 5.0.0
  • Default configuration:
    KeyValue
    warning 50
    error 100

Non Triggering Examples

//
// warning: 2
//

func f() {}

//
// warning: 2
//

func f() {
    let x = 0
}

//
// warning: 2
//

func f() {
    let x = 0
    let y = 1
}

//
// warning: 2
//

func f() {
    let x = 0
    // comments
    // will
    // be
    // ignored
}

//
// warning: 2
//

    func f() {
        let x = 0
        // empty lines will be ignored


    }

Triggering Examples

//
// warning: 2
//

func f() {
    let x = 0
    let y = 1
    let z = 2
}

//
// warning: 2
//

class C {
    deinit {
        let x = 0
        let y = 1
        let z = 2
    }
}

//
// warning: 2
//

class C {
    init() {
        let x = 0
        let y = 1
        let z = 2
    }
}

//
// warning: 2
//

class C {
    subscript() -> Int {
        let x = 0
        let y = 1
        return x + y
    }
}

//
// warning: 2
//

struct S {
    subscript() -> Int {
        get {
            let x = 0
            let y = 1
            return x + y
        }
        set {
            let x = 0
            let y = 1
            let z = 2
        }
        willSet {
            let x = 0
            let y = 1
            let z = 2
        }
    }
}