GeeCON 2018: Miro Cupak - Clean code with Java 9
GeeCON 2018: Miro Cupak - Clean code with Java 9.
Summary
### Overview In this GeeCON 2018 presentation, Miro Cupak discusses how Java 9 introduces several small APIs, convenience methods, and enhancements that improve the ability to write clean, maintainable, and readable code. Drawing inspiration from software design principles and books such as *Clean Code* and *Effective Java*, the talk demonstrates practical improvements using JShell (Java's REPL tool) across collection creation, resource management, interface design, stream processing, optionals, stack traces, and HTTP networking.
### Key Topics - **JShell (REPL):** A tool for evaluating code snippets and commands interactively, useful for prototyping, quick testing, and exploring APIs. - **Convenience Factory Methods for Collections:** Static factory methods (`List.of`, `Set.of`, `Map.of`, `Map.ofEntries`) that provide concise syntax for creating truly immutable collections. - **Try-with-Resources Enhancements:** Allowing final or effectively final variables to be managed directly by try-with-resources blocks without creating redundant local references. - **Private Methods in Interfaces:** Enabling helper methods to encapsulate common logic between interface default methods without exposing them to the public API. - **Diamond Operator Improvements:** Extending diamond operator type inference to anonymous inner classes. - **Stream API Extensions:** Introduction of `takeWhile`, `dropWhile`, improved `iterate` with predicates, and `Stream.ofNullable`. - **Optional Enhancements:** Additions including `ifPresentOrElse`, `or`, and the `stream()` method for cleaner chained operations. - **StackWalker API:** A modern, lazy, stream-friendly replacement for `Throwable.getStackTrace()`. - **Incubator HTTP/2 Client:** A cleaner, feature-rich replacement for `HttpURLConnection` supporting HTTP/2, WebSockets, TLS, and asynchronous calls.
### Technical Details - **Immutable Collections:** Methods like `List.of(...)` and `Set.of(...)` offer 0–10 fixed-parameter overloads alongside a varargs variant to optimize performance and prevent array allocation overhead. `Map.ofEntries(...)` handles heterogeneous key-value mapping concisely. Returned collections are strictly immutable rather than just unmodifiable views. - **I/O & Try-with-Resources:** Java 9 eliminates the need to assign resources to fresh variables inside the `try (...)` parentheses. Additionally, `InputStream.transferTo(OutputStream)` provides a single convenience method for streaming bytes between input and output. - **Stream Control:** `takeWhile` acts like a predicate-based `limit`, and `dropWhile` acts like a predicate-based `skip`. `Stream.iterate` now accepts a predicate condition functioning like a standard `for` loop, mitigating infinite stream bugs. `Stream.ofNullable` creates an empty stream if passed `null`, removing unnecessary ternary or null check boilerplate. - **Optional Integration:** `Optional.ifPresentOrElse(Consumer, Runnable)` removes conditional branching, `Optional.or(Supplier<Optional>)` enables fluent fallback options, and `Optional.stream()` simplifies flattening streams of optionals via `flatMap(Optional::stream)`. - **Stack Inspection:** `StackWalker` traverses JVM stack frames lazily via streams, avoiding the heavy performance cost of eagerly capturing entire stack traces. It can retain class references directly and permits filtering or limiting stack depth. - **HTTP/2 Client:** Split cleanly into `HttpClient`, `HttpRequest`, and `HttpResponse` using the builder pattern and supporting asynchronous flows with `sendAsync(...)` returning a `CompletableFuture`. In Java 9 and 10, it is distributed as an incubator module.
### Takeaways Developers can significantly clean up Java codebases by replacing verbose patterns and external boilerplate libraries with native JDK 9 capabilities. Defaulting to immutable collections, leveraging stream and optional convenience methods, adopting `StackWalker`, and utilizing modern try-with-resources semantics eliminate defensive copying, null checks, and legacy side-effect-heavy APIs.
Related talks
geecon conference·48 min
GeeCON Prague 2018: Miro Cupak - Pushing boundaries of types with modern Java.
PROIDEA Events·37 min
Analysts investigate attack surfaces and defenses concerning JDD 2022: Damian Petrecki - Is clean code a good code?.
PROIDEA Events·49 min
Jonathan Vila Lopez
Jonathan Vila Lopez details JDD 2024: Clean Code in the AI era.


