comment 0

#15 Nested enums or rather never ending posts about enums:)

Today I would like to discuss how to present UITableViewCell contents by using enums. A simple switch statement can help us with displaying data on our tableView. What scenario would I like to focus on?

PlaceInfoView
PlaceInfoView

To be more precise, I have created a simple schema of this screen:

PlaceInfoScheme
PlaceInfoScheme

By the way, have you had a chance to use Sketch for prototyping? Quite a nice tool!!!

The question is: what is the best proposal of enum configurations and functions, which can help us with displaying data when using tableView:cellForRowAtIndexPath: method for an example above?

My first proposal was:

Some statistics of the above code:

  • Lines of code of tableView:cellForRowAtIndexPath: function: 28
  • What did SwiftLint say?
SwiftLint
SwiftLint
  • Codebeat rated the code to 3.97 GPA (Function too long)

My intuition prompted that it can be done better:). So I have started thinking of how to refactor the code.

First refactoring

So here we go with first refactoring. Below is my to do list to make this enum better:

  • Remove white spaces according to SwiftLint
  • Reduce cases in enum to one line
  • Use nested enums
  • Necessarily I wanted to check if enum associated values can improve code metrics. I don’t know why, but I hoped that it will improve my situation:)

Is it better? I hoped so, so I have asked my friends about feedback:

  • Lines of code of tableView:cellForRowAtIndexPath: function: 20

  • What did SwiftLint say?

SwiftLint
SwiftLint
  • What did codebeat say?: 4.00 GPA

Second refactoring

I still had a feeling that the code was too complicated. I thought that getting rid of associated values would make it simpler.

Feedback:

  • Lines of code of tableView:cellForRowAtIndexPath: function: 18
  • SwiftLint:
SwiftLint
SwiftLint
  • Codebeat gave me 4.00 GPA for this code. codebeat badge

It seems that the easiest solution is the best:

GitHub Logo
GitHub Logo

Third refactoring

I would like to leave the next iteration of refactoring for you. What would you change to improve the solution?

Resources

Leave a Reply


*