moodymudskipper / flow

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

If-else ladder comments #71

Open jwallib opened 3 years ago

jwallib commented 3 years ago

Hi there,

First of all, thank you for the work you're doing on this package, I've only just come across it but it seems very helpful so I expect I will be using it a lot from now on! One issue I have spotted is that I am unable to/dont see how to add comments for each if statement in an if else code chunk. I would like to ideally add comments to these if statements and then hide the code like the variable code = FALSE does. See minimalistic example below:

dummy_function <- function(x){
  ##I can comment the first code chunk
  if(is.character(x)){
    ##Do something1
    print('Do something....')
  } else if(is.logical(x)) {
    ##Do something2
    print('Do something....')
  }else if(is.numeric(x)){
    ##Do something3
    print('Do something....')
  }else {
    ##Do something4
    print('Do something....')
  }
##Done
print('complete')
}
flow_view(dummy_function, prefix = '##', code = FALSE)

Capture

moodymudskipper commented 3 years ago

Hi @jwallib and thanks for the kind words. This is indeed a limitation, not so much a technical one but I don't know where we would put those comments...

A way to make it work currently is :

  ##I can comment the first code chunk
  if(is.character(x)){
    ##Do something1
    print('Do something....')
  } else {
    ## your new comment
    if(is.logical(x)) {
      ##Do something2
      print('Do something....')
    }
 } 

Not so pretty in case of many nested if else if but should display right.

We might implement something like this in theory, but it feels awkward to me :

else if (
  ## comment
  is.logical(x)) {...} 

#or   

else if
  ## comment
  (is.logical(x)) {...} 

Fwiw I tend more and more to give long name to conditions such as :

x_is_logical <- is.logical(x)
if(x_is_logical) {...} 

This way the if calls are self documenting, but it's not perfect either.

Happy to read suggestions for if you have ideas.

jwallib commented 3 years ago

Hi @moodymudskipper,

Thanks for your reply. I agree it is indeed difficult to decide where to put the comment. Just a suggestion but how about something like this:

comment for condition 1

if(condition1){ do_something(x)

comment for condition2

}else if(condition2){ do_something_else(x) }

So you check the line immediately before the else statement. Admittedly its not perfect as the comment would 'live' in the previous if statements code block but if you check conditionally for a comment in the last line of a block of code which is followed by and else statement it could work?

moodymudskipper commented 3 years ago

It might work yes, it's the easiest to type/most readable and it's unambiguous as there is nothing else to comment under it.

The downside is that it might not be where you'd put a regular comment so it makes this comments a bit more like code when I wanted them to be just comments, but all in all it seems to be the best way, at least to my taste in this moment.

It's not super easy to implement so don't hold your breath but I'll give it a shot after current release is pushed to CRAN

jwallib commented 3 years ago

That's absolutely fine, thank you for considering it!