top | item 13804470

(no title)

marcoms | 9 years ago

What's the argument for required brackets around if (conditions) and switch (conditions)? To me they seem redundant.

--

As a side point, I hope switch statements are less like C and more like Rust (Rust happens to have better looking syntax, not just because its Rust):

    match <identifier> {
        <val> => {
            //
        }
    
        ..
    }
C-style always creates confusion on how to indent blocks following case statements

discuss

order

david-given|9 years ago

Having tried this in the past with personal programming languages, I always found I needed the parentheses to disambiguate control flow statements. You end up with stuff like:

  if foo*bar baz()
...being parseable as either of these:

  if (foo) { *bar } baz()
  if (foo*bar) { baz() }
That example can be solved with enough lookahead but I think I found cases where it couldn't be (although I can't bring them to mind). Terminator tokens are another way to solve it, so you get Ada-style:

  if foo*bar then baz() endif
...or Go-style:

  if foo*bar { baz() }

erikpukinskis|9 years ago

If you stop putting multiple expressions per line it disambiguates too:

    if foo
      *bar
    baz()

    if foo*bar
      baz()

__s|9 years ago

Rust confronts this by making curlies mandatory

macintux|9 years ago

What you really should be asking for is replacing switch clauses with function heads like ML or Erlang. </soapbox>

yarrel|9 years ago

The argument (which I agree with, although it can be a pain to adopt at first):

http://wiki.c2.com/?AlwaysUseBracesOnIfThen

tekacs|9 years ago

What you linked is about braces around the /body/ of an if statement.

What the GP poster was talking about was parentheses around the /conditions/ of an if statement.

Note that Rust (for example) doesn't require the latter.

In fact, if you always require braces (as in the post that you linked), then it's very easy to parse and read code which is lacking those parentheses on the condition.

i.e. `if (condition) x = x + 1` would be hard to read without parens. but `if condition { x = x + 1 }` (with mandatory braces) is quite clear.

marcoms|9 years ago

As others speculated I meant parentheses around the condition - my mistake, I should have qualified

achamayou|9 years ago

Just the lack of fall through by default is already a significant improvement.