iflix/swift-style-guide

One type definition per file

Opened this issue ยท 7 comments

The trend in the codebase seems to be "one type definition per file", which is cool.

Are we fine with this? If we are I think we should apply it rigorously (see zero-one-infinity), that is even little types and typealias should have their own file.

Thoughts?

Yeah I'm in favour of this, it makes it much easier to find definitions.

Yes lets make a clear "one type definition per file" rule, which should apply to tests as well.

No, let's not do this and keep using the current mixed approach.

Generally I'm for one definition per file. There is though a problem with how we define definition. Sometimes is makes sense to an extent.

๐Ÿค” hmm.. to be honest, my preference is more into multiple type def in a file, as long as they are related. In let say, Actions struct definition. I prefer to include all actions related to Landing into one LandingActions file. But well, if we're going with one strict rule, I dont mind, anything for team productivity! :D

In my opinion, multiple definitions in a single file is still fine as long as they are strongly related. Let's pick up something as an example:

If you take a look at BarButtonItem.swift file, there are multiple *BarButtonItem structs being defined in a single class. Personally I think this is fine because:

  • each item struct is short only 4-5 LOC
  • each items are strongly related to each other.

I think in this case applying the 0, 1, infinity rule would help taking decisions. Rather than thinking whether it makes sense or not in an occasion compared to another, we shall always put one type definition per file. Less beard scratching.

We I say with "type definition" I'm thinking of:

class|struct|enum|protocol|typealias Foo {
  ...
}

This would result into

Preferred

// SomeAction.swift
struct SomeAction { }

// SomeProtocol.swift
protocol SomeProtocol {
  ...
}

// SomeType.swift
struct SomeType: SomeProtocol {
  ...
}

Not Preferred

// Actions.swift
struct AnAction { }
struct ARelatedAction { }
struct AnActionThatHasSomethingToDoWithTheOtherOnes { }