# Composing strings based on multiple conditions

**URL:** <https://community.seqera.io/t/composing-strings-based-on-multiple-conditions/1832>\
**Category:** Tips & Tricks\
**Tags:** nextflow\
**Created:** [February 27, 2025, 8:38am UTC](https://community.seqera.io/t/composing-strings-based-on-multiple-conditions/1832 "2025-02-27T08:38:22Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![mahesh.binzerpanchal](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/mahesh.binzerpanchal/32/253_2.png) [@mahesh.binzerpanchal](https://community.seqera.io/u/mahesh.binzerpanchal)\
**Post date:** [February 27, 2025, 8:38am UTC](https://community.seqera.io/t/composing-strings-based-on-multiple-conditions/1832/1 "2025-02-27T08:38:22Z")

</div>

When composing longer strings based on multiple tests in Nextflow workflows, using the ternary operator or string concatenation can lead to long, hard-to-read lines of code. An alternative approach is to use a list to store the parts and then join them to create the final string. This method improves readability and maintainability.

e.g.

```auto
ext.args = {"-ax ${meta.readtype.equals("hifi") ? "map-hifi" : meta.readtype.equals("clr") ? "map-pb" : meta.readtype.equals("ont") ? "map-ont" : meta.readtype.equals("illumina") ? "sr" : ""} --cs=short ${reference.size() > 2.5e9 ? (" -I" + Math.ceil(reference.size()/1e9)+"G") : ""}" }

```

could become

```auto
        ext.args = { [
            meta.read_type.equals("hifi") ? "-ax map-hifi" : "",
            meta.read_type.equals("clr") ? "-ax map-pb" : "",
            meta.read_type.equals("ont") ? "-ax map-ont" :"",
            meta.read_type.equals("illumina") ? "-ax sr" : "",
            "--cs=short",
            reference.size() > 2.5e9 ? (" -I" + Math.ceil(reference.size()/1e9)+"G") : "",
        ].minus("").join(" ") }

```

Key improvements:

- Each condition is on a new line, which improves readability.
- The `.minus("")` removes all empty strings (an alternative is `.findAll()` which removes anything that evaluates to Groovy false - `[[], 0, "", '', false ]` ).
- Then `join(" ")` which combines the remaining parts with spaces.

This improves maintainability, as adding and removing conditions becomes easier.

( Written with the aid of [Seqera AI](https://seqera.io/ask-ai/) )
