[Swift] Rather, it is meaningful to avoid var and declare it with let only when there is a branch in the assignment of the initial value.

Prerequisite environment:

Not limited to Swift, "avoiding reassignment to variables (as much as possible)" is widely understood as a theory for writing secure code.

Related Links: Why do you want to avoid reassigning to variables

However, in practice, it is easy to think that you have to write the reassignment code in the case where there is a branch in the assignment of the initial value as shown below.

Sample A


//The variable foo is ""hoge" or "fuga"The intention is that one of them will be initialized and will not change after that. "
var foo = ""
if isHoge {
    foo = "hoge"
} else {
    foo = "fuga"
}

The above code does not prevent reassignment to the variable foo. It remains possible that foo will be set to a value other than ("hoge" or "fuga") somewhere.

The above code can be improved as follows.

Sample B


let foo: String
if isHoge {
    foo = "hoge"
} else {
    foo = "fuga"
}

** If there is a path that is not initialized, the compiler will make an error **, so if you intend to "set the initial value and then do not reassign it",

--Variable initialization omission can be prevented. --It can be guaranteed that the value does not change after initialization.

In that respect, I think the code in sample B is safer.

Recommended Posts

[Swift] Rather, it is meaningful to avoid var and declare it with let only when there is a branch in the assignment of the initial value.
How to output the value when there is an array in the array
How to get the ID of a user authenticated with Firebase in Swift
[Java] When putting a character string in the case of a switch statement, it is necessary to make it a constant expression
How to solve the problem when the value is not sent when the form is disabled in rails and sent
In Swift, when JSON purge, it is passed in the snake case and troublesome coping method
[Ruby] Thinking when there is no receiver in front of the method (it looks like)
Determine that the value is a multiple of 〇 in Ruby
[Swift] Get the timing when the value of textField is changed
[Swift] When you want to know if the number of characters in a String matches a certain number ...
Throw an exception and catch when there is no handler corresponding to the path in spring
Graph the sensor information of Raspberry Pi in Java and check it with a web browser
Generate a serial number with Hibernate (JPA) TableGenerator and store it in the Id of String.
[Java] Output the result of ffprobe -show_streams in JSON and map it to an object with Jackson
What I did when I was addicted to the error "Could not find XXX in any of the sources" when I added a Gem and built it
The story of forgetting to close a file in Java and failing
[Ruby] I want to extract only the value of the hash and only the key
[Java small story] Monitor when a value is added to the List
Initial value when there is no form object property in Spring request
How to change the value of a variable at a breakpoint in intelliJ
Is it possible to put the library (aar) in the Android library (aar) and use it?
[Note] [Beginner] How to write when changing the value of an array element in a Ruby iterative statement
When reassigning to an argument in a Ruby method and then calling `super` → The reassigned one is used
I tried to make a web application that searches tweets with vue-word cloud and examines the tendency of what is written in the associated profile