+10 points for testing the empty array. Nice.
Everyone tests with a sample array. Some will test for the nil
case. Nice to see you also test the empty array case.
+10 points for equal sign and comment alignment. This is a style few adopt. But I use it, and think it makes your code look well organized.
-2 for parameter name
Nitpicking
This is very subjective comment. But when you write code for yourself, you know exactly what you logic is, your expected parameters, and desired output. Go crazy with how you name things.
But at some point, you'll be sharing your carefully crafted functions, classes, and custom views with your team. Do they know your intentions?
Consider how you name your function's parameters. Which of these seems more natural?
getRandomInteger(sourceArray: [Int]?) -> Int
// or -------
getRandomInteger(from sourceArray: [Int ]?) -> Int
In the second case, your teammate's code will read getRandomInteger(from: anArrayOfIntegers)
And Xcode will tell coders that the array must be an array
of integers
, or it could be nil
.
It's your choice, of course. But consider how a different parameter name can make the code read more naturally.
Keep Coding!