kodecocodes / swift-style-guide

The official Swift style guide for Kodeco.
Other
13.11k stars 2.15k forks source link

Proposal: Use let for all constant values #1

Closed ColinEberhardt closed 10 years ago

ColinEberhardt commented 10 years ago

I find myself using let for constant class properties, but often forget that you can use let for constants within the scope of functions.

My proposal is that let is used for any value that is a constant.

gregheo commented 10 years ago

I have a feeling I'm going to end up just saying :+1: to all of these issues :]

Following the general trend of immutability by default in Objective-C, I think this is a good idea.

hollance commented 10 years ago

Use let unless it needs to be a var. :-)

EDIT: I'd even say, Use let unless this results in a compiler error. (Or at least, unless the compiler won't let you.)

ghost commented 10 years ago

I don't think this is even a style issue. Apple said to use let for all constants. The compiler apparently produces faster code that way.

To get into the habit, I'm typing let all the time and then only changing to var when I find myself trying to change a value.

Sent from my iPhone

On Jun 10, 2014, at 3:55 AM, Matthijs Hollemans notifications@github.com wrote:

Use let unless it needs to be a var. :-)

— Reply to this email directly or view it on GitHub.

Paradox927 commented 10 years ago

I agree, we should use let for all constants.

hollance commented 10 years ago

I'm currently converting a SpriteKit demo app to Swift and it's not using var anywhere. :-)

rnystrom commented 10 years ago

Agreed, and especially important when using collections

sammyd commented 10 years ago

:+1:

cwagdev commented 10 years ago

:+1: