For instance, now I have this code
mutating func next() -> Element? {
//...
pos += 1
return data[pos-1]
}
or alternatively mutating func next() -> Element? {
//...
let e = data[pos]
pos += 1
return e
}
where previously I had mutating func next() -> Element? {
//...
return data[pos++]
} mutating func next() -> Element? {
//...
defer { pos += 1 }
return pos
}
Whether it's better is a matter of opinion return data[pos]i += 1; String name = names[i];
more readable than
String name = names[++i];
where I have to pause and remind myself of pre- and post- increment and which one is being used here and mentally translate the code into the above version.
Imperative languages already have a way of specifying execution order: it's the order of statements in your file. Let's reuse that rather than making things more complex.
As for brevity, I think clarity is more important. We should optimise for the time it takes to read and understand the code (clarity), not just read (brevity).
The best abstractions and programming language features are both brief and clear, like Python's list comprehensions. I find
[name.uppercase() for name in names if name.startswith("a")]
to be clearer than Java's
List<String> uppercaseNames = new ArrayList<>();
for (String name: names) {
if (name.startsWith("a")) uppercaseNames.add(name.toUpperCase());
}
So, the best programming language features enhance both brevity and clarity. When that's not possible, I'll take slightly longer but clearer code over short but confusing one.
It's death by a thousand papercuts for my mental load.
for (index, element) in list.enumerate() { print("Item \(index): \(element)") }
Functional decoration trivially provides that. Just zip a counter with the backing iterable, and modern languages generally provide that OOTB as a variant of `enumerate()` e.g.
for i, item in it.enumerate():
# stuff
> Also, x++ is a common syntax in many languagesMeh. It's generally present in B derivatives (via C) and that's pretty much it. Most languages do just fine without it.
for-in doesn't tell you the index of the current object so you'd have to manually keep track of the index.
Also, x++ is a common syntax in many languages and I think a new developer looking at Swift would try that first before using the other syntax (x += 1).