Design‎ > ‎

Statement blocks

How to define a block of statements and separate it from its environment?

1. By indent, like Python
      IF cond
        do this
      ELSE
        do that
    + ensures indenting is done right
    + easy to read back
    - easy to make mistakes, especially for longer constructions
    - automatic indenting not always possible
    - copy/pasted blocks require manual re-indenting, easy to make mistakes

2. With {}, like in C
      IF cond
      {
        do this
      }
      ELSE
      {
        do that
      }
    + clear syntax, easy to parse
    - many ways to put the {}, leads to different styles
    - two extra lines per block
    o can leave out for single statements, shorter but may cause mistakes

3. With "begin" and "end", like pascal
      IF cond
        BEGIN
          do this
        END
      ELSE
        BEGIN
          do that
        END
      END
    - too wordy, quickly lose overview

4. With "endif"
      IF cond
        do this
      ELSE
        do that
      ENDIF
    + simple and clear
    + only one extra line needed
    - longer than C when using single statements
    - "endif" doesn't stand out from other statements
        - when moving blocks of code around it's a hassle to match the block
          ends with the right start.

5. With an "end of block" symbol, e.g., '}' (like a full stop)
      IF cond
        do this
      ELSE
        do that
      }
    + simple and clear
    + only one extra line needed
    + less wordy than "endif"
    - longer than C when using single statements


Choice: 5: easy to read back, consistent style encouraged

One can also explicitly start a new block with {:
          {
             String s = "foo"
             IO.write(s)
          }


Switch statement

According to the above a switch statement looks like this:

     SWITCH expr
       CASE val1
         do_something(1)
         BREAK
       CASE val2
         do_something(2)
         BREAK
       DEFAULT
         do_nothing()
      }

Experience shows that it's a common mistake to forget the BREAK.  This is
very hard to notice.  The solution for that is to not fall through, but always
behave like the BREAK is there.  This also makes the code noticeable shorter
when there are only a couple of statements for each CASE.

However, some values may have the same or similar behavior.  Having to
repeat that or move it to a separate function is not good.  Therefore we will
allow giving multiple CASE statements, without code in between:

     SWITCH expr
       CASE val1
         do_something(1)
       CASE val2
       CASE val3

         do_something(2)
       DEFAULT
         do_nothing()
      }

In C it is possible to have one CASE with a few statements and
then fall through to the next one:

    switch (expr) {
      case 1:
         only_for_1();
      case 2:
         common_for_1_and_2();
         break;
    }

The Zimbu implementation is:

     SWITCH expr
       CASE 1
       CASE 2
         IF expr == 1
            only_for_1()
         }
         common_for_1_and_2()
      }

An alternative would be to explicitly fall through:

     SWITCH expr
       CASE 1
         only_for_1()
         FALLTHROUGH
       CASE 2
         common_for_1_and_2()

     }

The main reason to support this is to avoid duplicating "expr", which may
have side effects and may go out of sync between the two places where
it's used.

FALLTHROUGH is a bit long, a better alternative is PROCEED

Choice: support PROCEED to fall through to the next CASE.

We could also support PROCEED halfway a CASE block:

     SWITCH expr
       CASE 1
         IF cond
            PROCEED
         }
         for_1_only()

       CASE 2
         common_for_1_and_2()

     }

This encourages messy code, let's not support this.


Comments