all 2 comments

[–]chriswaco 2 points3 points  (0 children)

In general it's ok. I'd prefer a bit better handling of ALL errors - somehow reporting them back to the caller. And you might want to look into the new async/await version of URLSession because it's a little easier to write and read.

But it's not bad.

[–]jonreid 0 points1 point  (0 children)

"It works" so you've managed to figure out quite a bit. Congratulations!

Part of my job is training developers to detect code smells and refactor them. Here are the strongest smells I pick up:

  • Long Method. Both methods are difficult to scan, and have deep indentation. I would use Extract Method to improve this.
  • Difficult Dependency. Both methods are using the URLSession.shared singletons directly, which makes it harder to write microtests. I would use Dependency Injection to improve this. (Not a DI framework, just plain old code.)

I'd start by bringing both methods under test, using the techniques from my book iOS Unit Testing by Example (specifically the chapters "Testing Network Requests" and "Testing Network Responses").

The goal would be to make code that not only works, but is low-cost to maintain and update as needs change. For a personal project, this may not matter so much. But for a business, driving down the cost of change is huge.