I have two maps, let's call them oneMap
and twoMap
.
I am iterating through all the keys in oneMap
, and if the key exists in twoMap
I do something
like
fun exampleFunc(oneMap: Map<String, Any>, twoMap: Map<String, Any>) {
for((oneMapKey, oneMapVal) in oneMap) {
if (twoMap.containsKey(oneMapKey)) {
val twoMapVal = twoMap[oneMapKey]
if (twoMapVal == oneMapVal) {
//do more stuff
}
//do more stuff, I have more if statements
}
}
}
To avoid having more nested if statements, I was wondering if instead I could get rid of the
if (twoMap.containsKey(oneMapKey))
check. if twoMap
doesn't contain the oneMapKey
, we get a null object, and my code still works fine. I was wondering if this is considered bad convention though
fun exampleFunc(oneMap: Map<String, Any>, twoMap: Map<String, Any>) {
for((oneMapKey, oneMapVal) in oneMap) {
val twoMapVal = twoMap[oneMapKey]
if (twoMapVal == oneMapVal) {
//do more stuff
}
//do more stuff, I have more if statements
}
}
question from:
https://stackoverflow.com/questions/65602476/is-it-considered-bad-convention-when-in-iterating-through-two-maps-i-dont-chec 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…