BIO PRESENTATION

W6

6/20/2007 11:30:00 AM

"IMPROVING CODE QUALITY WITH AND ITS JAVA PLUG-INS"

Levent Gurses Stelligent

Better Software Conference & EXPO June 18-21, 2007 Las Vegas, NV USA

Levent Gurses

As a Solutions Director for Stelligent Incorporated, Levent helps customers design and implement continuous integration environments, automated build environments and test driven development. His primary responsibilities include the development and implementation of Stelligent's strategic array of software quality products and services. Levent is an IBM and Sun Certified Architect for J2EE and is a published author specializing in software quality and Agile methods.

Improving Code Quality with Eclipse and its Java Plug-ins Getting Started

© 2007, Levent Gurses 2 © 2007, Paul M. Duvall What is Code Quality?

Code quality is a non-functional requirement which is not called up by the customer's contract, but nevertheless a desirable requirement which enhances the value of a software application.

• Understandability/Readability • Completeness • Portability • Maintainability • Testability •Usability • Reliability •Security • Performance

© 2007, Levent Gurses 3 © 2007, Paul M. Duvall Code Analysis

• Coding standards • Code duplication • Test coverage • Dependency analysis • Code complexity

© 2007, Levent Gurses 4 © 2007, Paul M. Duvall Coding Standards

• 80% of an application’s lifetime cost goes to maintenance • Hardly any software is maintained for its whole life by its original author • Coding standards improve the readability and understandability of the software, allowing engineers to crank new code more quickly and thoroughly • If you ship your as a product, you need to make sure it is as well packaged and clean as any other product you create

© 2007, Levent Gurses 5 © 2007, Paul M. Duvall Coding Standards Examples • Empty Catch Blocks • Returns from Finally Blocks • Use StringBuffer for String Appends • Avoid Instantiating Objects in Loops • Loose Coupling – usage of HashSet vs. Set • Variable Naming Conventions • If Statements Must Use Braces • Test Class Without Test Cases

© 2007, Levent Gurses 6 © 2007, Paul M. Duvall Coding Standards: Eclipse

• Built-in code analysis tool • Detects most coding violations out of the box • Three violation levels - Error, Warning, Ignore • Results displayed in the Problems view

© 2007, Levent Gurses 7 © 2007, Paul M. Duvall Coding Standards: PMD • Static analysis tool for Java • Scans Java source code and looks for potential problems like: – Possible bugs - empty try/catch/finally/switch statements – Dead code - unused local variables, parameters and private methods – Suboptimal code - wasteful String/StringBuffer usage – Overcomplicated expressions - unnecessary if statements, for loops that could be while loops

© 2007, Levent Gurses 8 © 2007, Paul M. Duvall Coding Standards: Checkstyle • Similar to PMD • Highly configurable • Checks for – Class design problems – Duplicate code – Bug patterns

© 2007, Levent Gurses 9 © 2007, Paul M. Duvall Coding Standards: FindBugs • Uses static analysis to inspect Java bytecode • Looks for bug patterns in code • Bug patterns arise for a variety of reasons: – Difficult language features – Misunderstood API methods – Misunderstood invariants when code is modified during maintenance – Garden variety mistakes: typos, use of the wrong boolean operator

© 2007, Levent Gurses 10 © 2007, Paul M. Duvall Code Analysis

• Coding standards • Code duplication • Test coverage • Dependency analysis • Code complexity

© 2007, Levent Gurses 11 © 2007, Paul M. Duvall Code Duplication • Code gets duplicated when: – Copy and paste programming - "because it works“ – Functionality that is very similar to that in another part of a program is required and a developer independently writes code that is very similar to what exists elsewhere • Don’t Repeat Yourself (DRY) – Detect Copy and Paste Code – Refactor duplicate code – Develop indexed code meta library

© 2007, Levent Gurses 12 © 2007, Paul M. Duvall Code Duplication: CPD • Comes with PMD • Detects Copy and Paste code • Supports Java, /C++, JSP, PHP, Ruby • Configurable through Eclipse preferences

© 2007, Levent Gurses 13 © 2007, Paul M. Duvall Code Analysis

• Coding standards • Code duplication • Test coverage • Dependency analysis • Code complexity

© 2007, Levent Gurses 14 © 2007, Paul M. Duvall Test Coverage • Why bother testing? • What should be tested? • How much is enough? • Test coverage - – Statement coverage – Branch coverage – Path coverage – Block coverage

© 2007, Levent Gurses 15 © 2007, Paul M. Duvall Test Coverage: JUnit in Eclipse

© 2007, Levent Gurses 16 © 2007, Paul M. Duvall Test Coverage: Coverlipse • Direct feedback in the Eclipse Java Editor • All Uses Coverage • Block Coverage (aka. Statement Coverage) • One test run enough for evaluation of all coverage criteria • Simple configuration - done the same way as normal JUnit in Eclipse • Include/exclude packages from the test

© 2007, Levent Gurses 17 © 2007, Paul M. Duvall Code Analysis

• Coding standards • Code duplication • Test coverage • Dependency analysis • Code complexity

© 2007, Levent Gurses 18 © 2007, Paul M. Duvall Dependency Analysis • Number of classes in a package • Abstractness • Instability • Afferent Coupling • Efferent Coupling • Distance from the main sequence • Package cycles

© 2007, Levent Gurses 19 © 2007, Paul M. Duvall Dependency Analysis: JDepend4Eclipse

© 2007, Levent Gurses 20 © 2007, Paul M. Duvall Code Analysis

• Coding standards • Code duplication • Test coverage • Dependency analysis • Code complexity

© 2007, Levent Gurses 21 © 2007, Paul M. Duvall Code Complexity

• Number of Classes/Interfaces • Number of Children • Depth of Inheritance Tree (DIT) • Number of Overridden Methods (NORM) • Number of Methods (NOM) • McCabe Cyclomatic Complexity • Weighted Methods per Class (WMC) • Lack of Cohesion of Methods (LCOM*)

© 2007, Levent Gurses 22 © 2007, Paul M. Duvall Code Complexity: Metrics Plugin for Eclipse

© 2007, Levent Gurses 23 © 2007, Paul M. Duvall Resources

• PMD - http://pmd.sourceforge.net • Coverlipse - http://coverlipse.sourceforge.net • Metrics - http://metrics.sourceforge.net • Checkstyle - http://eclipse-cs.sourceforge.net •FindBugs -http://findbugs.sourceforge.net • Improving code with Eclipse plugins – Paul Duvall - http://www- 128.ibm.com/developerworks/java/library/j-ap01117/index.html • Code quality for software architects – Andrew Glover - http://www-128.ibm.com/developerworks/java/library/j- cq04256/index.html • Improving Code Quality with PMD and Eclipse – Jacoozi - http://www.jacoozi.com/index.php?option=com_content&task=view &id=121&Itemid=134

© 2007, Levent Gurses 24 © 2007, Paul M. Duvall Additional Inspection Tools

•JavaNCSS - http://www.kclee.de/clemens/java/javancss •FindBugs - http://findbugs.sourceforge.net •JCoverage - http://www.jcoverage.com •Cobertura - http://cobertura.sourceforge.net •Simian - http://www.redhillconsulting.com.au/products/simian •Jester - http://jester.sourceforge.net •Code Analysis Plugin http://sourceforge.net/projects/cap4e •JLint - http://jlint.sourceforge.net •Hammurapi - http://www.hammurapi.biz •Jupiter - http://csdl.ics.hawaii.edu/Tools/Jupiter •XRadar - http://xradar.sourceforge.net •QALAB - http://qalab.sourceforge.net •Emma - http://emma.sourceforge.net •JBlanket - http://csdl.ics.hawaii.edu/Tools/JBlanket •Macker - http://innig.net/macker

© 2007, Levent Gurses 25 © 2007, Paul M. Duvall Thank you!

• My weblog is at www.testearly.com/category/gurses • Reach me at [email protected]

Questions & Answers

© 2007, Levent Gurses 26 © 2007, Paul M. Duvall