[swift-evolution] Hidden initiallizations ...

Haravikk swift-evolution at haravikk.me
Tue Mar 29 07:33:03 CDT 2016


I’m a +1 for warning in cases like that, if let foo = foo is probably fine, though personally I use different names anyway.

Actually I use a lot of Applescript style naming, though admittedly I can be a bit inconsistent about it. For example, I like using eachFoo as a name for a loop variable like so:

	for eachIndex in 1 ..< 100 { … }

Which can read nicely as natural language, but since I don’t use eachFoo anywhere else, helps to avoid name collisions. If eachFoo is optional, then I might unwrap the value into theFoo instead like-so:

	let theValues:[Int?] = []
	for eachValue in theValues {
		if let theValue = eachValue { /* Do some stuff */ }
		else { /* Do some other stuff */ }
	}

I actually see a surprising number of examples like:

	for i in 1 ..< 100 { … }

But does anyone actually use these? Since I learned to stop using c-style loops in Swift I haven’t used single letter variables at all, so this seems like partly an issue of choosing bad variable names; not saying mine are better, but I find that they work well for me, even if they’re a bit longer overall. Unless I’m implementing some kind of formula with well understood letters for variables I wouldn’t use such short names anywhere, and even then I’d probably try to find out what each variable is and find a full name for it, for my own sake.

> On 29 Mar 2016, at 12:39, Andrey Tarantsov via swift-evolution <swift-evolution at swift.org> wrote:
> 
> This is idiomatic Swift. I see no readability issues with that.
> 
> Of course, shadowing a local var with a for loop counter is another case. I'd say a warning is warranted there.
> 
> A.
> 
> 
>> On Mar 29, 2016, at 5:30 PM, Biala <bialata at yahoo.com <mailto:bialata at yahoo.com>> wrote:
>> 
>> And you are OK with that ????
>> I can not imagine my team writing such code and me looking to 5 code reviews per day having to find out what the variable actually is every time :)
>> 
>> For me it is OK for student projects but not for big code that have to be supported for years ...
>> 
>> 
>> On Tuesday, March 29, 2016 2:02 PM, Andrey Tarantsov <andrey at tarantsov.com <mailto:andrey at tarantsov.com>> wrote:
>> 
>> 
>>> Having more that one variable with the same name in one method is stupid and the language should prevent that at least. 
>> 
>> Actually, we're doing it all the time:
>> 
>> if let foo = foo {
>>   ...
>> }
>> 
>> var foo = foo
>> 
>> You can do this in a subscope, you can do this to the arguments, and you can do this to fields.
>> 
>> A.
>> 
>> 
>> 
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160329/dba57404/attachment.html>


More information about the swift-evolution mailing list