Skip to content

if/else formatting can lead to unexpected breaks #334

@aronatkins

Description

@aronatkins

I just reformatted some written-long-ago code frequently included if/else statements without braces. Those statements are reformatted into a shape that leads to a less readable result (to my eye).

These examples show the spirit of what happened:

Input:

foo <- function() {
  if (first_long_variable && second_long_variable)
    cat("ahoy there mates!")
  else
    cat("arr! i be shivering.")
}

Formatted:

foo <- function() {
  if (first_long_variable && second_long_variable) cat("ahoy there mates!") else
    cat("arr! i be shivering.")
}

The input form made it obvious that this was an if/else statement. The reformatted form makes it less clear.

Things are also complicated with if/else-if chains.

Input:

foo <- function() {
  if (first)
    cat("first")
  else if (second)
    cat("second")
  else if (third)
    cat("third")
  else
    cat("last")
}

Formatted:

foo <- function() {
  if (first) cat("first") else if (second) cat("second") else if (third)
    cat("third") else cat("last")
}

The formatted form is much more challenging to skim to see the different expressions.

In each of these examples, I introduced braces in order to avoid letting air collapse them.

Using air 0.4.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions