> For the complete documentation index, see [llms.txt](https://www.pranaypourkar.co.in/the-programmers-guide/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.pranaypourkar.co.in/the-programmers-guide/java/best-practices/method-chaining.md).

# Method Chaining

#### 1. We have two versions of a method that checks conditions on a deeply nested object structure.

**Version 1: Repeated method chains**

```java
private boolean isRequestNotValidated(PaymentValidationRequest paymentValidationRequest) {
    return paymentValidationRequest.getPayment().getPaymentType().equals(TRANSFER)
        &&
        paymentValidationRequest.getPayment().getTransferTransactionInformation().getAdditions()
            .get(SESSION_ID)
            == null
        && !hashGenerator.generateValidationHash(requestDataUtils.getShortCIF()).equals(
        paymentValidationRequest.getPayment().getTransferTransactionInformation().getAdditions()
            .get(VALIDATION_HASH));
}
```

**Version 2: Intermediate variable for reuse**

```java
private boolean isRequestNotValidated(PaymentValidationRequest paymentValidationRequest) {
    var additions = paymentValidationRequest.getPayment().getTransferTransactionInformation().getAdditions();

    return paymentValidationRequest.getPayment().getPaymentType().equals(TRANSFER)
        &&
        additions.get(SESSION_ID) == null
        && !hashGenerator.generateValidationHash(requestDataUtils.getShortCIF()).equals(
        additions.get(VALIDATION_HASH));
}
```

**Version 2 is better** for the following reasons:

#### **1. Readability**

* Extracting intermediate results (`additions`) improves readability and reduces mental load.
* Readers only need to understand the logic of the condition, not how it’s fetched every time.

#### **2. Maintainability**

* If the method chain changes, we only have to update it in one place.
* Prevents errors from inconsistent calls.

#### **3. Performance**

* **No noticeable performance difference** in most cases.
* JVM performs **common subexpression elimination** and **method inlining** during Just-In-Time (JIT) compilation.
* Repeated chains may result in slightly more bytecode but won’t affect runtime efficiency significantly — **if the getter methods are cheap** (simple field accesses).

But if:

* The getters are doing more than returning a value (e.g., performing logic or expensive computation),
* Or they involve **reflection**, **network calls**, or **dynamic proxies** (common in some frameworks),

Then **repeated calls could degrade performance**.

So while JVM *might* optimize it, **don't depend on it for correctness or speed** — extracting to a variable is the cleaner and safer approach.

#### **Null Safety**

With repeated method calls, it’s easy to accidentally get a `NullPointerException` if any level in the chain is null.

Extracting intermediate variables allows:

* Easier null-checks
* Optional usage
* Better error messages and logging


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://www.pranaypourkar.co.in/the-programmers-guide/java/best-practices/method-chaining.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
