EXERCISE 3 Comments. | Consider this code. (Online at www.xp123.com/rwb ) Matcher.java public class Matcher { public Matcher() {} public boolean match(int[] expected, int[] actual, int clipLimit, int delta) { // Clip "too-large" values for (int i = 0; i < actual.length; i++) if (actual[i] > clipLimit) actual[i] = clipLimit; // Check for length differences if (actual.length != expected.length) return false; // Check that each entry within expected +/- delta for (int i = 0; i < actual.length; i++) if (Math.abs(expected[i] - actual[i]) > delta) return false; return true; } } MatcherTest.java import junit.framework.TestCase; public class MatcherTest extends TestCase { public MatcherTest(String name) {super(name);} public void testMatch() { Matcher matcher = new Matcher(); int[] expected = new int[] {10, 50, 30, 98}; int clipLimit = 100; int delta = 5; int[] actual = new int[] {12, 55, 25, 110}; assertTrue(matcher.match(expected, actual, clipLimit, delta)); actual = new int[] {10, 60, 30, 98}; assertTrue(!matcher.match(expected, actual, clipLimit, delta)); actual = new int[] {10, 50, 30}; assertTrue(!matcher.match(expected, actual, clipLimit, delta)); } } -
Use Extract Method to make the comments in match() redundant. -
Can everything important about the code be communicated using the code alone? Or do comments have a place? -
Find some code you wrote recently. Odds are good that you commented it. Can you eliminate the need for some of those comments by making the code reflect your intentions more directly? See Appendix A for solutions. |