Posts

Showing posts from 2011

Affect of Implicity vs. Explicit Exception Handling to the Code Quality

In this post I will try to compare 3 different implementations of the same method which are equivalent with respect to the set of unit tests they are expected to pass. The comparison is mainly based on the way exceptions are handled in each and I am comparing them with respect to how they impact code quality. Here is the method we are expected to implement: /** * Should push given element to stack with given stack no. * Should throw a RuntimeException if the capacity is full. **/ public void push( int stackNo, T element ) { ... } A part of the unit test that is written for this method is as follows: // Available capacity at this point is 2: ms.push( 10, e1); ms.push( 10, e1); //becomes full at this point. try { ms.push( 10, e1); assertTrue( false ); } catch( RuntimeException e ) { assertTrue( true ); } Here are 3 alternative implementations that pass the tests: 1) public void push( int stackNo, T element ) { elements[num_elements] = (StackRecord) new StackRecord(); elements[num_ele