...
Coding
...
Standards
...
This
...
page
...
describes
...
standard
...
coding
...
practices,
...
and
...
is
...
intended
...
to
...
be
...
an
...
aid
...
for
...
developers
...
as
...
they
...
write
...
code.
...
Code
...
reviews
...
will
...
be
...
conducted
...
to
...
ensure
...
we
...
are
...
following
...
our
...
standards.
...
General
- DWR (Direct
...
- Web
...
- Remoting)
...
- is
...
- deprecated.
...
- New
...
- projects
...
- should
...
- not
...
- contain
...
- any
...
- references
...
- to
...
- DWR.
...
- All
...
- XML
...
- schema
...
- definition
...
- (XSD)
...
- locations
...
- in
...
- project
...
- XML
...
- files
...
- should
...
- be
...
- declared
...
- without
...
- version
...
- numbers.
...
- Version
...
- numbers
...
- seem
...
- to
...
- make
...
- the
...
- app
...
- look
...
- on
...
- the
...
- internet
...
- for
...
- the
...
- XSD;
...
- omitting
...
- the
...
- version
...
- numbers
...
- forces
...
- the
...
- XSD
...
- bundled
...
- in
...
- the
...
- jar
...
- files
...
- to
...
- be
...
- used.
...
For
...
example,
...
DO
...
THIS:
Code Block |
---|
}
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"
|
DO NOT DO THIS:
Code Block |
---|
{code} {color:#ff0000}{*}{_}DO NOT DO THIS:_{*}{color} {code} xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.3.xsd" ^^^^^ NO!!! {code} h3. |
Web/Controllers
...
- In
...
- a
...
- Spring
...
- MVC
...
- web
...
- application,
...
- controller
...
- classes
...
- should
...
- not
...
- extend
...
- any
...
- Spring
...
- framework
...
- classes.
...
- Controller
...
- classes
...
- should
...
- be
...
- identified
...
- to
...
- Spring
...
- as
...
- a
...
- controller
...
- bean
...
- by
...
- using the @Controller annotation.
...
- Request
...
- URLs
...
- should
...
- be
...
- mapped
...
- to
...
- controller
...
- classes
...
- and
...
- methods
...
- by
...
- using
...
- the
...
- @RequestMapping
...
- annotation.
...
- Generally
...
- speaking,
...
- we
...
- aim
...
- to
...
- configure
...
- controllers
...
- by
...
- using
...
- annotations.
...
- We
...
- should
...
- use
...
- the
...
- @Autowired
...
- annotation
...
- for
...
- injected
...
- dependencies.
...
- As
...
- controllers
...
- are
...
- typically
...
- configured
...
- as
...
- singletons
...
- in
...
- the
...
- Spring
...
- context,
...
- the
...
- classes
...
- should
...
- be
...
- thread-safe.
...
- In
...
- practical
...
- terms,
...
- this
...
- means
...
- fields,
...
- or
...
- instance
...
- variables,
...
- should
...
- never
...
- maintain
...
- any
...
- state,
...
- and
...
- they
...
- should
...
- not
...
- be
...
- instantiated
...
- by
...
- the
...
- new operator.
...
- The
...
- only
...
- instance
...
- variables
...
- in
...
- a
...
- controller
...
- should
...
- be
...
- other
...
- Spring
...
- beans,
...
- injected
...
- by
...
- the
...
- Spring
...
- container.
...
- Typically
...
- these
...
- will
...
- be
...
- service
...
- classes
...
- or
...
- helper
...
- classes.
...
- Controllers
...
- should
...
- not
...
- contain
...
- any
...
- business
...
- logic
...
- -
...
- they
...
- should
...
- be
...
- concerned
...
- with
...
- UI-related
...
- logic.
...
- Business
...
- logic
...
- should
...
- be
...
- delegated
...
- to
...
- service
...
- classes.
...
Service
...
Classes
...
- Service classes should be identified to Spring as a service bean by using the @Service annotation.
- Generally speaking, we aim to configure service classes by using annotations.
- We should use the @Autowired annotation for injected dependencies.
- As service classes are typically configured as singletons in the Spring context, the classes should be thread-safe. In practical terms, this means fields, or instance variables, should never maintain any state, and they should not be instantiated by the new operator. The only instance variables in a controller should be other Spring beans, injected by the Spring container. Typically these will be DAO classes or helper classes or other service classes.
- Service classes should contain only business logic - they should NOT be concerned with the user interface. Neither should they be concerned directly with data retrieval. Data retrieval should be delegated to DAO or other service classes.
DAO Classes
- DAOs do not extend Spring’s HibernateDAOSupport class.
- DAOs should not extend any other class.
- DAOs should include a private instance variable DaoFoundation, injected by Spring via the @Autowired annotation. The DaoFoundation class provides most of the methods necessary to communicate with the Hibernate session. For Hibernate operations not directly supported by DaoFoundation, the Hibernate Session and SessionFactory objects are available via the DaoFoundation.
- DAOs should not use Spring’s HibernateTemplate. HibernateTemplate for Hibernate 3 is deprecated, and does not exist at all in Spring's Hibernate 4 module.
Domain Classes
- Domain classes to be used as Hibernate managed entities should extend AbstractDomainBase. This automatically provides certain attributes (version, create date & user, modify date & user) and allows Hibernate to automatically maintain these attributes.
- Domain classes should normally be very simple POJOs with attributes and accessors. Business logic is typically implemented in service classes.
- Domain class getter methods should never modify attributes.
Java - General
- Use constants declared as static variables rather than hard-coding values in the Java code.
- Place instance variables at the top of the class; getters and setters at the bottom of the class.
- Use log level of “debug” unless there’s a specific reason to use info or error.
- An exception that is intercepted in a "catch" block should be re-thrown, unless there is a specific reason for not re-throwing. As a rule, do not simply have a catch block log a message and continue.
- Methods and classes should be documented accurately with javadoc.
- All methods should have corresponding unit tests.