Martin McBride
1 min readMar 3, 2020

--

The original code looks up the value in the dictionary just once, storing the result in a variable. Your alternative looks up the value twice, via get() and via [].

This is a possible source of bugs. For example if the element name was changed to ‘heading’ it is possible that the developer might only change the first occurrence and not realise it needed changing somewhere else. So you would be testing for the existence of one element, then retrieving a different element to print.

It is also usually going to be less efficient, although of course that isn’t always going to be a significant consideration.

With := you get the best of both world, fewer lines of code without repeating potentially expensive operations.

--

--