Introduction to Software Engineering: Tools and Environments

Session 6

Oded Lachish

Room: Mal 405 Visiting Hours: Wednesday 17:00 to 20:00 Email: [email protected] Module URL: http://www.dcs.bbk.ac.uk/~oded/Tools2012-2013/Web/Tools2012-2013.html

1 y Clone project from

git://.com/odedlac/Verification.git

2 Until now

• High level overview of the SDLC • Welcome to –Refactor, Source • Version control –git • Unit testing – Junit • Mocking –mockito

Hopefully by now we know how to mock software. But what if there is a bug? “println” –we don’t want to touch the code 3 Eclipse has a plug‐ins

• Debugger

• Code coverage

• Static code analysis

4 Debugging

5 Clone project from

git://github.com/odedlac/Verification.git

6 Debugger

Press F11

7 Debugger View

8 Set a breakpoint

• Pick line

• Add breakpoint

9 Notice the hotkeys Breakpoint

• Right click menu

10 Debug Local variable values

Stopped at breakpoint

11 Debug Local variable values

Stopped at breakpoint

12 Debug Local variable values

Stopped at breakpoint

13 After breakpoint reached –execution suspended

Two options

Before resume After resume

14 Step into (the method)

Then step return, what does step over do?

15 What else can we do?

Pick code and right mouse click

16 Options

• All instances ‐ list of all java object of selected type • Instance count – number of instances of selected type • Force return –forces a return with a specified value • Watch –shows contents on “expressions “ • Inspect –shows contents • Display –shows value pop‐up dialog • Run to line –run until the line selected (when we don’t wont a breakpoint)

17 To do now

• Change code so that you can easily see how things behave.

• Try to find bug

18 Code Coverage ecorbetura

http://ecobertura.johoop.de/update/

19 Code Coverage How do you know you have tested enough. (At least in theory it seems you never know) However one popular metric is code coverage.

The idea is to check for example • All lines of code were executed • All conditions in the code evaluated to every possible value Naturally this process is automated.

20 ecorbetura

• Open coverage tool • Reports which lines are covered • Reports which branches were covered • Reports line coverage percentage • Reports branch coverage percentage

21 Tell Eclipse to use coverage

22 After we ran test

Green is covered Red not covered

23 Coverage report

Go here

24 Coverage report

Choose this

25 Coverage report

26 Now Get all These Colours Out of My Code

Have you tried turning it of and on again?

27 Static Code Analysis FindBugs

28 FindBugs

http://ecobertura.johoop.de/update/

29 FindBugs (http://findbugs.cs.umd.edu/eclipse/)

What: Static code analyzer (does not execute code) Why: find java “bugs” and “bad coding” examples: • Comparing objects instead of using equals • null pointers. String name;

if (name == null || name.length() == 0) return;

In this case “name” may never be null.

30 FindBugs

31 FindBugs results

32 PMD

http://pmd.sourceforge.net/eclipse

33 PMD (http://pmd.sourceforge.net/eclipse)

What: Static code analyzer (does not execute code) Why: find java “bugs” and “bad coding” examples:

• Possible bugs ‐ empty try/catch/finally/switch statements • Dead code ‐ unused local variables, parameters and private methods • Suboptimal code ‐ wasteful String usage • Overcomplicated expressions ‐ unnecessary if statements, for loops that could be while loops • Duplicate code ‐ copied/pasted code means copied/pasted bugs

34 PMD

35 PMD challenge

Change your code so that PMD shouts

36 checkStyle

http://eclipse-cs.sf.net/update/

37 checkstyle

What: Static code analyzer (does not execute code) Why: make sure that the style of code adheres to standards: Annotations Block Checks Class Design Coding Duplicate Code Headers Imports Javadoc Comments Metrics Miscellaneous Modifiers Naming Conventions Regexp Size Violations Whitespace 38 checkstyle

What else? checkstyle can be extended

The functionality of checkstyle is implements as checks. You need to know how to write checks to write new functionality.

39 checkStyle

40 checkstyle challenge

Change your code so that checkstyle is happy

41