DanielPerezJensen/mapc-uva

Should we treat the dictionary of nodes in graph as a defaultdict instead of a normal dict?

DanielPerezJensen opened this issue · 1 comments

Currently when we retrieve a node from the beliefs (graph) that doesn't yet exist we either get None or a node (from self.beliefs.get_node(coords)). But wouldn't it make more sense to simply treat the dictionary of nodes as a default dictionary, with a default value of an empty Node object? Like so:

from collections import defaultdict

self.nodes = defaultdict(Node)

Then now if we access the self.nodes dictionary for a key that does not yet exist it generates an empty node object and returns that instead of None or yielding an error.

We could do that but, a) an empty node isn't connected to its neighbours and navigation makes use of what the neighbours of a node are. And b) most of the time when a node isn't found that means that there is something wrong in the code (at least in the graph.py), in this case a KeyError is more useful than the program creating it's own node.

What I could do is improve beliefs.get_node(coords), so that when a node isn't found it does return a new node (which (if possible) will be connected to its neighbours). This way you get the same result but we hold a bit more control.