[Workshop – 1] What is wrong with this viewDidLoad() method?

This article is part of an email series I wrote to my subscribers to teach them how to refactor existing legacy code. If you want to join us in the next workshop, make sure you subscribe.

  1. What is wrong with this viewDidLoad() method?
  2. Don’t waste time writing tests for untestable code
  3. Breaking up a method into shorter methods with single responsibilities
  4. How to write clean code in a component architecture using Clean Swift

Take a look at this viewDidLoad() method.

What is wrong with it? Why is it so bad? Can you tell what it does?

After the view is loaded, it tries to show the 5 most recent posts of a user’s followers. If the user isn’t logged in, it shows a login button.

  • How long did it take you to read and understand what it does?
  • Do you think you’ll remember a week from today?
  • If I ask you to make a change, will you know what to do without having to read it again?
  • Can you be sure you won’t break something when you make the change?
  • What do you feel about passing this along to someone else who needs to make a change?
  • What if you are on the receiving end of this method?
  • How will you go about fixing it?

We’ll be dissecting this method down to the nitty-gritty to find ways to improve it.

Hit reply and let me know your answers now.