somu
(somu)
1
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]) {
}
stuchlej
(Mikoláš Stuchlík)
2
Something like typealias?
typealias CarName = String
1 Like
somu
(somu)
3
@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 
2 Likes
AlexanderM
(Alexander Momchilov)
4
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
somu
(somu)
5
Thanks @AlexanderM that is a good suggestion yeah grouped could be inferred from the dictionary type