Skip to content
This repository was archived by the owner on Oct 9, 2018. It is now read-only.

Style of if expressions used as ternary operators? #42

Open
dhardy opened this issue Jan 19, 2015 · 3 comments
Open

Style of if expressions used as ternary operators? #42

dhardy opened this issue Jan 19, 2015 · 3 comments

Comments

@dhardy
Copy link

dhardy commented Jan 19, 2015

Any recommendations on (white-space formatting of) the following?

let val = if some_long_and_complex_condition() {
    val_a
} else {
    val_b
};

verse something more condensed like:

// ugly, but at least not overly long:
let val = if some_long_and_complex_condition() {
    val_a } else { val_b };
@chris-morgan
Copy link
Member

For starters, there should always be whitespace around curly braces with the exception of after a closing brace followed by certain pieces of punctuation (), ], , and ; being the ones that occur to me initially).

For the rest, I would say either is acceptable, but once must take into account the line length limits (99 characters) and æsthetic concerns which may easily render the single-line version undesirable.

@dhardy
Copy link
Author

dhardy commented Jan 19, 2015

Updated spacing to reflect chris-morgan's comment. I would consider single-line formatting not to be a problem, but my example was meant to ask about cases too long for a single line.

@donsignore
Copy link

Additional alternative easy for humans to parse:

let val = if some_long_and_complex_condition() {
    val_a } else {
    val_b };

or:

let val = if some_long_and_complex_condition() {
        val_a } else {
        val_b };

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants