3
class CurrencyConverter {

    // 1
    private let conversionRate = 1.3 

    // 2
    private static let conversionRate = 1.3

    func convertToForeign(fromlocal local: Double) -> Double {
        return local * CurrencyConverter.conversionRate
    }
}

let c = CurrencyConverter()
print(c.convertToForeign(fromlocal: 5))

With reference to the code snippet above, assume that I need to use the constant conversionRate ONLY in an instance method.

What are the pros & cons of declaring conversionRate as only (1)let vs (2)static let ?

Which style would be more readable & optimised?

Additionally, let's say I will only need 1 short lived instance of CurrencyConverter and conversionRate itself is bulky (an array of 10K Doubles). Will (1)let conversionRate be more memory optimised?

Raunak
  • 3,314
  • 1
  • 22
  • 28
  • Personally I would not use `static` just because I don't have to write the class name all the time. – Sweeper Feb 06 '20 at 07:30
  • 2
    When you use `static let` you are saying that this value is global and will be the same for all instance of your class but when you use `let` only your are saying that the value is constant for an instance. Now in your case there is no practical difference since you instantiate the property with a hardcoded value but at least `static` tells you something about the purpose of the property. – Joakim Danielson Feb 06 '20 at 07:45
  • See also [Swift constants: Struct or Enum](https://stackoverflow.com/q/38585344/1187415). – Martin R Feb 06 '20 at 07:53

3 Answers3

2

Neither. Use an uninhabited (caseless) enum to create a Constant namespace; it reads better.

class CurrencyConverter {
    private enum Constant {
        static let conversionRate = 1.3
    }
    func convertToForeign(fromlocal local: Double) -> Double {
        return local * Constant.conversionRate
    }
}

let c = CurrencyConverter()
print(c.convertToForeign(fromlocal: 5))
Josh Homann
  • 15,933
  • 3
  • 30
  • 33
  • I like this style. However let's say I will only need 1 short lived instance of this class and conversionRate itself is bulky. Will "let conversionRate" be more memory optimised? – Raunak Feb 06 '20 at 07:40
  • @Raunak I would declare your conversionRate as static property extending Double `extension Double { static let conversionRate = 1.3 }` this way you can do `local * .conversionRate` – Leo Dabus Feb 06 '20 at 11:13
  • 1
    @Raunak the compiler loads and generally. deduplicates all constant primitives into a symbol table, so the 1.3 is in memory no matter what; its not like it loads it from disk when you ask for it. So it does't matter if its an instance or static constant -- they compile to the same thing, Thats not true if this were a class like a UIImage because then the data actually does load from disk, – Josh Homann Feb 06 '20 at 14:58
2

In the code below,

private let conversionRate = 1.3

the above is an instance variable, i.e. it will last as long the instance of CurrencyConverter (c) exists. Once c goes out of scope, conversionRate's scope also ends.

Whereas, in the below code

private static let conversionRate = 1.3

Here conversionRate is associated with the class CurrencyConverter and not the instance. So, it will remain in memory even if the instance c goes out of scope, i.e. as long as the app runs.

So, creating instance variable is more optimized than creating the static variable.

PGDev
  • 23,751
  • 6
  • 34
  • 88
0

If you add static to the class method or field then it is called class field/method, with this you can access it without creating an instance of the class.

Without static keyword the field/method will be instance field/method , and this is accessible only by creating the instance of the class.

In this case it’s better to go with static field as it’s not dependent on other class fields/methods, also it’s memory efficient if you have many objects of same class.

Community
  • 1
  • 1
Sohail Ashraf
  • 10,078
  • 2
  • 26
  • 42