[swift-evolution] [Review] Require self for accessing instance members

Jeremy Pereira jeremy.j.pereira at googlemail.com
Thu Dec 17 05:54:36 CST 2015


> On 16 Dec 2015, at 21:18, Bill Burgar via swift-evolution <swift-evolution at swift.org> wrote:
> 
> 
> The arguments in favour of removing self that claim to make code more readable (I think) centre on the idea that there are fewer characters on the line


I don’t think this is the case. Consider the following:

import Darwin

struct Quadratic
{
    let a: Double
    let b: Double
    let c: Double

    func rootsNoSelf() -> (Double, Double)?
    {
        let discriminant = b * b - 4 * a * c

        guard discriminant >= 0 else { return nil }
        let discRoot = sqrt(discriminant)
	return ((-b + discRoot) / (2 * a), (-b - discRoot) / (2 * a))
    }

    func rootsSelf() -> (Double, Double)?
    {
        let discriminant = self.b * self.b - 4 * self.a * self.c

        guard discriminant >= 0 else { return nil }
        let discRoot = sqrt(discriminant)
        return ((-self.b + discRoot) / (2 * self.a), (-self.b - discRoot) / (2 * self.a))
    }
}

I think the former function is easier to read. It’s instantly recognisable to anybody who has a passing acquaintance with The Formula. The second function, slightly less so. The constants get a bit lost with all the “self”s around and I don’t think it has anything to do with line length.


More information about the swift-evolution mailing list