2

This is more of a concept question. Why bother with using init?

    class Person {
        var name:String
        var height:Double
        ...

        init(name: String) {
        self.name = name
        self.height:Double
        ...
    }

Why not just give everything a default value?

    var name = "Daniel"
    var height = 178.0

That way, I also won't have to worry about deciding between designated and 'convenience' inits because everything would just inherit from their superclass. Is there a reason for having this init method?

Does it allow for coding patterns that a strictly default-value-initialized app cannot achieve? Or is it for reasons like resource/memory management?

  • 1
    So when you don't set the name/height for a new Person you are fine with it being named `Daniel` which is `178` tall? – Luca Angeletti Aug 17 '16 at 13:31
  • If no initializer is provided by programmer Swift provides a defalut initializer which set the declared property to its initial declared property. Property must be declared, either a nil or non nil value. – Khundragpan Aug 17 '16 at 13:32
  • 1
    So every `Person` instance should by default be named Daniel and have a height of 178? That doesn't sound like a good idea. Having spurious data as the default just invites programmer error of forgetting to populate those fields with real data, which is what the initialiser is supposed to ensure in the first place. – Hamish Aug 17 '16 at 13:33
  • 2
    You also lose the benefit of having immutable properties. Setting them as vars means they can be changed externally, which may not always be what you want. – Abizern Aug 17 '16 at 14:01
  • But those aren't constants. Whether it has a default value or not, I will definately change those values when I create new instances in the future. The only difference being that in one case I am replacing "daniel", the other case I am replacing "nothing". Hamish, so you're saying that the reason for `init` existing is to prevent human error? It has nothing to do with code functionality or memory cost? – Daniel with a Spaniel Aug 17 '16 at 14:11
  • 2
    There are multiple reasons why this is a very bad idea. One of them is the order of initialization because some properties usually depend on other properties. Some things are always constant (most of them, ideally). I think you are missing the point of modern programming languages. They are not better than older programming languages (e.g. C or C++) but they can help you to write a more robust code, preventing you from making bugs and write more readable code. By intentionally avoiding the features, you are making it very hard for every future mantainer of the code. – Sulthan Aug 17 '16 at 14:17
  • @Sulthan I see. Thank you very much for the reply. I understand now. – Daniel with a Spaniel Aug 17 '16 at 14:41

2 Answers2

3

Lets assume you have a factory that builds cars and a tool / class "BuildCar" with the properties:

var numberOfTires =  4

var color = UIColor.red()

var maxSpeed = 100// mph

Now you want to build a whole lot of your fancy cars and use your 'BuildCar' Tool to do so. All you can do is build red cars that have 4 tires, and have a max speed of 100 mph.

But what if you want to build a new car? A blue one, with 4 tires and max speed of 120? If you used init, you could change the variables easily.

Pranav Wadhwa
  • 7,666
  • 6
  • 39
  • 61
chriOSx
  • 89
  • 1
  • 13
  • Ohhh. I see. If I am doing consecutive changes to the class one step at a time, it would be much easier to have the class retain its previous values rather than defaulting them again. Got you. Thanks for the great edit, penatheboss. – Daniel with a Spaniel Aug 17 '16 at 14:17
1

If you were very careful, you could get away with it. It would require you to configure every Person object with all the correct values as soon as you create it, to prevent it from using the default values.

After just a few uses, though, that would mean more code written than just creating an init function that accepts the required values to initialize the object.

One of the dangers is that if somebody else (or you in the future) uses the code, they might initialize a Person, and not configure it properly, which could cause unexpected behaviour.

Suggestion

In many cases, you don't need to use a class, you can get away with using a struct. One of the magical properties of structs is that as long as you don't create an init method yourself, a default init is created which accepts a value for each property.

In you case, if Person was a struct, you could initialize it like this without writing the initializer yourself: Person(name: "David", height: 178)

EmilioPelaez
  • 18,758
  • 6
  • 46
  • 50