Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Request: Multiline conditionals can begin with operator (and/or/etc) #2448

Closed
mrak opened this Issue Jul 20, 2012 · 5 comments

Comments

Projects
None yet
4 participants

mrak commented Jul 20, 2012

I would like to propose the following for increased readability on multiline conditional statements:

if thesun is bright
and thesky is blue
and theair is clean
   console.log "My oh my, It's a wonderful day!"

Which I find much more readable than

if thesun is bright and
thesky is blue and
theair is clean
   console.log "My oh my, It's a wonderful day!"
Contributor

epidemian commented Jul 24, 2012

There are some alternatives.

Using the logical operators at the end of the line plus some indentation looks good to me:

if thesun is bright and
   thesky is blue and
   theair is clean 
  console.log "My oh my, It's a wonderful day!"

But you can also use line continuations:

if thesun is bright\
and thesky is blue\
and theair is clean 
  console.log "My oh my, It's a wonderful day!"

Or even parentheses:

if (thesun is bright
and thesky is blue
and theair is clean)
  console.log "My oh my, It's a wonderful day!"

Of those options, i prefer the former, but in general, i try to keep conditions simple so they can fit in one line. In this case, it's basically asking if a bunch of variables have the same value, so:

if thesun is bright is blue is clean
  console.log "My oh my, thesun, bright, blue and clean are the same thing!"

erisdev commented Jul 24, 2012

@epidemian:

In this case, it's basically asking if a bunch of variables have the same value

That's not quite the case! there are three pairs of variables being compared here, so ands are necessary.

When I want to do multi-line conditionals, I tend to use the line continuations because I too like the keywords to line up. Putting the logical operators at the end and indenting seems to be a cleaner solution since it uses CoffeeScript's natural line continuation rules, but the weird indentation gives me vertigo. The parentheses method doesn't actually work.

I guess the line continuations also give a visual cue that we're not quite finished yet, so that might be a plus, ugly or not.

Contributor

epidemian commented Jul 24, 2012

That's not quite the case! there are three pairs of variables being compared here, so ands are necessary.

Ho, snap, how could i misread that? (i even copy&pasted it three times! my brain just discarded everything after "the"). And yeah, the parentheses don't work; i don't know where my head was at =/

Now, trying to add something constructive to this conversation, i think the only problem with this proposal is that it adds another special case for multi-line expressions. And in a language with no semicolons to mark statement terminations or braces for block delimiting (no free-form grammar) i think it's key to keep the parsing rules as simple as possible.

For example, adding this rule would create an inconsistency between binary logical operators and the binary + and - operators. The former would continue the previous line expression while the later won't. For example, this is valid CoffeeScript:

if something
   + somethingElse is 0 # Does the previous line continue or am i using the unary + to convert somethingElse to a number?

So the rule wouldn't apply to all binary operators, and instead would have to be something like "if a line starts with a an operator that can only be binary, then it is a continuation of the previous one".

mrak commented Jul 24, 2012

I do have to admit that the times I use multiline conditionals is quite rare. That being said, there a few times that it makes the most sense to do so with the most concise and readable code.

I was going to comment that the first thing I tried was the parenthesis, but erisdiscord beat me to it :p

I didn't think of the situations involved with arithmetic operators. I admit that I was zoned in to conditional statements and boolean logic when I posted the request. I've greatly enjoyed the readability that coffescript offers over javascript and this is one of the few scenarios that I would think reverting back to javascript syntax (thus the attempt at parenthesis) would be it easier on the eyes:

if( thesun === bright
&& thesky === blue
&& theair === clean ){
  console.log("My oh my, it's a wonderful day!");
}

It's been a while since I've dealt with context-free grammar, but how complicated would it be to incorporate this into coffee's parsing engine? It sounds from first glance that the parsing can still be deterministic, and thus conceivable...

Collaborator

GeoffreyBooth commented Apr 29, 2017

if thesun is bright and
   thesky is blue and
   theair is clean 
  console.log "My oh my, It's a wonderful day!"

compiles now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment