I believe there comes a time when every developer adopts a technique for reusing common parameters. For years, I've been keeping all of my "settings" in a single place:

GlobalSettings struct example

Which I would use like:

  • label.textColor = GlobalSettings.darkRed
  • directionalLayoutMargins = GlobalSettings.defaultMargins

But there is a better way.

Extending individual types:

UIColor extension with static darkRed property NSDirectionalEdgeInsets extension with static defaultMargins property

This makes coding so much easier and less verbose at the point of assigning — just start typing with a dot and rely on autocomplete to get to:

  • label.textColor = .darkRed
  • directionalLayoutMargins = .defaultMargins

And as a bonus, keeping these extensions in individual files reduces compile time. Remember GlobalSettings? Changing a single parameter in there would require every source file that references GlobalSettings to get recompiled.


Images above were generated with Snippet built by Seb Vidal.