Session id: 40232 J2EE Development Best Practices: Improving Code Quality Stuart Malkin Senior Product Manager Oracle Corporation
Agenda Why analyze and optimize code? Static Analysis Dynamic Analysis SCM, Building, Testing
Agenda Why analyze and optimize code? Static Analysis Dynamic Analysis SCM, Building, Testing
Why Analyze and Optimize? Catch common problems before entering code base. Extra effort saves time and project cost later Instill best practices/standards across teams Address code quality and performance throughout the entire development cycle
Benefits of Using Oracle JDeveloper 10g for Tuning Integrated features means no tool-swapping Increase productivity Easy to incorporate static, dynamic analysis and tuning as part of development Build better applications faster Optimized for real applications We use JDeveloper to tune JDeveloper
Agenda Why analyze and optimize code? Static Analysis Dynamic Analysis SCM, Building, Testing
Static Analysis Auditing Code Static qualitative analysis of source files Compliance with a set of rules Suggested fixes Metrics Static quantitative analysis of attributes of source files Chidamber-Kemmerer metrics Generally measure code complexity
Audit Coding Conventions http://java.sun.com/docs/codeconv/html/codeco nvtoc.doc.html Java Doc Rules that verify completeness and correctness of doc comments J2EE specification Your own standards!
Metrics Chidamber and Kemerer Metrics Cyclomatic Complexity (CC) Source Lines of Code (SLOC) Weighted Methods per Class (WMC) Response for Class (RFC) Lack of Cohesion of Methods (LCOM) Coupling Between Objects (CBO) Depth of Inheritance Tree (DIT) Number of Children (NOC) Other Metrics Number of Statements (NOS) Executable Statements (EXEC) Comment Percentage (CP) Number of Audit Violations (NAV)
D E M O N S T R A T I O N Audit and Metrics
Agenda Why analyze and optimize code? Static Analysis Dynamic Analysis SCM, Building, Testing
Dynamic Analysis Performance Tuning Needs to run application in VM Profilers Debuggers Code Advisors Investigate use case to provide context sensitive results.
Performance Network, file Input/Output (I/O), database access Memory footprint CPU usage
When to Performance Tune Address performance throughout the entire development cycle Not too early, not too late Tune iteratively 80/20 principle Code Test Tune
How to Measure Performance Stopwatch System.currentTimeMillis() Benchmarks Profilers
What to measure? Time Elapsed time CPU time Memory Usage Garbage Collection VM events Class loading, Class resolving Application specific events
Profilers Local, remote, and distributed profiling Execution time (CPU) Events Memory usage
Execution Profiler Where is my program spending time? Takes samples of execution stack at customizable intervals Shows: Time spent in the method Percentage of samples when method is on top of the execution stack Percentage of samples when method is anywhere on the execution stack Calls to other methods and calls from other methods
Execution Profiler Measure time spent in methods within execution stack
Memory Profiler How is memory being utilized? Takes snapshot of Java heap at customizable intervals Shows: Number of object instances in heap and amount of memory consumed by the objects Difference between number of instances created and freed for each time sample Origins of object allocation (drill-down) Watch memory footprint and detect memory leaks
Memory Profiler
Event Profiler How often does an event occur and how long does it last? Measures the beginning, end, and duration of events Shows: VM events (loading classes, resolving classes, garbage collecting, etc) BC4J events (loading meta-data, connecting to database, executing queries, etc) Custom events you instrument using a simple API
Event Profiler VM Events Class loading Class resolving Garbage Collection Custom Events Execute_Query JDBC_Create_ Statement
Debugger Super-fast debugging HotSwap recompilation with popframe Many performance-related features Locate memory leaks View loaded classes and number of instances Examine Java heap usage Find references to object instances in the heap Force garbage collection while debugging Automatically detect deadlocks Examine status of monitors
UI Debugger Examine the GUI structure and the event sequences Examine application component hierarchy Display component Info in Watches Window Trace Events generated by components Discover listeners on components
UI Debugger
Code Advisor--CodeCoach Analyzes code at runtime Identifies (and fixes) common coding pitfalls Modifier and scope definition Unused variables and fields Object sizing Dead code Inefficient instanceof usage Immutable objects with constant parameters
CodeCoach Fix issues Scope definition Sizing suggestions Memory suggestions CodeCoach identifies and fixes common coding pitfalls
CodeCoach customization
D E M O N S T R A T I O N Dynamic Analysis and Tuning
Agenda Why analyze and optimize code? Static Analysis Dynamic Analysis SCM, Building, Testing
SCM Software Configuration Management (SCM) Contains multiple versions of source code Sometimes called a Version Control System (VCS) Allows Developers to work in parallel Many Commercial and Open Source Tools CVS, Clear Case, Visual Source Safe Oracle SCM, and many others ( too many to list )
Building Many ways to build Shell scripts Ant GNU make Perl scripts All end up invoking a Java compiler As a Java developer, you may like Ant
Testing with JUnit JUnit is an Open Source white box testing framework and test harness Basic building blocks are test cases Test cases may use test fixtures Reusable logic to be shared among test cases Test cases may be combined into test suites Test runners launch test suites and report the results in various ways (GUI/Console/HTML)
D E M O N S T R A T I O N Building and Testing
Next Steps. Recommended sessions Using Oracle JDeveloper and Oracle Application Server 10g in Tandem (session 40253) Setting Up Your Java Development Environment on Linux (session 40257) Recommended demos and/or hands-on labs Develop J2EE applications using the new J2EE framework, Oracle Application Development Framework (Oracle ADF) Design, develop, tune, and debug your SQL and PL/SQL applications in Oracle JDeveloper. See Your Business in Our Software Visit the DEMOgrounds and talk 1-on-1 with experts. Relevant web sites to visit for more information http://otn.oracle.com/products/jdev
otn.oracle.com Join Over 3,000,000 Developers! Free Technical Advice Free Software Downloads http://otn.oracle.com
FREE Develop your career with Oracle s experts Web services, Forms Upgrade to the Web, TopLink, Business Intelligence, Integration, J2EE, Linux Connect with your peers Sign up at http://otn.oracle.com/events/otnworkshop
Q U E S T I O N S A N S W E R S
Reminder please complete the OracleWorld online session survey Thank you.