Wednesday, 7 August 2013

Assertions in recursive call?

Assertions in recursive call?

This is a code to check if the graph is bipartite or not. My question is
regarding assertions. I want a check to validate if graph is null or not.
Effective java encourages checks even in private functions. Lets say I add
an assert graph != null, it would be checked as many times the recursive
function is called. This appears inefficient. If the check if done before
recursive function is called, then we violate best practices stated in
effective java, that every function should validate parameters.Is there
some best practice / tradeoff etc? Thanks.
private void dfsBipartiteDetector(Graph graph, int vertex, int i) {
assert graph != null; // <--------- appears inefficient for recursive
call.
visited[vertex] = true;
vertexSets.get(i).add(vertex);
final List<Integer> adjList = graph.adj(vertex);
for (int v : adjList) {
if (!visited[v]) {
dfsBipartiteDetector(graph, v, i == 0 ? 1 : 0);
} else {
if (vertexSets.get(i).contains(v)) {
isBipartite = false;
}
}
}
}

No comments:

Post a Comment