Friday, August 14, 2009

Enterprise Java - a firmer grasp of the fundamentals

This blog is about Enterprise Java. The word "Enterprise" mainly refers to the fact, that it need some more that just plain
Java to run applications that fulfill enterprise requirements. Beside the fact, that most of them were tried to be adressed
by the Java standard with a nearly equal name (JSR 316) there is still lot to learn about this topic beside the basics.
Here are some hints and best practices for beginners wanting to gain a firmer grasp of the fundamentals.
These tips assume, that you already knwo about OOP and Java in general.

1) Read, read, read, read and understand
The Enterprise Java Specification is incredibly thorough and has truly helpful comments following each included standard.
Before asking questions or trying to figure out an issue on your own, save some time and just head straight to the
documentation. Another good place to look for answers and get a deeper understanding about the collaboration of the
standards is the Duke's Bank Sample application. Also known as the JEE Tutorial. Besid this, it is always a good idea
to have a look at the documentation of the application server of your choice.

- JSR 313 Documentation
- Duke's Bank Tutoral
- Certified Java Application Servers

2) Get used to exceptions and learn to read them
You’ll find bugs in your code that you might not have spotted earlier, as not all bugs keep the application from working.
There are different levels of strictness in the reporting that you can use. Depending on the framework, application server
or JDK you are using, you have to configure error reporting the right way.

- JDK Logging
- Commons Logging
- Log4j

3) Use an enterprise IDE
IDE’s (Integrated Development Environments) are helpful tools for any developer.
While they’re not for everyone, an IDE definitely has its place. IDE’s provide tools like

* syntax highlighting
* code completion
* error and deprocation warnings
* refactoring (reworking)
* appserver integration.

And many other features. There are plenty of great IDEs out there that support Enterprise Java. Have a look and take
your choice:

- Eclipse IDE
- Netbeans
- IntelliJ
- OEPE

4) Try some frameworks
Starting with JEE 5 the specification got much easier to use. Anyway, there are still some concepts that feel to
overengineered. Some frameworks try to fight this and reach the goal of providing enterprise grade applications on
a different track. Beeing a skilled developer, you should have seen some of them. Take your time and experiment with them.

- Springframework
- JBoss Seam

5) Try different O/R Mappers
JEE 5 introduced the Java persistence API (JPA). Beside the reference implementation, that ships with the specification
there are plenty other, very good and stable O/R mappers available out there. Most of them have a moving history and
come with advantages or disadvantages depending on your needs. To use the right O/R mapper, it is worth seeing some.

- Hibernate
- EclipseLink
- OpenJPA

6) Don’t Repeat Yourself (DRY)
DRY stands for Don’t Repeat Yourself, and it’s a valuable programming concept, no matter what the language.
DRY programming, as the name implies, is ensuring that you don’t write redundant code.

7) Think about readable code
If you don’t use indentations and white space in your code, the result looks may look like a painting.
Ensure that your code is readable and easy to search because you’ll most definitely be making changes in the future.
IDEs and advanced text editors can add indentation automatically. Take your time to adjust the default settings to
meet your needs. Have a look at available code-templates and best practices out in the web or other projects.

8) Think in tiers
Maintenability is one of the trickiest parts in JEE development. There are plenty of options for architecting your
software. The first paradigma is about tiers. Separete the client from the logic and the database access. spend some
time reading about tiers and how to decouple them.

9) Be aware of dependencies
Even if your follow tip 7, you still have to think about dependencies. You can have a couple of dependencies not only
inside your code and tiers but also with your libraries and infrastructure components. Make shure, you are using the
right and only the needed dependendencies wherever they may appear. Handle them on different levels (Architecture,
Development, Build and Depolyment)

10) Stick to naming conventions
Naming this isn’t just for your own good. There’s nothing worse than trying to find your way through some other
programmer’s nonsensical naming conventions. Help yourself and others by using names that make sense for your
classes, variables and declarations. It's worth taking a look at the basic Sun naming conventions. There are plenty
of different project or customer sets out there, too. Respect them and don't think of them as unneeded. One aspect
of JEE programming is about teams. Most likely you will not be the only one working on a project. Sticking to naming
conventions makes live much easier for all involved team members.

11) Comment your code
Aside from plain formatting, you’ll also want to use inline comments and JavaDoc to
annotate your code. You’ll thank yourself later when you’re needing to go back and find something in the code,
or if you just can’t remember what a certain function did. It’s also useful for anyone else who needs to look
over your code. Remember, that your most likely will not be the only one writing code in an enterprise grade project.

12) Decide on the right plattform to start learning with
There are some application servers out there for free. Well known are Sun's Glassfish, Apache's Geronimo or even Redhat's
JBoss. Beside this, you could always decide to start over with a free for development edition of one of the major commercial
vendors, like Oracle's Weblogic, IBM's Websphere or SAP's AS Java. No matter, which version you start over.
This will always be your home to come back to. Therefore it could be
worth thinking about your future needs. What plattform is your company using? Which area of interest drives my developments?

13) Never, Ever Trust Users
If your application has places for user input, you should always assume that they’re going to try to input naughty code.
I am not implying that users are bad people. It’s just a good mindset. A great way to keep your application
hacker-free is to always initialize your variables and validate input on any way to safeguard your site from attacks
(e.g. XSS, SQL Injection).


14) Ask for Help
It’s only human nature to want to hide the fact that we don’t know much about a certain topic.
Nobody likes being a rooky! But how are we going to learn without asking? Feel free to use forums, StackOverflow or
event the vendors support base to ask more seasoned JEE developers questions.


Have any ideas and tips of your own? I’m sure you do! Comments welcome!