Pass The Swift Code Review: Dictionary Edition
Difficulty: Beginner | Easy | Normal | Challenging
This article has been developed using Xcode 12.1, and Swift 5.3
- Some knowledge of Swift’s Dictionaries would be useful
- The answers here use the Ternary Operator
- Control Transfer Statements are used in the final solution
- Higher-Order functions are used in the final solution
- Reduce is used in one of the functions
Dictionary: The association between keys and values (where all the keys are of the same type, and all values are of the same type)
Here is a way of passing that code review, even though you think your code is perfectly adequate in any case.
Writing Succinct Code
I know, it’s annoying. You have clear working code and it does not necessarily pass code review due to style (that isn’t actually in your firm’s style guide).
Be careful out there, we need to write great code. So we need to understand what this is.
There are a number of developers at an organization, and they are stored in a dictionary (which is a dictionary of Strings). Something exactly like the following:
let developers: [String: [String]] = [ “Mr”: [“Lim”], “Mrs”: [“Smith”, “Abayomi”], “Miss”: [“González”], “Ms”: [“Johnson”, “Wilson”], “Other”:  ]
What we would like to do, is create an array of Strings which can then be used in a
UITableView or similar. That is, an output of the following:
[“Ms Johnson”, “Ms Wilson”, “Mr Lim”, “Other”, “Miss González”, “Mrs Smith”, “Mrs Abayomi”]
where the order does not matter.
A GREAT Solution
So we need an array of Strings. No problem, we can traverse the dictionary, and if there aren’t any values we put the key into the Array. If there are, we can append the key and value into the Array (for each possible value).
The code is, arguably, easier to read than a great explanation. That explanation isn’t great so here is some easy to read code:
There is nothing *wrong* with this solution. However, it won’t always pass code review.
The reason — it’s not as short as it might be.
We can reduce the developers into an Array of String.
We are appending to the result Array as we go, and have each element of the Dictionary to deal with as we traverse the dictionary. Of course we need to map all of those lovely values to make sure each and every one has a key — no problem!
so we map the developers dictionary. The logic is around the same as the code above, but makes use of the ternary operator to return an array of arrays. Then, of course, this needs to be
flatMap into an array of Strings, which is duly done.
You’ll notice that this hasn’t been placed into the result array, this is partly because you can print the answer direct to the console (map return the String array in this case).
flatMap ( I like this one )
We can (wait for it) SKIP A STEP. Calling map followed by flatMap seems like rather a waste of time. Therefore we can make this a little easier by seeing if there are any values for this element, if there aren’t add the key if not add the key and the value (for each value mapped).
Here is a better explanation, the code:
Opinion, opinion, opinion. This is all about opinion.
All of the code posted in this article work. They pretty much all work in similar ways.
For some, this is all they need to know. Some are looking for the *correct* solution, and unfortunately that isn’t what this article is about.
This article is about opinions, and thinking through solutions to problems. If you can do that, you are well on the way to becoming a great developer.
If you’ve any questions, comments or suggestions please hit me up on Twitter