Refactoring String to TypeAlias to Enum

One small step ...

I learned ... rather I was exposed ... but haven't quite mastered the coding step of small-revertable-steps. I've seen my mentor James Greening do this consistantly in his workshop on C/C++ TDD - I'm practicing! Still... practicing...

The switch from a String -> to a typealias is uneventful... it goes without issues.

typealias AssetType = String

Next - an Enum... failure.

So in my Swift app project - I've gone a bit wild with Strings all over the place. Lance Kind - another mentor on TDD, has called this Primitive Obsession! So here is a chance to practice... I'm refactoring in tiny steps and seeing what breaks and playing with options to work out of the broken code and keep it compiling and running.

The switch of t
ype to an Enum - AssetType results in the enclosing structure no longer conforming to Codable. The fix is simple... and doing small changes means I understand the immediate impact the Enum caused. The fix... conform the Enum to Codable.

That is a ah-ha moment for the practice of SMALL STEPS!

I've got two other compile errors in another effected file. Yet, because my context of changing to the Enum - I know just how to fix them.

And I've got design choices: a) an unidentified AssetType or a nil value, or assuming a stock type. Looks like a coin toss.

Oh - WOW! Didn't see that problem until I ran it.

I'm guessing the JSON decodder doesn't apperciate my new AssetType!

I'm happy I have the very tiny context to associate this verbose but mostly useless message within my brain.

case unknown = "unknown"

Breaking that down to bits ... with my mental context of swapping out from the type alias to the enum - I find the one object that has result & an interger - SearchResponse. My guess is that the API has provided a type of nil within the SearchResults. So I wonder how I tell the JSON decoder to map from nil to my unknown case? Let's check the interTUBE...
how do you set nil to codable enum when unexpected value in swift iOS

Because I've got such a small step to debug and the ability to work with running App code - after reading all the options in the Stack Exchange - I'm going to set my unknown type to the nil String to see if this just might work.
"Cannot initialize AssetType from invalid String value ", underlyingError: nil

Wow!
Don't I feel smart - and lucky - it works!

Review: What did I learn?

Going slow and tiny has many advantages if the cognitive load is HIGH... and the tiny steps greatly reduces the high cognitive load of coding many unrelated changes. Therefore when problem (bugs) do occur and boy will they... one has the receient context of what just changed. This is very powerful in reducing the UNDERSTANDING of the problem (identifing the bug's root cause) and makes fixing it rather easy!

I learned that going from a primitive String to a swift typealias is rather trivial... and might be enough to get some level of type-safety into your code.

Moving from a String to an Enum is very easy in the code base - the compiler will point out the call-sites that need to change. But there are some lurking run-time errors waiting on this migration to bite you!

An Enum type just might need that NIL check or an unknown case - even though a programmer knows (thinks) this will not be needed - OUCH!

... later on ...

I've discovered an issue with the typealias Price = String it is causing this compile error:

And then the Docs for StringProtocol say DONT DO IT. Only the String and Substring types in the standard library are valid conforming types.

So what's a caveman to do?

Let me try to back out of that one typealias and see what happens.

That didn't get me anywhere... just one less jump of indirection.

And the Binding computed properties are not helping... maybe I'm doing it WRONG?

Then xCode 14.1 RC crashed... maybe it is time for a dog walk...
Here's a Pro-Tip for you - when xCode crashes and then will not open your project file... you might have luck ... restart BUT open a different project FIRST... then close it nicely and open your other project that xCode crashed with. Works for me!

an update...

Later on... I've found that the API I've been using for Stock Symbols has an undocumented number of AssetTypes so using the Enum for that means I'm failing to decode the JSON every time I run into yet another type. So reverting back to a simple String for AssetType - allows parsing to succeed.