In chapter 13 the book introduced a MockService
which is great pattern for testing the app without using real API calls, but its implementation contains a lot of annoying duplicated code and it’s logic is unnecessary complex (IMHO).
Even if that’s not too important for the concepts learned in this chapter, for beginners it may seem too complex, and they may learn bad practices if they take it as it is, so I though I may share my refactoring to this class to make it more simple and clear (I hope ).
-
Current implementation contain a lot of duplications, and the usage of
switch
makes the logic unnecessary complex:
-
In my refactoring I used an array
_jsonRecipes
to hold the decoded jsons for the follwing reasons:
1- to make the solution more flexible than using just two fixed variables.
2- to make the logic of picking a random response inqueryRecipes()
simpler.