Implicit Getter

ID

swift.implicit_getter

Severity

info

Remediation Complexity

medium

Remediation Risk

low

Remediation Effort

medium

Resource

Code Smell

Language

Swift

Tags

code-smell, redundancy

Description

Reports a read-only computed property that wraps its body in an explicit get {} block. Swift allows the get keyword to be dropped on read-only computed properties, and the bare-brace form is the idiomatic style.

var name: String {       // OK — bare braces
    return _name
}

var name: String {       // FLAW
    get {
        return _name
    }
}

Rationale

The get keyword is mandatory only when a setter is also present (the parser needs to tell the two blocks apart). Without a setter the keyword is pure ceremony.

Remediation

Drop the get keyword and one level of braces:

var name: String {
    return _name
}