3 ms·
Dart vs. Swift: A Comparison
- Someone 8y agoNice overview. My eye caught one bug: the code for ”iterates over a list of items, using a closure to print the index and contents of each item” is buggy in both examples; it will print incorrect index values if the list contains duplicates. It also is inefficient, searching the list for every value in the list, turning an O(n) algorithm in O(n²). It’s easy to prevent that by just keeping track of the index value, increasing it in the lambda. That”s ugly, but both faster and correct. In Swift, you can do better by using enumerated() (https://developer.apple.com/documentation/swift/array/1687832-enumerated https://developer.apple.com/documentation/swift/array/168783...). Chances are Dart has a similar construct. (Yes, I know that’s not what is being discussed in that section, but people will copy-paste that code and learn incorrect patterns from it. To prevent introducing two concepts in one example, I would change the example to just print the items, and add an item on the use of enumerated)