You are here

Writing awesome Java code workshop

Posted by Steven

A couple of weeks ago, I gave another workshop for my colleagues in Braunschweig, Germany. The first thoughts for this workshop came to me months ago. I noticed that all developers I know are proficient in reading and writing Java code. They know the language concepts and syntactic rules. However, some of them struggle with coding elegantly and efficiently. I realized that basic concepts of "advanced Java programming" are not known or not understood. Most of the developers I know don't go to conferences or read magazines on a regular basis, so they don't learn emerging languages or concepts. Inhouse workshops can also not deliver the broad spectrum of topics the Java ecosystem develops rapidly.

As you can see in the linked repository, all sources and explanations are open source, just like in my Java 8 and JavaFX workshops. Please feel free to use the material for your own workshops, autodidactic learning or even to develop it further via Pull Requests.

These are the topics of the workshop:

Part 1: Java language and methods

Comments: I know, I know. Comments are one of the first things you learn about Java. But some of us use line comments (" // ") to add documentation to methods, not knowing that these should begin with " /* " to be recognized by IDEs as JavaDoc.

Visibility modifier. Same here: Basic stuff. But do you know the difference between package private and protected methods? Hint: To make a private method testable, make it package private instead of protected. That way, you keep the encapsulation and you can test the method.

hashcode() and equals(). Most of us Java developers work in projects that exists for a couple of months or years, so basic decisions have been made by our successors. How to implement hashcode() and equals() is one of those things. But what ways are there to implement those basic methods?

Pure functions. Java supports functional programming more and more. Because of that and because of the better understandability, every developer should know what a pure function is.

Immutability. Like functional programming, immutability can (partially) be used to write Java code that is easier to maintain.

Null and null objects. Some say, null is one of the biggest mistakes introduced into Java. Indeed, it creates unreadable code with a lot of is-not-null decisions. In this chapter, I show how to use null objects to create better code.

Fluent APIs. The design of APIs is often cut short in projects because of time constraints. However, APIs are the interface to the written code for other developers and should be designed with great care. Fluent APIs are one of many ways of designing APIs.

JUnit. Also old as the hills. Nevertheless, developers don't want to write tests. In my experience, developers in legacy projects are demotivated because a simple "hello world" JUnit test will not solve their problems of highly unreadable, entangled big-ball-of-mud-code. To give them a start anyway, this chapter is a gentle introduction in JUnit.

Part 2: Methods for legacy code

As written in the last topic of part 1, legacy code is the most likely environment of today's developers. In part 2, I show methods that enhance existing code. These include:

Sprout methods

Golden Master

Mikado method

As you can see in the codebase, this chapter consists mainly of written text because the methods are best explained in an overview style. You can use this as a manual to prepare your own workshops or simply try these methods for yourself.

Part 3: Applying to legacy code

The last part of the workshop focusses on applying all the learned methods to real legacy code. To have a common starting point, I provide two legacy code bases: The famous ugly trivia game and one extracted method from a "real world project". The participants of the workshop use these codebases to exercise the methods in a legacy code retreat.