Michael Long
1 min readMay 20, 2021

--

Your solution is overcomplicating the problem. Now, if you have a half-dozen different functions on your Accunt class, and if each function was doing the if-then-else dance based on account type, THEN doing a state class has merit as it would dramtically simplify the internals of each function.

But for a single function? Not so much.

Further, this article is like your previous article in which you show the simplified code, but rather conveniently left out the code needed to perform the same work.

This "solution" hard-codes the state to Open in the initializer. But to be truely functional and actually perform the same work as the original "bad" code, you're going to need some sort of switch statement somewhere to map the account state to the correct state object.

And that code is missing in the "simplified" version. As is, there's no way for the Closed or Frozen objects to be instantiated or used. In short, you've improved ithe code to the point where it no longer works.

In a way, this ambly demonstrates one of my favorite axoims, "More code == more bugs".

--

--

Michael Long
Michael Long

Written by Michael Long

I write about Apple, Swift, and SwiftUI in particular, and technology in general. I'm also a Lead iOS Engineer at InRhythm, a modern digital consulting firm.

No responses yet