moodymudskipper / flow

View and Browse Code Using Flow Diagrams
https://moodymudskipper.github.io/flow/
Other
398 stars 28 forks source link

compiling nested ifs into a "case when" box when possible ? #21

Closed moodymudskipper closed 4 years ago

moodymudskipper commented 4 years ago

See the following :

[<choice>if(is.character(x)] -> [x]
[<choice>if(is.character(x)] -> [<choice>if(is.numeric(x)]
[<choice>if(is.numeric(x)] -> [as.character(x)]
[<choice>if(is.numeric(x)] -> [<choice>if(is.logical(x)]
[<choice>if(is.logical(x)] -> [as.character(as.numeric(x))]

image

This could be simplified in a "casewhen box" such as this :

[.
[<choice>if(is.character(x)] -> [x]
[<choice>if(is.numeric(x)] -> [as.character(x)]
[<choice>if(is.logical(x)] -> [as.character(as.numeric(x))]
]

image

It's a bit awkward because it must be named and the name can't be invisible, but we can name them with two dots separated by any amount of spaces to differentiate them and the visual pollution will be minimal.

The box ends up at the end of the last if.

We would gain a bit of space, similar calls would sit next to each other, reducing cognitive load, and the main thread would stay centered rather than being offset to the right, which is often the case with longer functions.

moodymudskipper commented 4 years ago

The example above doesn't have last else, if we had, it could be either implemented under the last if, or could be its own "else" item.

the latter might look better and make more sense because "else" is not really link to a specific option here, it means "if all the other options were FALSE".

We wouldn't have duplicate issues there because in a box you can find labels already used outside of the box, but we'd probably number those anyway (though we can make a case that the else itself shouldn't be numbered).

moodymudskipper commented 4 years ago

if without else are another type of operation that shifts the layout to the right, we could also box them, so we continue right below them.

if with else clauses but no nested ifs could of course be boxed in principle too, but in that case it doesn't add much clarity.

We'd have to opt separately in for those 2 options (box_if_no_else = FALSE, box_nested_if_calls = FALSE).

It's not trivilal because we could have boxes in boxes, that might be a nightmare to implement.

moodymudskipper commented 4 years ago

not that useful, let's drop for now

github-actions[bot] commented 2 years ago

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue and link to this old issue if necessary.