Is there any gain in Swift by defining constants instead of variables as much as possible?

Is there speed in speed, memory usage, whatever, in Swift, defining as many x vars constants as possible?

I mean, defining as many let as possible instead of var ?

+6
source share
1 answer

In theory, there should be no difference in speed or memory usage - internally, the variables work the same way. In practice, letting the compiler know that something is a constant can lead to better optimizations.

However, the most important reason is that using constants (or immutable objects) helps prevent programmer errors. It is no coincidence that method parameters and iterators are constant by default.

Using immutable objects is also very useful in multi-threaded applications, as they prevent one type of synchronization problem.

+10
source

Source: https://habr.com/ru/post/971959/


All Articles