DEV Community

Discussion on: Got Coding Superstitions?🤞

Collapse
 
taikedz profile image
Tai Kedzierski • Edited

Don't use backslashes \ to split lines - especially in shell scripts

# actually remove old settings before redeploying, else it will append
ssh user@host \
    rm -r .config
Enter fullscreen mode Exit fullscreen mode

Somehow I got bitten by that once where the character after backslash became inline whitespace and caused a hard to find bug with the second part becoming a new statement on a new line.

Using parentheses usually gets us out of r trouble, and allows more granular commenting:

tokens=(
    ssh user@host
    # actually remove old settings before redeploying, else it will append
    rm -r .config
)
"${tokens[@]}"
Enter fullscreen mode Exit fullscreen mode

In python it's less likely to be an issue, but it's still nice for inline commenting

assert i_did_it(data), \
    "You did not" + \
    f"You did: {data}"
Enter fullscreen mode Exit fullscreen mode
assert i_did_it(data), (
    # Admonish programmer
    "You did not"
    # Highlight irregularity
    f"You did: {data}"
)
Enter fullscreen mode Exit fullscreen mode