-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Variable name for Optional Binding #64
Conversation
|
I often find myself shadowing the original variable, but I am in two minds as to whether this constitutes good style or not: if let title = title {
// do something with title
}I like that we're not making up pointless new variable names (e.g. I am finding that shadowing is conceptually appropriate and am not having difficulty understanding. This is primarily from writing code as opposed to reading it though. sam |
|
I'm with Sam. Coming up with other names just seems starnge to me. Or having an instance variables like — On Sun, Nov 23, 2014 at 5:28 AM, Sam Davies notifications@github.com
|
|
Sounds like a practical solution. It could be confusing though, because one variable name maps to multiple types now. Shuo |
|
Looking back at my Swift code, it's a bit of a mess regarding optional naming. I've used Sam's technique of 'shadowing' in a few places, but have also got some horrible names like All horrible! Personally I think the shadowing approach is the most elegant. |
|
I agree that shadowing is most elegant. However it, for me, never stopped feeling wrong |
|
For me |
|
I also think shadowing is the cleanest approach, but I too have concerns about legibility. What are your thoughts about naming conventions when dealing with collections of type I was originally prefixing "any" to collections of type |
|
I'm feeling against putting "unwrapped" or "u" in front as it starts to smell like much-maligned Hungarian notation, which strong typing should be saving us from. I like the idea of shadowing. I haven't seen many optional variables named like "maybeTitle" but rather just "title" and I don't see the benefit of "maybeTitle" and "reallyTitle" or any such prefixes – just call it a title and whether it's a |
|
Thanks for all the opinions! I'm now convinced of the shadowing approach. Should we expand the style guide to include this? |
|
👍 for shadowing - let's get this into the guide as it's a common question/problem we have. |
|
Thanks for starting the discussion here @ishuo! |
Variable name for Optional Binding
|
👎 I got here because I saw this shadowing being used in an NSHipster article w/o explanation and had to Google around to figure out what the real deal was. I won't be the only one. Feels hacky. Just to make it look nicer? Reminds me of the hubub concerning "optional" semicolons in javascript. Just because you can doesn't mean you should. |
|
I sometimes go with let title = myTitle, but this sounds like an online product name from 2002, so I change it to the more generous and plummy let title = ourTitle. |
|
@patchin I am now used to the shadowing. Works pretty well in my projects, pragmatic! Of course you can make suggestions if you have better ideas. |
|
How about: or: It also has the visual effect of looking unwrapped. |
|
Been following the various discussions on this topic and thought I'd add my view. To me, shadowing felt a bit strange at first just as not prefixing 'properties' with self did. Now I've gotten used to both and don't look back. I agree 100% with what @ColinEberhardt wrote on issue #75. Regarding using underscores as prefixes, I don't personally distinguish between private and public variables in Swift. ...not yet. If I would, I imagine they would look like this: I would in other words expect underscore to have the same meaning in Swift as in most other languages I've worked with. So I'd be careful giving them a different meaning in Swift. |
|
|
@patchin - My point was that a local variable would then risk having the same prefix as a private class member. So far I haven't seen any guidelines for private vs. public class member prefixes but I would imagine that we'll get there one day even in Swift. |
|
I swear, you've edited your comment after I've made mine. Ok then, use the underscore as a suffix. |
To use the value of an optional property, we can use Optional Binding. For example:
But I'm always confused about the naming of the optionally bounded variable. In the case above, how should the variable
tbe named? Using the first letter is of course very bad!Any ideas?