Get the FREE Ultimate OpenClaw Setup Guide β†’

142-java-functional-programming

Scanned
npx machina-cli add skill jabrena/cursor-rules-java/142-java-functional-programming --openclaw
Files (1)
SKILL.md
3.2 KB

Java Functional Programming rules

Identify and apply functional programming principles in Java to improve immutability, expressiveness, and maintainability.

Core areas: Immutable objects and Records (JEP 395), pure functions free of side effects, functional interfaces (Function, Predicate, Consumer, Supplier) and custom @FunctionalInterface types, lambda expressions and method references, Stream API (filter/map/reduce pipelines, parallel streams, toUnmodifiable* collectors), Optional idiomatic usage (map/flatMap/filter/orElse* over isPresent()+get()), function composition (andThen/compose), higher-order functions (memoization, currying, partial application), Pattern Matching for instanceof and switch (Java 21), sealed classes and interfaces (Java 17) for exhaustive domain hierarchies, Switch Expressions (Java 14) for concise multi-way conditionals, Stream Gatherers (JEP 461) for custom intermediate operations, effect-boundary separation (side effects at edges, pure core logic), and immutable collections (List.of(), Collectors.toUnmodifiableList()).

Prerequisites: Run ./mvnw compile or mvn compile before applying any changes. If compilation fails, stop immediately β€” do not proceed until the project compiles successfully. Also verify that the project's maven-compiler-plugin source/target supports the Java features being used.

Multi-step scope: Step 1 validates compilation. Step 2 categorizes functional programming opportunities by impact (CRITICAL, MAINTAINABILITY, PERFORMANCE, EXPRESSIVENESS) and area (immutability violations, side effects, imperative patterns, type safety gaps). Step 3 converts mutable objects to immutable Records or final classes. Step 4 extracts pure functions from methods with side effects. Step 5 replaces imperative loops with declarative Stream API operations. Step 6 adopts Optional for null-safe method returns and functional chaining. Step 7 applies function composition, currying, and higher-order functions. Step 8 introduces sealed types, pattern matching, and switch expressions for type-safe conditional logic. Step 9 hardens collectors (merge functions for toMap, downstream collectors, unmodifiable results). Step 10 runs ./mvnw clean verify to confirm all tests pass after changes.

Before applying changes: Read the reference for detailed good/bad examples, constraints, and safeguards for each functional programming pattern.

Reference

For detailed guidance, examples, and constraints, see references/142-java-functional-programming.md.

Source

git clone https://github.com/jabrena/cursor-rules-java/blob/main/skills/142-java-functional-programming/SKILL.mdView on GitHub

Overview

Identify and apply functional programming principles in Java to improve immutability, expressiveness, and maintainability. Core areas include immutable objects and Records, pure functions, functional interfaces, lambdas, Stream pipelines, Optional usage, function composition, higher-order functions, pattern matching for instanceof and switch, sealed types, and safe collection handling. This skill follows a pragmatic, multi-step workflow from building to verifying with tests.

How This Skill Works

Learn to apply Java FP techniques in practice: create immutable objects and Records, use functional interfaces and lambdas, and build Stream pipelines for transformations. It also covers safe usage of Optional, function composition (andThen/compose), currying and higher-order functions, and modern type-safe features like pattern matching and sealed hierarchies with switch expressions and Stream Gatherers.

When to Use It

  • Design domain models as immutable objects or Records to ensure thread-safety and easier reasoning.
  • Adopt Optional and functional chaining to handle absence without explicit null checks.
  • Refactor mutable classes into immutable final types or Records to improve maintainability.
  • Replace imperative loops with declarative Stream API pipelines for readability and performance.
  • Leverage sealed types, pattern matching, and switch expressions to enforce exhaustive, type-safe logic.

Quick Start

  1. Step 1: Run ./mvnw compile (or mvn compile) to validate the project builds.
  2. Step 2: Identify side-effect boundaries and extract pure functions.
  3. Step 3: Replace imperative loops with Stream API pipelines and adopt Optional for null-safety.

Best Practices

  • Prefer immutable objects and Records over mutable classes.
  • Use functional interfaces and lambdas instead of anonymous inner classes.
  • Favor Optional with map/flatMap and cautious use of orElse to avoid isPresent/get.
  • Leverage Stream API pipelines and toUnmodifiable collectors to preserve purity.
  • Adopt sealed types, pattern matching, and switch expressions to ensure exhaustive handling.

Example Use Cases

  • Refactor a mutable DTO to an immutable Record with final fields.
  • Replace a mutable loop that builds a result list with a Stream pipeline and collect toUnmodifiableList.
  • Compose small pure functions and reuse them via andThen/compose.
  • Replace null checks with Optional and a functional chain for defaults and transformations.
  • Introduce pattern matching and sealed hierarchies to safely handle a shapes/animals domain with a switch expression.

Frequently Asked Questions

Add this skill to your agents
Sponsor this space

Reach thousands of developers β†—