Trikalabs
  • Home
  • Best online TDD videos
  • Book Suggestions
  • About Me
  • Contact
Trikalabs
No Result
View All Result

Project Mess #2 – Magic numbers for UI spacing

by fragi
February 18, 2022
in Project Mess
Share on FacebookShare on Twitter

In this short article we will address one common issue that can lead to a project mess is when all the paddings and spacing are hardcoded in the UI. Even having them as constants in the file does not solve the issue nicely.

Paddings and spacing should be mostly driven by a design library specs and should be centralised so any changes can apply globally.

Let’s see the following example:

Swift
1
2
3
4
5
6
7
8
struct ContentView: View {
    var body: some View {
        VStack {
            Text("Hello, world!")
                .padding(.leading, 8)
        }
    }
}

In order to remove this hard coded value we can introduce a Spacing enum:

Swift
1
2
3
4
5
6
enum Spacing: CGFloat {
    case none = 0
    case tight = 8
    case standard = 16
    case loose = 24
}

And now we can replace the 8 with Spacing.tight:

Swift
1
2
3
4
5
6
7
8
struct ContentView: View {
    var body: some View {
        VStack {
            Text("Hello, world!")
                .padding(.leading, Spacing.tight.rawValue)
        }
    }
}

fragi

fragi

Related Posts

Project Mess

Multiple Equatable methods on different Packages – Bad practise

May 24, 2023

In this article we will explore what happens when we have different equatable methods for a type across packages and...

Project Mess #3: Hardcoded UI colours
Project Mess

Project Mess #3: Hardcoded UI colours

February 19, 2022

In this short article we will see another project mess.Let's see the following example: A worst variation of this...

Project Mess #1: No Localised Strings.
Project Mess

Project Mess #1: No Localised Strings.

May 13, 2021

Every app has some static text. Either as a title of a screen or as a title of a button,...

Next Post
Project Mess #3: Hardcoded UI colours

Project Mess #3: Hardcoded UI colours

Extract build settings – Xcode

Extract build settings - Xcode

Postman – Mock server

Postman - Mock server

  • Advertise
  • Privacy & Policy
  • Contact

© 2019 Trikalabs Ltd. All rights reserved.

No Result
View All Result
  • Home
  • About Me
  • A curated list with the best free online TDD videos
  • Book Suggestions
  • Pinner Code Club

© 2019 Trikalabs Ltd. All rights reserved.