## Solved: HWS Checkpoint 9

Days 13 & 14 were definitely a bit tough and had to think about things. But I think I have the proper solution...

``````// Checkpoint 9
// Will return a single Int if at least one exists in the array passed in
// Otherwise will return a random number between 1 and 100 if either
//   the array doesn't exist (nil) or exist but is empty.

func getRandomInteger(sourceArray: [Int]?) -> Int {
return sourceArray?.randomElement() ?? Int.random(in: 1...100)
}

var myIntegers:[Int]? = [1, 6, 8]                                 // Source array

var randomeNumber  = getRandomInteger(sourceArray: nil)           // Gives a random number because there is no array
randomeNumber      = getRandomInteger(sourceArray: myIntegers)    // Gives one of the three numbers
myIntegers?.removeAll()                                           // Makes array empty but still exist
myIntegers == nil                                                 // Prints "false" shows array is empty but NOT nil
randomeNumber      = getRandomInteger(sourceArray: myIntegers)    // Gives a random number because array is empty``````

3

+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!

3

LOL...

Well I figure since I got +20 points I can absorb a -2...

But adding "from" makes it more readable for sure. As I wrote code in the past (now 60+ aged), I alway paid attention to naming as I agree it is key to looking at something later yourself or a team member. I guess I just wasn't thinking much about it here other than very general. Will focus a bit more going forward.

I also line things up as much as practical. Not everything but where it makes sense. Always have so thinks for noticing...

Keep up the excellent monitoring and critiquing!

3

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until July 28th.

Sponsor Hacking with Swift and reach the world's largest Swift community!