1
SOFT1902
Software Development Tools
School of Information Technologies
SOFT1902 © The University of Sydney, 2005
2
Announcement
SOFT1902 Quiz 1 in lecture NEXT WEEK
SOFT1902 © The University of Sydney, 2005
3
Today’s Lecture
Yes: we have evolved to the point of using tools… Version Control Automatic Documentation Managing Testing Debugging IDEs 4
Version Control
“If C gives you enough rope to hang yourself with, think of Subversion as a sort of rope storage facility”
– Brian W. Fitzpatrick, one of the authors of Subversion; http://www.red-bean.com/fitz/software.shtml
SOFT1902 © The University of Sydney, 2005
5
Multiple iterations
Software emerges in many stages and iterations. This might be from
agile development, discovery of bugs, extensions, and
keeping up with changing environments.
SOFT1902 © The University of Sydney, 2005
6
What is Version Control
Version control is one of the key tools of group software development: it enables developers to
keep track of releases,
share code among themselves, and
archive code – and any other text files (e.g. documentation, LaTeX...) It is very very useful.
SOFT1902 © The University of Sydney, 2005
7
Flavours of version control
Microsfot Word “track changes” Keeping multiple copies
CVS (Concurrent Version System) Subversion (not an acronym). Visual SourceSafe ...and others
SOFT1902 © The University of Sydney, 2005
8
Models of version control
lock-modify-unlock copy-modify-merge
SOFT1902 © The University of Sydney, 2005
9
File system
In a simple file system where anyone reads and writes it is not possible to keep track of versions, despite the best of intentions.
SOFT1902 © The University of Sydney, 2005
10
File sharing
A situation to avoid!
With this model, developers can undo each other’s work.
SOFT1902 © The University of Sydney, 2005
11
Lock-Modify-Unlock
Developers lock files, make changes, then commit them. This can be
slow,
unnecessary (if both
developers are working on different parts of the file) and
misleading:
dependencies can be violated (if one person uses a file someone else is changing).
SOFT1902 © The University of Sydney, 2005
12
Copy-Modify-Merge 1
Two developers check out the same file & make changes. Once one commits the changes, the other is ‘out of date’.
SOFT1902 © The University of Sydney, 2005
13
Copy-Modify-Merge 2
The versions are compared and merged by a developer; Then it is published for all.
SOFT1902 © The University of Sydney, 2005
14
Consistency through VC
Some installations of version control can force
coding styles
JStyle is an automated tool for following conventions such as
http://www.geosoft.no/development/javastyle.html (but costs money)
unit testing
… but you shouldn’t need these. Should you?
SOFT1902 © The University of Sydney, 2005
15
CVS or SVN?
CVS = Concurrent Version System SVN = Subversion
Both have advantages but SVN is preferred (by me):
plain text archives (as of version 1.2) can move folders/directories
slightly nicer more ‘esoteric’ features like
branching (not detailed here).
SOFT1902 © The University of Sydney, 2005
16
Further benefits
Version Control also enables roll-backs to earlier versions – when something really goes wrong!
You can use it for your text files too!
SOFT1902 © The University of Sydney, 2005
17
handy places to look
CVS: homepage http://www.nongnu.org/cvs/ manual http://ximbiot.com/cvs/manual/ SVN: homepage http://subversion.tigris.org/ book http://svnbook.red-bean.com/ 18
Automatic Documentation
SOFT1902 © The University of Sydney, 2005
19
When you remember about
documentation…
Automatic documentation is very useful for saving time. You put comments in your code; you should use them.
/**
Javadoc comments look like this. (And so do Doxygen comments.) */
/// one-liner comments can be handy
SOFT1902 © The University of Sydney, 2005
20
What to automatically document
Not everything!But anything that is needed by a client (programmer) who is calling the public methods in your code:
@param and @return explain the meaning of arguments and return values.
@pre and @post conditions are essential pieces of knowledge, not just for collaborative projects.
SOFT1902 © The University of Sydney, 2005
21
Commenting practise
Comments should clarify and explain. Scalability needs to be calculated for method calls: it's easier when they're all documented. It is not true that good code should need no comments!
It is not true that everything should be commented.
SOFT1902 © The University of Sydney, 2005
22
Commenting practise
int putPowers(double x, int n, double[] result) /**
Put positive powers of the input number n into result.
@pre x is positive or zero, n is positive, result is of length n.
@post result[i] contains x^(i+1), for i = 0..(n-1).
*/ {
double current = x; // assign current to value x for (int i = 0; i < n; i++) {
result[i] = current;
current = current * x; // now current = x^(i+1)
} }
SOFT1902 © The University of Sydney, 2005
23
import java.util.*; public class Powers { public Powers() {}
int putPowers(double x, int n, double[] result) /**
Put positive powers of the input number n into result. @pre x is positive or zero, n is positive, result is of length n.
@post result[i] contains x^(i+1), for i = 0..(n-1).
*/ {
double current = x; // assign current to value x for (int i = 0; i < n; i++) {
result[i] = current;
current = current * x; // now current = x^(i+1)
} } }
Automatic documentation example: Powers.java
SOFT1902 © The University of Sydney, 2005
24
Using Javadoc
~> emacs Powers.java
~> javadoc Powers.java
Loading source file Powers.java... Constructing Javadoc information... Standard Doclet version 1.5.0_06
Building tree for all the packages and classes... Generating Powers.html...
Generating package-frame.html... Generating package-summary.html... Generating package-tree.html... Generating constant-values.html...
Building index for all the packages and classes... Generating overview-tree.html...
Generating index-all.html... Generating deprecated-list.html... Building index for all classes... Generating allclasses-frame.html... Generating allclasses-noframe.html... Generating index.html... Generating help-doc.html... Generating stylesheet.css... ~>
SOFT1902 © The University of Sydney, 2005 25 Javadoc creates html pages describing your whole program, with all inheritance, methods, variables, and comments...
SOFT1902 © The University of Sydney, 2005
26
hierarchy
graphs
This is a file hierarchy for some C++ codeSOFT1902 © The University of Sydney, 2005
27
hierarchy
graphs
This is a class hierarchy for some C++ codeSOFT1902 © The University of Sydney, 2005
28
Doxygen also handles pre- and post- conditions.
SOFT1902 © The University of Sydney, 2005
29
LaTeX
Doxygen can produce LaTeX and rtf documents
too.
30
Managing Testing
because you can't prove the non-existence of bugs
SOFT1902 © The University of Sydney, 2005
31
Contents
Terminology: Black box, White box (e.g., unit testing), Test coverage
Unit testing
Assertion (v useful) Testing harness
Test cases should be easy to re-use (Advanced Topic 10.1 in Big
Java (2nd ed))
Numeric class example (pg 374) Oracles
The test suite Regression testing Testing Tools:
JUnit (free from http://junit.org), built in to BlueJ and Eclipse)
SOFT1902 © The University of Sydney, 2005
32
Terminology
Black box testing:
don't look inside the code akin to beta-release
White box:
look inside and use program structure
includes Unit Testing (if you actually look inside!)
Test coverage:
the proportion of a program's code that has been
tested.
SOFT1902 © The University of Sydney, 2005
33
Unit Testing
A unit test is test of a single method or closely related group of methods.
Unit tests typically have a wrapper called a "harness".
When you write a method you may as well write your unit test at the same time. JUnit can help you here by providing a simple and convenient interface to your testing methods.
SOFT1902 © The University of Sydney, 2005
34
Verifying the output
How to test your method is working? Suppose you are solving an equation (for x): then just substitute the value of x into that equation and see if it's true:
testing mysqrt(2.0), equation is x2 = 2. x = 1.41422 = 1.99996164 2 mysqrt fails the test.
SOFT1902 © The University of Sydney, 2005
35
Oracles
An alternative is to find an oracle: another method that works, which is more reliable (though may be slower). E.g., compare mysqrt(2.0) with Java built-in pow(2.0, 0.5)
(=1.4142135623730951).
Oracles are not always easy to find!
SOFT1902 © The University of Sydney, 2005
36
Rounding error
Rounding error can suggest spurious bugs: don't be too hasty claiming things are not equal!
You can use approximation where appropriate.
BigJava provides a Numeric class you may find useful.
SOFT1902 © The University of Sydney, 2005
37
Numeric class
public class Numeric { /** From Big Java pg 374 */
public static boolean approxEqual(double x, double y) {
final double EPSILON=1E-12; return Math.abs(x-y) <= EPSILON; }
}
SOFT1902 © The University of Sydney, 2005
38
Testing suite and regression
Never throw anything away.
When you create a (unit) test, keep it! Often when you fix something else, an old
bug may creep back...
You can accumulate a suite of tests. Re-running previous tests after changes is called regression testing.
SOFT1902 © The University of Sydney, 2005
39
Make it easy on yourself
Tests need to be easily repeatable. Manual testing of many cases is
slow and difficult to repeat.
You can
hard-code test cases in your testing classes, or read a set of test cases from an input file:
java MyProgram < testdata.txt
SOFT1902 © The University of Sydney, 2005
40
JUnit
JUnit is free from http://junit.org and is built in to BlueJ and Eclipse.
For each class you build, you also build a tester that extends TestCase from the junit.framework package. For each test case define a method beginning with test, like
testSimpleInput.
SOFT1902 © The University of Sydney, 2005
41
JUnit example
import junit.framework.TestCase;
public class RootApproximatorTest extends TestCase { public void testSimpleCase() {
double x = 4;
RootApproximator a = new RootApproximator(x); double r = a.getRoot();
assertTrue(Numeric.approxEqual(r, 2)); }
public testBoundaryCase() { // more testing here... }
// more test cases... }
SOFT1902 © The University of Sydney, 2005
42
JUnit output
JUnit runs all the tests and returns a summary.
43
Debugging
you and your Mortein
SOFT1902 © The University of Sydney, 2005
44
Contents
General advice:
consider boundary (also called 'corner') cases Trace through problem areas
Output messages
Log messages: use Logger class Use a debugger! Eclipse, JSwat
Basic tools
Breakpoints, steps, inspection
Flashy tricks: Conditional breaks
Change variables on the fly
Change code on the fly
SOFT1902 © The University of Sydney, 2005
45
General Advice
It is much easier to prove bugs exist than that they don't!
It is in general not possible to write bug-free code (first time).
SOFT1902 © The University of Sydney, 2005
46
Tracing
One of commonest techniques you will use is tracing through your code.
(Yes, there's a reason we teach you to trace
code!)
You can often find errors quickly this way but you need to pay close attention!
Step through your code slowly and make sure every part is correct.
SOFT1902 © The University of Sydney, 2005
47
Judicious use of println
Another simple debugging method is printing out interim results and data. Put println("...") statements where you suspect there to be errors.
Disadvantages:
you have to take out the printlns later it slows down the program and
can flood you with unnecessary verbiage.
SOFT1902 © The University of Sydney, 2005
48
Logger
You can keep a log file with
Logger.global.info("message");
...which prints by default. Alternatively set
Logger.global.setLevel(Level.OFF);
at the beginning of main().
Don't forget to turn off logging prior to release!
SOFT1902 © The University of Sydney, 2005
49
assert
Java provides an assert keyword:
int putPowers(double x, int n, double[] result) /**
Put positive powers of the input number n into result. @pre x is positive or zero, n is positive, result is of length n. @post result[i] contains x^(i+1), for i = 0..(n-1).
*/ {
assert x >= 0; // we expect x to be non-negative assert n >= 0; // n must not be negative either.
double current = x; // assign current to value x
for (int i = 0; i < n; i++) { result[i] = current;
current = current * x; // now current = x^(i+1)
} }
SOFT1902 © The University of Sydney, 2005
50
assert
The assert keyword will cause a run-time error if the following statement is false.
assert is enabled with
java -enableassertions MyProg java -ea MyProg
otherwise the default is not to check assertions.
SOFT1902 © The University of Sydney, 2005
51
Debuggers
For bigger projects it's good to use a dedicated debugger.
Some IDEs have debuggers built-in; there are other debugger-only applications.
Debuggers give a broad view of your code and enable to you stop wherever you want.
SOFT1902 © The University of Sydney, 2005
52
Standard debugging features
breakpoint: a position in code whereexecution will halt. You can continue on after inspecting the state of variables;
step into: go into methods (the smallest step possible);
step over: go to the next line, over method calls;
step out: go back up the program stack (via the completion of the current method).
SOFT1902 © The University of Sydney, 2005
53
Flashy debugging tricks
conditional breakpoints: set a condition such that if the condition is met, execution pauses.watch points: watch a location in memory (such as for a particular variable) and stop if it changes. SLOW!
change variables: change the value of a variable (dangerous!)
54
Building Big Projects
when vi(m) and emacs aren't enough
SOFT1902 © The University of Sydney, 2005
55
ant
‘ant’ is the ‘make’ of Java. This sentence makes sense. ant is a tool for the creation of big projects:
create an XML description of the project
with all dependencies
SOFT1902 © The University of Sydney, 2005
56
ant build file
<project name="MyProject" default="dist" basedir="."><description> simple example build file </description>
<property name="src" location="src"/> <!-- set global properties for this build --> <property name="build" location="build"/>
<property name="dist" location="dist"/> <target name="init">
<tstamp/> <!-- Create the time stamp -->
<mkdir dir="${build}"/> <!-- Create the build directory structure used by compile --> </target>
<target name="compile" depends="init" description="compile the source " > <javac srcdir="${src}" destdir="${build}"/> <!-- Compile the java code from ${src} into
${build} --> </target>
<target name="dist" depends="compile” description="generate the distribution" > <mkdir dir="${dist}/lib"/> <!-- Create the distribution directory -->
<jar jarfile="${dist}/lib/MyProject-${DSTAMP}.jar" basedir="${build}"/> <!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file -->
</target>
<target name="clean” description="clean up" >
<delete dir="${build}"/> <!-- Delete the ${build} and ${dist} directory trees --> <delete dir="${dist}"/>
</target> </project>
define dependencies among logical components (compile depends on init); define targets to compile larger components (including the complete project)
SOFT1902 © The University of Sydney, 2005
57
using ant
Once your build file is correct with all dependencies, type
ant <target>
e.g.
ant clean ant compile ...and sit back.
SOFT1902 © The University of Sydney, 2005
58
BlueJ
Available from:
http://www.bluej.org/download/download.html
Platforms: Windows, Mac OS X, .jar Features: free, simple to use, can and run bare methods.
SOFT1902 © The University of Sydney, 2005
59
BlueJ summary
BlueJ was conceived at U. Syd, developed at Monash, and is now maintained from Kent and Deakin Universities.
It is specifically for teaching Object Oriented Programming with Java to novices.
It’s not as sophisticated as Eclipse but is easy to use.
SOFT1902 © The University of Sydney, 2005
SOFT1902 © The University of Sydney, 2005
61 SOFT1902 © The University of Sydney, 2005 62
Eclipse
Eclipse is a powerful open-source development environment. Available from
http://www.eclipse.org (current version 3.2) Platforms: just about everything Features
very nice for Java! quite nice for C/C++ uses external compilers built-in debugger Javadoc refactoring
plug-ins for Subversion, other languages
SOFT1902 © The University of Sydney, 2005
63
Good for multiple large projects
SOFT1902 © The University of Sydney, 2005
64
Summary
Version Control
Essential for collaborative projects; SVN is good, use it!
Automatic Documentation Good for APIs
Managing Testing
Test, test, test.. Unit Testing (as in JUnit) Debugging
takes up lots of time Big Projects:
ant, BlueJ, Eclipse
SOFT1902 © The University of Sydney, 2005
65
And for next week...
Monday: GUIs
Thursday: SOFT1902 Quiz! Yay! on all that stuff up to last week