Top Banner
Best Practices in Java Development JA-SIG Summer Conference Denver, CO June 24 – 27, 2007
40

Best Practices in Java Development JA-SIG Summer Conference Denver, CO June 24 – 27, 2007.

Dec 23, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Best Practices in Java Development JA-SIG Summer Conference Denver, CO June 24 – 27, 2007.

Best Practices in Java Development

JA-SIG Summer ConferenceDenver, CO

June 24 – 27, 2007

Page 2: Best Practices in Java Development JA-SIG Summer Conference Denver, CO June 24 – 27, 2007.

JA-SIG Summer Conference – June 24 – June 27, 2007

Best Practices in Java Development

Who am I?

• I’m Scott Battaglia!

• Application Developer @ Rutgers

• Java Developer for 5+ Years

• Lead Developer/Architect on JA-SIG CAS

• Committer to Acegi Security

Page 3: Best Practices in Java Development JA-SIG Summer Conference Denver, CO June 24 – 27, 2007.

JA-SIG Summer Conference – June 24 – June 27, 2007

Best Practices in Java Development

Agenda

• Methodologies• Language Features• Non Language Specific• Tool Chest• Discussion

Page 4: Best Practices in Java Development JA-SIG Summer Conference Denver, CO June 24 – 27, 2007.

JA-SIG Summer Conference – June 24 – June 27, 2007

Best Practices in Java Development

1.Methodologies

Page 5: Best Practices in Java Development JA-SIG Summer Conference Denver, CO June 24 – 27, 2007.

JA-SIG Summer Conference – June 24 – June 27, 2007

Best Practices in Java Development

Test Driven Development

• Writing a test case and implementing only code necessary to pass test

• A method of designing software, not merely a method of testing

• Can still produce crappy code

• Unit vs. Integration Tests

• Useful when used judiciously

Page 6: Best Practices in Java Development JA-SIG Summer Conference Denver, CO June 24 – 27, 2007.

JA-SIG Summer Conference – June 24 – June 27, 2007

Best Practices in Java Development

Test Driven Development

• Three rules from “Uncle Bob”

– You are not allowed to write any production code unless it is to make a failing unit test pass.

– You are not allowed to write any more of a unit test than is sufficient to fail; and compilation failures are failures.

– You are not allowed to write any more production code than is sufficient to pass the one failing unit test.

• http://butunclebob.com/ArticleS.UncleBob.TheThreeRulesOfTdd

Page 7: Best Practices in Java Development JA-SIG Summer Conference Denver, CO June 24 – 27, 2007.

JA-SIG Summer Conference – June 24 – June 27, 2007

Best Practices in Java Development

Test Driven Development

• What makes a good unit test?– Run fast (they have short setups, run times, and break downs).

– Run in isolation (you should be able to reorder them).

– Use data that makes them easy to read and to understand.

– Use real data (e.g. copies of production data) when they need to.

– Represent one step towards your overall goal.

Page 8: Best Practices in Java Development JA-SIG Summer Conference Denver, CO June 24 – 27, 2007.

JA-SIG Summer Conference – June 24 – June 27, 2007

Best Practices in Java Development

Domain Driven Design

• The premise of domain-driven design is two-fold:

– For most software projects, the primary focus should be on the domain and domain logic; and

– Complex domain designs should be based on a model.

Page 9: Best Practices in Java Development JA-SIG Summer Conference Denver, CO June 24 – 27, 2007.

JA-SIG Summer Conference – June 24 – June 27, 2007

Best Practices in Java Development

Domain Driven Design

• Business people talk naturally in Business terms (using ubiquitous language):

- Open grading period, Pending class roster, Student eligible for grading, Course grading policy, etc.

• Objects shift language from data to behavior- Away from flags and if conditions- Toward responsibilities and naming

Page 10: Best Practices in Java Development JA-SIG Summer Conference Denver, CO June 24 – 27, 2007.

JA-SIG Summer Conference – June 24 – June 27, 2007

Best Practices in Java Development

Domain Driven Design

• Models the business domain concepts

• Maps to Screens and DB

• Easier to test, and change

• Reusable (different application clients)

Page 11: Best Practices in Java Development JA-SIG Summer Conference Denver, CO June 24 – 27, 2007.

JA-SIG Summer Conference – June 24 – June 27, 2007

Best Practices in Java Development

Domain Driven Design

• Business logic can be complex

- Rules in Domain Model (DM) describe the many cases & variations

• DM creates a web of interconnected objects where each represents a meaningful concept some as large as an entire company or as small as a person’s name

Page 12: Best Practices in Java Development JA-SIG Summer Conference Denver, CO June 24 – 27, 2007.

JA-SIG Summer Conference – June 24 – June 27, 2007

Best Practices in Java Development

Iterative Development

• Cyclic/incremental

• Milestones, Release Candidates…

• Feedback before its too late!

• Allows you to take advantage of previously gained knowledge

Page 13: Best Practices in Java Development JA-SIG Summer Conference Denver, CO June 24 – 27, 2007.

JA-SIG Summer Conference – June 24 – June 27, 2007

Best Practices in Java Development

Code Reviews

• Systematic examination of course code

• Goals– Improve quality of code– Share knowledge

• Ideally…– Short– Focused on code

• Many ways of doing it

Page 14: Best Practices in Java Development JA-SIG Summer Conference Denver, CO June 24 – 27, 2007.

JA-SIG Summer Conference – June 24 – June 27, 2007

Best Practices in Java Development

Aspect Oriented Programming

• Deals with the separation of concerns (cross-cutting concerns)

• Breaking down a program into distinct parts that overlap in functionality as little as possible

• Examples– Transactions, logging, security, ContractByDesign

Page 15: Best Practices in Java Development JA-SIG Summer Conference Denver, CO June 24 – 27, 2007.

JA-SIG Summer Conference – June 24 – June 27, 2007

Best Practices in Java Development

2.Language Features

Page 16: Best Practices in Java Development JA-SIG Summer Conference Denver, CO June 24 – 27, 2007.

JA-SIG Summer Conference – June 24 – June 27, 2007

Best Practices in Java Development

“final” Keyword

• Used as follows:– final Object o = new Object();

• Advantages:– Prevents accidental assigning of variables– Turns logical errors into compile errors– JVM can optimize final constants– Limit Scope of Variables

• Gotchas:– Final primitives and Strings are substituted at compile-time– Final means no variable re-assignment

Page 17: Best Practices in Java Development JA-SIG Summer Conference Denver, CO June 24 – 27, 2007.

JA-SIG Summer Conference – June 24 – June 27, 2007

Best Practices in Java Development

StringBuilder

• Usage:– final StringBuilder builder = new StringBuilder(50);– builder.append(“myString”);– builder.append(“moreOfMyString”);

• StringBuilder vs. StringBuffer vs String

• “Best Guess” StringBuilder size

Page 18: Best Practices in Java Development JA-SIG Summer Conference Denver, CO June 24 – 27, 2007.

JA-SIG Summer Conference – June 24 – June 27, 2007

Best Practices in Java Development

Enumerations

• Traditional Enumerations:– public final int ENUM_VALUE_1 = 1– public final int ENUM_VALUE_2 = 2– …

• Type Safe Enums:– Implementation of Java classes

• Java 5 Enumerations– enum COLOR {BLACK, WHITE, RED, GREEN, BLUE, YELLOW}

Page 19: Best Practices in Java Development JA-SIG Summer Conference Denver, CO June 24 – 27, 2007.

JA-SIG Summer Conference – June 24 – June 27, 2007

Best Practices in Java Development

JPA

• JPA is– Java Persistence API– -Entities– Java Persistence Query Language

• Advantages– Pluggable backend (TopLink, Hibernate, etc.)– Database agnostic DAO layer– No complex mapping files

• Disadvantages– Difficult to retrofit “legacy” tables

Page 20: Best Practices in Java Development JA-SIG Summer Conference Denver, CO June 24 – 27, 2007.

JA-SIG Summer Conference – June 24 – June 27, 2007

Best Practices in Java Development

PreparedStatements

• Statements vs. PreparedStatements

• Advantages:– Compiled– Correctly escape characters– As of JDBC 3, can be pooled

Page 21: Best Practices in Java Development JA-SIG Summer Conference Denver, CO June 24 – 27, 2007.

JA-SIG Summer Conference – June 24 – June 27, 2007

Best Practices in Java Development

Annotations

• Extra information associated with Class, Method, Variables

• Most useful for non-frequently changing information

• Keeps meta data and object together

• Examples:– Spring Transactions– CAS Property Validation

Page 22: Best Practices in Java Development JA-SIG Summer Conference Denver, CO June 24 – 27, 2007.

JA-SIG Summer Conference – June 24 – June 27, 2007

Best Practices in Java Development

3.Non-Language Specific

Page 23: Best Practices in Java Development JA-SIG Summer Conference Denver, CO June 24 – 27, 2007.

JA-SIG Summer Conference – June 24 – June 27, 2007

Best Practices in Java Development

Design Patterns

• Recurring solutions to common problems

• Solve design problems not computational problems

• Five types of Patterns

• Benefits:– Easy to understand code– General solutions– Allow people to communicate using the same language

Page 24: Best Practices in Java Development JA-SIG Summer Conference Denver, CO June 24 – 27, 2007.

JA-SIG Summer Conference – June 24 – June 27, 2007

Best Practices in Java Development

Model-View-Controller

• Division of Labor into the following parts:– Those responsible for business logic (the Model -- often

implemented using Enterprise JavaBeans™ or plain old Java objects).

– Those responsible for presentation of the user interface (the View).

– Those responsible for application navigation (the Controller -- usually implemented with Java servlets or associated classes like Struts controllers).

Page 25: Best Practices in Java Development JA-SIG Summer Conference Denver, CO June 24 – 27, 2007.

JA-SIG Summer Conference – June 24 – June 27, 2007

Best Practices in Java Development

Interfaces

• Interface vs. Abstract Class

• Coding to Interfaces

• Examples– Collections Framework– CAS

Page 26: Best Practices in Java Development JA-SIG Summer Conference Denver, CO June 24 – 27, 2007.

JA-SIG Summer Conference – June 24 – June 27, 2007

Best Practices in Java Development

Logging

• Makes debugging easier

• Everywhere you would have put a System.out, put a logger statement

• log.isXXXXEnabled() vs. log.XXXX(“data”)

• Logging via AOP

Page 27: Best Practices in Java Development JA-SIG Summer Conference Denver, CO June 24 – 27, 2007.

JA-SIG Summer Conference – June 24 – June 27, 2007

Best Practices in Java Development

Exception vs. Runtime Exception

• Two types of Exceptions in Java– Exception (checked)– Runtime Exception (unchecked)

• Checked Exceptions indicate something that can be handled.

• Unchecked Exceptions indicate something that can’t be.

Page 28: Best Practices in Java Development JA-SIG Summer Conference Denver, CO June 24 – 27, 2007.

JA-SIG Summer Conference – June 24 – June 27, 2007

Best Practices in Java Development

Don't Re-Invent the Wheel

• Use Common well know frameworks– Spring, Hibernate, Web Flow, Acegi...

• Don't fall for “not invented here” syndrome

• Balance needs of application vs. library

Page 29: Best Practices in Java Development JA-SIG Summer Conference Denver, CO June 24 – 27, 2007.

JA-SIG Summer Conference – June 24 – June 27, 2007

Best Practices in Java Development

Documentation & Comments

• Documentation is pretty much the bane of all developer’s existences

• Be sure to place comments in code, but don’t put too much– // send the mail message– javaMailSender.send(message);

• You never know when you need to revisit your code– Document methods, classes, configuration

Page 30: Best Practices in Java Development JA-SIG Summer Conference Denver, CO June 24 – 27, 2007.

JA-SIG Summer Conference – June 24 – June 27, 2007

Best Practices in Java Development

Unit Testing

• Use a Code Coverage tool• Don't Mandate 100% test coverage• Add tests when you see they don't exist• Add tests to prove a bug• Run your tests frequently• Make sure the tests execute quickly• Don’t let the tests become out of date• Don’t throw them away when they stop passing - fix

them!

Page 31: Best Practices in Java Development JA-SIG Summer Conference Denver, CO June 24 – 27, 2007.

JA-SIG Summer Conference – June 24 – June 27, 2007

Best Practices in Java Development

4.Tool Chest

Page 32: Best Practices in Java Development JA-SIG Summer Conference Denver, CO June 24 – 27, 2007.

JA-SIG Summer Conference – June 24 – June 27, 2007

Best Practices in Java Development

Build Tools

Page 33: Best Practices in Java Development JA-SIG Summer Conference Denver, CO June 24 – 27, 2007.

JA-SIG Summer Conference – June 24 – June 27, 2007

Best Practices in Java Development

Wiki

Page 34: Best Practices in Java Development JA-SIG Summer Conference Denver, CO June 24 – 27, 2007.

JA-SIG Summer Conference – June 24 – June 27, 2007

Best Practices in Java Development

Issue Tracking

Page 35: Best Practices in Java Development JA-SIG Summer Conference Denver, CO June 24 – 27, 2007.

JA-SIG Summer Conference – June 24 – June 27, 2007

Best Practices in Java Development

Continuous Integration

Page 36: Best Practices in Java Development JA-SIG Summer Conference Denver, CO June 24 – 27, 2007.

JA-SIG Summer Conference – June 24 – June 27, 2007

Best Practices in Java Development

Version Control

Page 37: Best Practices in Java Development JA-SIG Summer Conference Denver, CO June 24 – 27, 2007.

JA-SIG Summer Conference – June 24 – June 27, 2007

Best Practices in Java Development

Integrated Development Environment

Page 38: Best Practices in Java Development JA-SIG Summer Conference Denver, CO June 24 – 27, 2007.

JA-SIG Summer Conference – June 24 – June 27, 2007

Best Practices in Java Development

5.Discussion

Page 39: Best Practices in Java Development JA-SIG Summer Conference Denver, CO June 24 – 27, 2007.

JA-SIG Summer Conference – June 24 – June 27, 2007

Best Practices in Java Development

Further Reading..

• Thinking in Java by Bruce Eckel

• J2EE Design and Development by Rod Johnson

• Refactoring: Improving the Design of Existing Code by Martin Fowler

• Effective Java by Joshua Bloch

• Domain Driven Design by Eric Evans

Page 40: Best Practices in Java Development JA-SIG Summer Conference Denver, CO June 24 – 27, 2007.

JA-SIG Summer Conference – June 24 – June 27, 2007

Best Practices in Java Development

?What are Your “Best Practices?”