2

I found this example in an online video and I can't really grasp its importance and what is happening behind the scene:

class Person {
    private var _name: String!

    var name: String {
        return  _name
    }

    init(name: String){
        _name = name
    }
}

In the video he mentions that private variables are meant to prevent classes from manipulating the data, but I can't understand why that would be a problem and how it would even happen.

Can someone please explain this to me like i'm 5?

Thanks for the help

Ryan H.
  • 2,543
  • 1
  • 15
  • 24
slimboy
  • 1,633
  • 2
  • 22
  • 45
  • 1
    Here's a general explanation: http://softwareengineering.stackexchange.com/questions/143736/why-do-we-need-private-variables – Keiwan Jan 06 '17 at 23:20
  • 1
    Here are more answers (essentially the same as in the first link) [Why encapsulation is an important feature of OOP languages?](http://stackoverflow.com/questions/18300953/why-encapsulation-is-an-important-feature-of-oop-languages) – Keiwan Jan 06 '17 at 23:27
  • 4
    BTW this is an awful style - you should better use this syntax: `private(set) var name: String!` – Fyodor Volchyok Jan 06 '17 at 23:35
  • 1
    @FyodorVolchyok - Agreed, if the class might change it but you don't want external class modifying it, private setter makes sense. Alternatively, if you're not going to change it after your initializer sets it, just `let name: String`. – Rob Jan 06 '17 at 23:53
  • 6
    This is Java code. Swift isn't Java. Don't write Java code in Swift. Also, don't trust anyone who writes Java code in Swift. – Alexander Jan 07 '17 at 01:04
  • I have rarely found it necessary to do this in my Swift apps, although I did it all the time in C#. – Mike Taverne Jan 07 '17 at 01:12

2 Answers2

11

Can someone please explain this to me like i'm 5?

OK, my five-year old friend, you are familiar with the "stranger = danger" rule, right? Sometimes, your objects need to interact with strangers. Although in many situations strangers interacting with your object are well-meaning, occasionally you would run across ones who want to harm your object, for example, by changing its name:

// If name were public, anyone could do this:
somePerson._name = "nasty-boy" // Not a good name!

In order to protect your object from strangers who want to rename them you make important things inaccessible to anyone outside your object by marking them private. This keeps these important things inaccessible to anyone outside your object. Object's own methods, however, can freely access private variables, for example, to return them to strangers for reading, but not for writing:

var name: String {
    return  _name
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Even if anyone is just *you*, then this technique is still useful because as your program grows *future you* may forget and change `_name` somewhere else inadvertently, introducing a not so obvious bug. – Ryan H. Jan 06 '17 at 23:42
4

If you want to make sure that a person's name is always a valid value, you need a place in your code to check the data that is written to the name field. If you hide the field by changing its visibility to private, code from outside the class must use the setter method to write to the name field. You can perform all your validation requirements in the setter method to make sure that the data within the person object has always a valid state.

This is a fundamental principal of object oriented programming. You can read more about at this link Wikipedia - Encapsulation

Markus Müller
  • 2,611
  • 1
  • 17
  • 25