Skip to content

Better if readability #75

@jackfirth

Description

@jackfirth

fmt currently puts an if expression onto one line as long as it will fit in the column limit. However, this can make it tricky to tell where the true branch ends and the false branch begins. See these GitHub threads:

The if expressions in those threads are:

  • (if subs (list (restore expr (loop subs))) (list (shift expr)))
  • (if (< s 0) (loop (+ s (* 2 pi))) s)
  • (if (boolean? mred-launcher) (if mred-launcher 'mred 'mzscheme) #t)

I agree with Robby that these are pretty difficult to read. After some discussion in the Racket Discord server, I have a fmt proposal: if expressions should only occupy a single line if the true branch and false branch don't have parentheses. The three examples above would be formatted according to that rule like this:

(if subs
    (list (restore expr (loop subs)))
    (list (shift expr)))

(if (< s 0)
    (loop (+ s (* 2 pi)))
    s)

(if (boolean? mred-launcher)
    (if mred-launcher 'mred 'mzscheme)
    #t)

However, these if expressions would still be allowed to occupy a single line:

  • (if simple? widget gizmo)
  • (if (foo? x) x #false)
  • (if (f (g (h a b c))) 'good 'bad)

This seems to make it easy to tell which expressions are the true and false branches without overly penalizing simple if expressions.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions