So it's a pretty easy principle generally things (API's and the like) should generally do the least surprising thing. Reactions like 'What the f***' are a pretty good example of this principle being broken. Imagine my suprise when I found what the following groovy does:
def map = ["key1":"value1"]
map.get("missingKey","defaultValue")
print map
Output:
[key1:value1, missingKey:defaultValue]
The get with default method actually updates the map!
Surely that can't be a good thing to have a 'get' method update the underlying map?
// the preferred groovy idiom is to use the elvis operator
print map["missingKey"]?:"defaultValue"
No comments:
Post a Comment