Some coding considerations
Download
Report
Transcript Some coding considerations
Some Coding Considerations
Collection Classes
• Use the collection classes in the java.util
package.
• The type declaration should refer to the
interface, not the implementation.
Yes:
import java.util.List;
...
List views = new ArrayList();
No:
ArrayList views = new ArrayList();
2
Collection Iteration Idioms
• For standard collection instance, c:
for (Iterator i = c.iterator(); i.hasNext; )
{
doSomething( i.next() );
}
• For high-performance iteration over random
access list, list:
for ( int i = 0, n = list.size(); i < n; i++)
{
doSomething( list.get(i) );
}
3
Implementation Order
• For acyclic classes, implement/test
7
classes bottom-up.
6
• The figure is an example.
– Arcs represent uses relation
3
5
4
– Node numbers indicate an ordering.
2
1
4
Implementation Order …
For cyclic classes,
7
test nodes referring to each other at
the same time.
6
or
1. Test aspects of 4 that do not refer to 5
3
2. Test aspects of 5 that do not refer to 4.
5
4
3. Test aspects of 4 that refer to 5.
4. Test aspects of 5 that refer to 4.
2
1
5
Test-First
• The general class development
algorithm is approximately as follows:
while ( ! class.isComplete )
{
write a little test code;
write the corresponding production code;
}
6
Testing …
• Keep a class’s unit test code in either:
– The class’s main method
• Quick & simple
• Not optimized for regression testing
• Increases class’s size, even when not testing.
– A separate class.
• Our Resources page links to http://www.junit.org/.
The Junit Cookbook.
7