Naming a dictionary

Hi,

Apologies if this is not a very technical question, but I am struggling with naming my dictionary.

Questions

  • Is there a better way to name my dictionary and function?

Reasoning

  • This there are multiple String fields in Car and the dictionary and thought (could be wrong) it be better to state what the key field in the dictionary meant.

Code

struct Car {
    let name: String
    let sourceID: String
    let manufacturerID: String
    let price: Int
}

let cars = [Car]() //array
let carsGroupedBySourceID = [String: Car]() //Dictionary

func doSomething(withCarsGroupedBySourceID carsGroupedBySourceID: [String: Car]) {
}

Something like typealias?

typealias CarName = String
1 Like

@stuchlej That's a really good suggestion, making the type more explicit and would avoid the long name trying to explain the key.

Thank you so much!!!

I could now call it groupedCars :smiley:

2 Likes

The "grouped" is typically pretty easy to infer, so I usually for for names like carsBySourceID, peopleByLastName, etc.

Types can definitely help, too.

2 Likes

Thanks @AlexanderM that is a good suggestion yeah grouped could be inferred from the dictionary type