---
title: DSL2 Coding guidelines and best practices
tags: open-source, nf-core, Nextflow, DSL2, best practices, coding, guidelines
---
# DSL2 Coding guidelines and best practices
- [Maxime U. Garcia](https://github.com/maxulysse)
- [James A. Fellows Yates](https://github.com/jfy133)
- [Mahesh Binzer-Panchal](https://github.com/mahesh-panchal)
- [Friederike Hanssen](https://github.com/FriederikeHanssen)
---
## Disclaimer
- We're trying to forge the best practices
- Syntax and/or logic might (and probably will) evolve
---
## Comments
- Add them
- It's useful for:
- 👉 **future you** 👈
- your collaborators
- whoever will look at your code at any point
---
## How, if and when to use `if` and `when` to control modules
- Use `if` logic in the main workflow
- or even better `branch`, which follows the data flow paradigm
- All subworkflows should be simpler and have no need for such logic
- Use `when` in `modules.config` to control modules
- Expect if too many empty process thant clutter the terminal
- Then ok to use `if`
---
## Channel Assignment
- Use explicit channel assignment rather than `.set{}` for readability. E.g.,
```bash
ch_foo = ch_bar.map{ meta, baz -> [baz] }
```
is easier to identify input/output than
```bash
ch_bar.map{ meta, baz -> [baz] }.set{ ch_foo }
```
---
## Collect files to use in a process
```
script:
def inputs = input.collect{"-I $it"}.join(' ')
"""
```
---
## Error messages
- Use Nextflow `error` function
- See also https://github.com/nf-core/nf-co.re/issues/1052
---
## Naming conventions
- Channel variables should start with `ch_`
- `snake_case` for variables
---
## Add CI Tests