Download presentation
Presentation is loading. Please wait.
1
COMP 114 Recitation Kimberly Noonan Chris VanderKnyff William Luebke
2
What we’re doing today… Questions about the Test Questions about the Test Questions about Wednesday’s Lecture Questions about Wednesday’s Lecture Recursion Recursion Rubric for Project 4 Rubric for Project 4 Examples of bad code (or, how NOT to get the cleverness/elegance points). Examples of bad code (or, how NOT to get the cleverness/elegance points). Hand back Assignment 3 (for those of you who didn’t get it on Monday or Wednesday). Hand back Assignment 3 (for those of you who didn’t get it on Monday or Wednesday).
3
What is wrong with… abstract class A { public abstract boolean isB(); } class B extends A { public boolean isB() { return true; }} class C extends A { public boolean isB() { return false; }}
4
Here’s a better example… // Recall Assignment 3... Number compute(Vector v) // v stores the expression to evaluate { if (v.size()%2==0) throw new IllegalArgumentException("v has an even number of elements!"); Number n=(Number)v.get(0); for (int i=1;i<v.size();i+=2) { Operator o=(Operator)v.get(i); Number m=(Number)v.get(i+1); if (o.getTokenString().charAt(0)=='+') { n=new Number(n.getValue()+m.getValue()); } else if (o.getTokenString().charAt(0)=='-') { n=new Number(n.getValue()-m.getValue()); } else if (o.getTokenString().charAt(0)=='*') { n=new Number(n.getValue()*m.getValue()); } else if (o.getTokenString().charAt(0)=='/') { n=new Number(n.getValue()/m.getValue()); }} return n; }
5
A better way… // Recall Assignment 3... Number compute(Vector v) // v stores the expression to evaluate { if (v.size()%2==0) throw new IllegalArgumentException("v has an even number of elements!"); Number n=(Number)v.get(0); for (int i=1;i<v.size();i+=2) { Operator o=(Operator)v.get(i); Number m=(Number)v.get(i+1); n=o.operate(n,m); // Use dynamic dispatch... not “instanceof” or equivalent } return n; }
6
What is wrong with… public class StringScanner implements Enumeration { String _s; int _i; String _t; public StringScanner(String s) {_s=s;trimLeadingSpaces();} void trimLeadingSpaces() { for (;_s.length()>0&&_s.charAt(0)==' ';_s=_s.substring(1)) {}} public String nextToken() { for (_i=1;_i<s.length()&&_s.charAt(_i)!=' ';_i++) {}_t=_s.substring(0,_i);_s=_s.substring(_i);trimLeadingSpaces(); return _t; } public boolean hasMoreTokens() { return _s.length()>0; }}
7
A better way… public class StringScanner implements Enumeration { String _s; // Fewer instance variables... _s is the only one we really need. public StringScanner(String s) {_s=trimLeadingSpaces(s);} String trimLeadingSpaces(String s) // This is now a more generic method. { // We could even make it static... for (;s.length()>0&&s.charAt(0)==' ';s=s.substring(1)) {} return s; } public String nextToken() // i and t (or _i and _t) are really local to this method. { int i=1; for (;i<_s.length()&&_s.charAt(i)!=' ';i++) {} String t=_s.substring(0,i); _s=trimLeadingSpaces(_s.substring(i)); return t; } public boolean hasMoreTokens() { return _s.length()>0; }}
8
Complexity is Bad! Fewer variables is more elegant: Fewer variables is more elegant: Fewer states to keep track of. Fewer states to keep track of. Easier to read and think about. Easier to read and think about. Less commenting required! Less commenting required! Programs take less memory with fewer variables. Programs take less memory with fewer variables. It will help down the road: It will help down the road: More complicated programs are composed of simple parts. More complicated programs are composed of simple parts. Multithreading/synchronization issues Multithreading/synchronization issues
9
Complexity is Bad! Smaller code is more elegant: Smaller code is more elegant: Redundant code – avoid! Redundant code – avoid! Takes up more space in memory Takes up more space in memory Is harder to maintain Is harder to maintain In general copying and pasting code is bad. In general copying and pasting code is bad. Simpler code: Simpler code: Less commenting! Less commenting! Easier to understand – every block does one simple task and does it well. Easier to understand – every block does one simple task and does it well.
10
Complexity is Bad! Smaller interfaces are more elegant: Smaller interfaces are more elegant: Fewer interactions between software components. Fewer interactions between software components. Implementing classes need only implement a small set of methods to participate in polymorphic functions. Implementing classes need only implement a small set of methods to participate in polymorphic functions. Smaller classes are more elegant: Smaller classes are more elegant: “Do one thing, and do it well.” “Do one thing, and do it well.” Promotes reuse: Promotes reuse: Other classes can extend or delegate to simple classes more easily. Other classes can extend or delegate to simple classes more easily. Complicated classes may be too specific to reuse. Complicated classes may be too specific to reuse. “Swiss-army knife classes” are complicated. “Swiss-army knife classes” are complicated.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.