Consider the following two functions in a class called Graph. The entire source code is found here: http://www.keithschwarz.com/interesting/code/?dir=dijkstra.
public void addNode(T node) {
mGraph.put(node, new HashMap<T, Double>());
}
public void addEdge(T start, T dest, double length) {
mGraph.get(start).put(dest, length);
}
Here, the addEdge
method is blindly trusting addNode
method that it has added hashmap
to mGraph. Is it a common practice to trust that other methods in class are doing their job correctly ? Or is it recommended that a method be skeptical of everything and do a check something like:
public void addEdge(T start, T dest, double length) {
Map m = mGraph.get(start)
if ( m ! = null) ... ...
}
Once again, I am interested in knowing whats commonly done
and whats ideally recommended
.