Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid defining a value's type if it can be inferred #13

Open
mokagio opened this issue Feb 10, 2017 · 5 comments
Open

Avoid defining a value's type if it can be inferred #13

mokagio opened this issue Feb 10, 2017 · 5 comments

Comments

@mokagio
Copy link
Contributor

mokagio commented Feb 10, 2017

Here and there in the codebase we have some code doing:

let foo: String = "string"

let fontSize: CGFloat = CGFloat(42.0)

In such cases the type information is redundant, the compiler can infer it, an so can the reader.

A point has been made that this style provide more clarity, I can see that point, although I personally disagree.

I have a 3 suggestions which I'll list as single comments so that they can be 👍 ed.

@mokagio
Copy link
Contributor Author

mokagio commented Feb 10, 2017

Never define the type if it can be inferred.

@mokagio
Copy link
Contributor Author

mokagio commented Feb 10, 2017

Take into account the fact that specifying the type adds clarity and do so only when such clarity is needed.

This opens a whole new can of worms. A comment I got a few times is that this style is useful in hte properties list of a type, so:

Always define the type when listing properties for a type

@mokagio
Copy link
Contributor Author

mokagio commented Feb 10, 2017

For @mokagio to admit this is just a pet peeve of his, and forget about it.

@marcelmika
Copy link

There's also one thing I saw:

view.color = UIColor.red

I think the information of UIColor is redundant as well. So idealy:

view.color = .red

@nhisyam
Copy link

nhisyam commented Mar 9, 2017

Found a good article talking about this. (But couldn't locate it now). This does in a way affect our Xcode build time due to how its inference engine works. We can make it work less by putting in the type.

But hey, who knows future Xcode will improve on this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants