[swift-evolution] Thoughts regarding readable INTENT of Loop keywords

Nathanial Burton-Bradford nathanial_bb at hotmail.com
Wed Mar 30 14:54:57 CDT 2016


Swift Control Flow using Loops


One of Swifts strengths is clear, readable INTENT, conveyed by the languages Keyword tokens. For anyone new to programming, clear, readable intent, goes a long way in helping them master the language, by helping reduce logical/semantic errors caused by a misunderstanding of the names used in specifying the languages keyword tokens…

Swift’s control flow keywords can be ambiguous, in this respect, when conveying the functional intent of loop control logic, within the language.


I would like to open up discussion, therefore, regarding the possible renaming of Swift’s control flow keywords, with the hope that this leads to clearer INTENT regarding loops.



Replace For-In with Loop Using


for foo in d…y loop foo using d…y


For x in 1…5 loop x using 1…5

loop i=0 using i<10

loop i using 0..<10




For _ in 1 …power ———-> loop using 1…power


“The underscore character (_) used in place of a loop variable causes the individual values to be ignored and does not provide access to the current value during each iteration of the loop.”


I feel underscore character (_) is confusing, semantically.



let names = ["Anna", "Alex", "Brian", “Jack"]

for name in names { ———-> loop names using name {

    print("Hello, \(name)!")

}


Note : loop ARRAY using ELEMENT(S) - which is logically consistent with ‘-using’ as last keyword instance.


let numberOfLegs = ["spider": 8, "ant": 6, "cat": 4]

for (animalName, legCount) in numberOfLegs {

    print("\(animalName)s have \(legCount) legs")

}


loop numberOfLegs using (animalName, legCount) {

print(“ \(animalName)s have \(legCount) Legs”)

}


Note : Again loop ARRAY using ELEMENT(S) - which is logically consistent with ‘-using’ as last keyword instance.



for (airportCode, airportName) in airports {

loop airports using (airportCode, airportName)

for airportCode in airports.keys {

loop airports.keys using airportCode {



for airportName in airports.values {

loop airports.values using airportName {


for value in array[1..<array.count] {

loop array[1..<array.count] using value


‘while’ using loop while


loop while square < finalSquare {

    // roll the dice

    diceRoll += 1

    if diceRoll == 7 { diceRoll = 1 }

    // move by the rolled amount


Control transfer statements


repeat { ——> loop {

…

} while



“Break

The break statement ends execution of an entire control flow statement immediately. ”


I propose replacing Break, in loops, with exit now (intent is explicit)

Note: Labeled Statements with break -

break gameLoop exit now gameLoop


“Continue

The continue statement tells a loop to stop what it is doing and start again at the beginning of the next iteration through the loop.”


“switch character {

    case "a", "e", "i", "o", "u", " ":

        continue // Continue what? to next line? to end? to beginning?

    default:

        puzzleOutput.append(character)

    }”


I propose replacing continue with next cycle or next loop



“switch character {

    case "a", "e", "i", "o", "u", " ":

        next cycle // intent is explicit

    default:

        puzzleOutput.append(character)

    }”


Note: Labeled Statements with Continue - as per Break example, next cycle gameLoop


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


More information about the swift-evolution mailing list