Bug 387294 - Add 'elseif' syntax and simplify the case statement
Summary: Add 'elseif' syntax and simplify the case statement
Status: NEW
Alias: None
Product: z_Archived
Classification: Eclipse Foundation
Component: EDT (show other bugs)
Version: unspecified   Edit
Hardware: PC Windows XP
: P3 enhancement (vote)
Target Milestone: ---   Edit
Assignee: Project Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-08-15 11:52 EDT by Matt Heitz CLA
Modified: 2017-02-23 14:05 EST (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Matt Heitz CLA 2012-08-15 11:52:21 EDT
The most natural way to write code that checks several conditions is a series of nested if-else statements, like this:

if ( today == "Tuesday" )
  fireworks();
else
  if ( itIsMealTime() )
    eatLunch();
  else
    if ( x > y + z )
      fightMonster();
    else
      readBook();
    end
  end
end

I don't like the way the amount of indentation keeps increasing even though the conditions are logically at the same level.  If you decide not to indent, you end up with this:

if ( today == "Tuesday" )
  fireworks();
else if ( itIsMealTime() )
  eatLunch();
else if ( x > y + z )
  fightMonster();
else
  readBook();
end end end

The chain of end's in the last line is an abomination.


We have a fairly clean way to handle this situation, using one form of our case statement:

case
  when ( today == "Tuesday" )
    fireworks();
  when ( itIsMealTime() )
    eatLunch();
  when ( x > y + z )
    fightMonster();
  otherwise
    readBook();
end

This is fine, but as I said before an 'if' statement is the most natural way to do it.  I bet most EGL programmers (in EDT and RBD) wouldn't even think to use a case statement for this.

My solution is to introduce a new 'elseif' keyword, which will allow people to write code like the following, which is very intuitive:

if ( today == "Tuesday" )
  fireworks();
elseif ( itIsMealTime() )
  eatLunch();
elseif ( x > y + z )
  fightMonster();
else
  readBook();
end

Many languages have syntax like 'elseif', including Perl, Python, PHP, Visual Basic, and some Unix shell-scripting languages.  It's even in the C pre-processor as #elseif.

As a bonus, the form of case statement mentioned above would become redundant, so we could remove it.  We'd keep the other kind of case, which is like a switch in Java or C.  With only one form of case statement, EGL becomes a simpler language.  We can tell people "the EGL case statement is like the switch statement in other languages."  Easy!

Finally, we might represent if-elseif's in the IRs as nested if-else-if's.  That means the generators and debuggers wouldn't need to change.
Comment 1 Matt Heitz CLA 2012-08-15 11:54:40 EDT
By the way, some of the languages with syntax like 'elseif' spell it 'elsif' or 'elif'.  I'm not opposed to 'elif' but I think 'elsif' would lead to lots of syntax errors as people accidentally include the second 'e'.