The exercise task at p. 222 to rewrite the logic to use a didSet property observer on the image instance variable so placing the photo into image will automatically update the UIImageView, without needing to call show(image) looks not so clear to me as you either need to call show(image) from the observer or move the logic from that function to the observer so I ended with this:
The logic of showing an image is encapsulated in this function. If in the future the requirements will change where or how to show an image you need to change the logic just in one place while all other places simply call this logic. Even if there are two places itâs hard to remember/recollecte where they are which gives you a potential bug.
I donât agree. By moving the logic for showing the image into the property for the image, you simplify the logic â now the only way to change the contents of the image view is through the image property. Therefore, it hides an implementation detail (the existence of the image view) and actually increases encapsulation.
In the current state of affairs, youâre right. The show(image:) is called in one place and moving in into the didSet is better. What I want to say that decoupling the logic from the didSet has an aim to ease development in the future if requirements change.
Perhaps⊠it depends on the future. Itâs usually impossible to see into the future, and many would argue that already preparing your code for a future that might never happen is not a good use of your time.
Of course, if you are 99% sure that youâre going to have to add a new feature to the app that requires you to have a separate show(image:) method, then it makes sense to have it as a separate method. But if not, then making it separate method âjust in caseâ is not guaranteed to actually make your code any better.
Youâre quite right, my friend. This is the hardest part to find the âGolden Spotâ in decoupling and encapsulation. I bet, itâs a skill gained with an experience to predict what parts of your code are possible to be changed in the future.