SOFT1902 Software Development Tools Announcement SOFT1902 Quiz 1 in lecture NEXT WEEK School of Information Technologies 1 2 Today s Lecture Yes: we have evolved to the point of using tools Version Control Automatic Documentation Managing Testing Debugging IDEs 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 3 4 Multiple iterations What is Version Control Software emerges in many stages and iterations. This might be from agile development, discovery of bugs, extensions, and keeping up with changing environments. 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. 5 6 1
Flavours of version control Models of version control Microsfot Word track changes Keeping multiple copies CVS (Concurrent Version System) Subversion (not an acronym). Visual SourceSafe...and others lock-modify-unlock copy-modify-merge 7 8 File system File sharing In a simple file system where anyone reads and writes it is not possible to keep track of versions, despite the best of intentions. A situation to avoid! With this model, developers can undo each other s work. 9 10 Lock-Modify-Unlock Copy-Modify-Merge 1 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). Two developers check out the same file & make changes. Once one commits the changes, the other is out of date. 11 12 2
Copy-Modify-Merge 2 Consistency through VC The versions are compared and merged by a developer; Then it is published for all. 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? 13 14 CVS or SVN? Further benefits 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). Version Control also enables roll-backs to earlier versions when something really goes wrong! You can use it for your text files too! 15 16 handy places to look CVS: homepage http://www.nongnu.org/cvs/ manual http://ximbiot.com/cvs/manual/ Automatic Documentation SVN: Javadoc and Doxygen homepage http://subversion.tigris.org/ book http://svnbook.red-bean.com/ 17 18 3
When you remember about documentation What to automatically document 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 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. 19 20 Commenting practise 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. 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) 21 22 Automatic documentation example: Powers.java Using Javadoc 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) 23 ~> 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... ~> 24 4
hierarchy graphs Javadoc creates html pages describing your whole program, with all inheritance, methods, variables, and comments... This is a file hierarchy for some C++ code 25 26 hierarchy graphs This is a class hierarchy for some C++ code Doxygen also handles pre- and post- conditions. 27 28 LaTeX Managing Testing because you can't prove the nonexistence of bugs Doxygen can produce LaTeX and rtf documents too. 29 30 5
Contents Terminology 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) 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. 31 32 Unit Testing Verifying the output 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. 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 x 2 = 2. x = 1.4142 2 = 1.99996164 2 mysqrt fails the test. 33 34 Oracles Rounding error 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! 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. 35 36 6
Numeric class Testing suite and regression 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; 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. 37 38 Make it easy on yourself JUnit 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 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. 39 40 JUnit example JUnit output import junit.framework.testcase; JUnit runs all the tests and returns a summary. 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... source:http://www.netbeans.org/images/articles/freeform-import/pmd_junit.png 41 42 7
Contents Debugging you and your Mortein 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 43 44 General Advice Tracing 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). 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. 45 46 Judicious use of println Logger 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. 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! 47 48 8
assert 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) The assert keyword will cause a runtime 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. 49 50 Debuggers Standard debugging features 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. breakpoint: a position in code where execution 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). 51 52 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!) Building Big Projects when vi(m) and emacs aren't enough 53 54 9
ant ant build file 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 <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) 55 56 using ant BlueJ Once your build file is correct with all dependencies, type ant <target> e.g. ant clean ant compile...and sit back. 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. 57 58 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. 59 60 10
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 61 62 Good for multiple large projects 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 63 64 And for next week... Monday: GUIs Thursday: SOFT1902 Quiz! Yay! on all that stuff up to last week 65 11