Correct Usage of Java 8 Optional

Optional is a new type of Java 8. I saw it before with scala and was quite impressed. You know Tony Hoare’s “billion-dollar mistake”: the null reference. How many NullPointerExceptions have you yet? The type Optional wants to fix that. With Optional you won’t get any NullPointerExceptions anymore. But it is important to use Optional the right way. So don’t disappoint Hoare and take the chance to fix the billion-dollar mistake.

Return values

If you have a method with a result which sometimes may be empty, don’t return null. Even if you add a comment to your method. They will not always read it.

* @return an object or else null.
public Object getSomething() {
  if (haveObject) {
    return object;
  } else {
    return null;

You should tell your user and the compiler that they can not be sure to get an object or not. Use the Optional:

* @return Optional of Object
public Optional<Object> getSomething() {
  if (haveObject) {
    return Optional.of(new Object());
  } else {
    return Optional.empty();

Only do if not null

Consuming an optional value you probably want to do something only if a value is present. You could do it by using the method isPresent():

public void doOnlyIfPresentComplicated() {
  Optional<Object> something = logic.getSomething();
  if (something.isPresent()) {

But this is not much of a progress compared to writing if (something != null). This is where the lambda expressions of optional come into play. Using ifPresent() you can write this much better:

 public void doOnlyIfPresent() {

Avoid nested null checks

Sometimes you have several optional values in a row. Only if all optionals return a value, you want to return an http 200. Http 404 should be returned if somewhere in the chain there is no value. Again there is a complicated way:

public Response avoidNullChecksComplicated() {
  Optional<Object> something = logic.getSomething();
  if (something.isPresent()) {
    Optional<Object> otherOptional = logic.otherOptional(something.get());
    if (otherOptional.isPresent()) {
      Optional<Object> lastOptional = logic.lastOptional(otherOptional.get());
      if (lastOptional.isPresent()) {
        return Response.ok(lastOptional.get()).build();
  return Response.status(Status.NOT_FOUND).build();

Or you avoid the nested if statements and simply use flatMap and method references:

 public Response avoidNullChecks() {
   return logic.getSomething() //
     .flatMap(this::getOtherOptional) //
     .flatMap(this::getLastOptional) //
     .map(this::getOKResponse) //

 private Optional<Object> getOtherOptional(Object o) {
   return logic.otherOptional(o);

 private Optional<Object> getLastOptional(Object o) {
   return logic.lastOptional(o);

 private Response getOKResponse(Object o) {
   return Response.ok(o).build();

 private Response get404Response() {
   return Response.status(Status.NOT_FOUND).build();

I think it is much better than the nested if statements. If you use all Optionals the right way, your code stays readable and you can fix some of the “billion-dollar mistake”.


Why I don’t like IBM RTC as Source Control Management

In a previous post I already mentioned why the architecture of IBM RTC will never scale like git. As a developer you use an SCM and probably a project management tool on a daily basis. Even small annoying things make you feel bad and are time consuming. In this post I list some reasons why a move away from RTC to git will save real money for your organisation and make your work easier.

Maven integration

IBM RTC is not prepared to work with hierarchical structured projects. Maven is a widely used build and dependency management tool. In some cases it is important to have a hierarchical structure of your projects. Especially if you work with parent and child projects. Unfortunately RTC hinders you to check out parent and child projects in the same workspace. You can make it run but have to do some workarounds. There is also a good blog post about it by Mike MacDonagh (Working with Maven multi-module projects in RTC Jazz SCM with m2). He talks about RTC v3.x but it is basically the same as with v5.x.

There are no reasons why you couldn’t do it with git. git does not know anything about a project. It is not it’s business. RTC seams to manage things that it shouldn’t.

IDE integration

All current IDEs have git support in their basic versions. I don’t know of any Java-IDE which comes with IBM RTC integration out of the box. This means you have to install it manually. In case of Eclipse it means to download about 500 MB(!) of plugins. With every new version of Eclipse you have to wait for the new IBM plugin before you can use the new features. You got the same situation with Visual Studio, IntelliJ and NetBeans.

Comparing file versions between branches / streams

git_branch_compareWith git you can easily compare file versions from different releases in different branches. The history view shows all very clearly (image on the right side).

In IBM RTC you can’t do that. It is a recommendation from IBM to create a new stream for every release. At first sight it might look pretty but if you really work with it you see the disadvantages:

  • It is complicated to merge streams.
  • There is really no way to compare your file from one stream with another stream until you load both streams.
  • It is even impossible to see if the file changed in another branch.


Some people say the merging process of git was better than the one with IBM RTC. Maybe I will post a direct and detailed comparison between those two tools later.

Complicated commits / delivery

This is a quick comparison between the git way and the RTC way.

The git way

Either you use the console:

git pull
git commit -m 'your comment'
git push origin master

Or you use the awesome Git Staging view from Eclipse:

To commit and push there are only three steps:

  1. Drag and drop the Files to your staging area
  2. Add a comment
  3. Click the Button “Commit and Push”

The RTC way

IBM RTC didn’t manage to create such a smooth view. Every action needs lots of server calls because you don’t have a local repository. They added a lot of asynchronous calls which lets your view update randomly. You sometimes wait for ages depending on how big your component, stream, change set or history is.

You need to do make at least five steps. It includes context menus, keyboard strokes and tree navigation:

  1. check-in your changes (3 clicks)
  2. Open the outgoing folder under your stream and find your newly created change set.
  3. Either click on it once and press “F2” or wait a second and perform another click.
  4. type your comment
  5. Deliver (another 2 clicks)
  6. Sometimes you have to answer some complicated questions about not checked-in changes.
  7. Then some asynchronous tasks start running and mostly after a second or two everything is delivered.

Working offline

As you have no local repository every interaction with RTC requires a server call. It is not designed to work offline. Git always had that use case in mind.

Git ecosystem

Git is very popular, widely used and extendable with hooks and triggers. Therefore many open source projects emerged around it. Just a brief list:

  • “Social coding”. The most popular git repository. I social web ui with issue tracking and many other integrations.
  • gitblit: Github like open source server ready to host on your local infrastructure.
  • Project management web application with git integration.
  • Jenkins / Travis: Build servers best integrated with git.
  • Atlassian Confluence: Team collaboration for software development.

You need really good reasons to make a decision against this tools and for a proprietary SCM.

Licenses and hardware resources

IBM RTC is expensive and resource consuming. I am probably not allowed to tell you the price you have to pay. But believe me it is expensive. To keep RTC running you need:

  • RTC-Server on a heavy weight Websphere
  • An expensive database like DB/2 (or MSSQL or Oracle although not recommended)

Git needs:

  • A filesystem for your repositories

You will install another tool like those mentioned above if you need issue tracking or project management. They will mostly run with an apache2 or apache tomcat and a MySQL database server.

Other issues or feedback?

Let me know if you got further pros or cons of RTC or git. I will update this post as I stumble upon more points.