> For the complete documentation index, see [llms.txt](https://ondrej-kvasnovsky-2.gitbook.io/design-patterns-handbook/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ondrej-kvasnovsky-2.gitbook.io/design-patterns-handbook/clean-code-patterns/if-else-over-ternary-operator.md).

# If-else over ternary operator

We need to avoid very long lines that are using ternary operator, because it makes code very difficult to read.

> Straightforward usage of ternary operator is OK, for example: String name = user ? user.getName() : "";

Here is an example, last line in a controller that decides what is going to be returned.

```
return result != null ? ResponseEntity.ok(result).header("Location", "/v1/task/" + result.getId()).build() : Response.status(Status.BAD_REQUEST).header("ErrorMessage", "Invalid taskdata").build();
```

What if we want to refactor such a code? We first split it into if-else branches and then we figure out what is actually happening.
