Skip to content

Documentation for switch statements with no control expression #2417

New issue

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

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 5, 2013
Merged

Documentation for switch statements with no control expression #2417

merged 3 commits into from
Mar 5, 2013

Conversation

gabehollombe
Copy link
Contributor

Added the following to the docs in the Switch section:

Switch statements can also be used without a control expression, turning them in to a cleaner alternative to if/else chains.

score = 76
grade = switch
when score < 60 then 'F'
when score < 70 then 'D'
when score < 80 then 'C'
when score < 90 then 'B'
else 'A'

grade == 'C'

@jashkenas
Copy link
Owner

While legal, I think this would usually be an abuse of switch. Why not just use if/else for this? Because you prefer to type when instead of else if ? Performance?

@marcandre
Copy link

+1.

A 'switch' statement is very descriptive: from the get go you know that at most one of many conditions will take place. I find it more expressive and harder to get confused. There's also only one way to indent it, compared to nested else-if that could be indented in different ways.

Other argument: what else could/should @gabehollombe 's code be translated to?

Finally: it works now, so for compatibility... it should be supported in the future too.

@gabehollombe
Copy link
Contributor Author

Why not just use if/else for this? Because you prefer to type when
instead of else if ? Performance?

I feel the switch format reads a lot nicer. Less keywords (if/else) on the
screen makes the intent of the assignment easier to parse, for my brain at
least.

On Fri, Jul 6, 2012 at 9:36 PM, Jeremy Ashkenas <
[email protected]

wrote:

While legal, I think this would usually be an abuse of switch. Why not
just use if/else for this? Because you prefer to type when instead of
else if ? Performance?


Reply to this email directly or view it on GitHub:
#2417 (comment)

@marcandre
Copy link

PS: Forgot to mention that Ruby supports this style of switch too...

@sergeych
Copy link

+1

this is cleaner and more convenient way than 'else if' chains. Less noise, more sense. Pls include ;) 'Least surprising' principle, as in ruby, where this construction is widely used (while ruby hardly misses chained comaprisons ;)).

@epidemian
Copy link
Contributor

+1 for documenting empty switch statements. -1 for this particular example. I'd prefer it written as:

score = 76
grade = switch
  when score < 60 then 'F'
  when score < 70 then 'D'
  when score < 80 then 'C'
  when score < 90 then 'B'
  else 'A'

It can also be written as a cascade of return ... if ... if you put it inside a function (see this is SO question for reference)

@justinclift
Copy link

@epidemian That does look a lot clearer than the initial proposal by @gabehollombe. Shows the common usage for ranges very nicely.

@gabehollombe Any objections to using @epidemian's approach instead?

@gabehollombe
Copy link
Contributor Author

No objections at all. =-)

On Sun, Oct 7, 2012 at 5:33 AM, Justin Clift [email protected]:

@epidemian https://github.com/epidemian That does look a lot clearer
than the initial proposal by @gabehollombehttps://github.com/gabehollombe.
Shows the common usage for ranges very nicely.

@gabehollombe https://github.com/gabehollombe Any objections to using
@epidemian https://github.com/epidemian's approach instead?


Reply to this email directly or view it on GitHubhttps://github.com//pull/2417#issuecomment-9200932.

@justinclift
Copy link

@gabehollombe Heh, cool. Any chance you can update the pull request? :)

jashkenas added a commit that referenced this pull request Mar 5, 2013
…trol_expression

Documentation for switch statements with no control expression
@jashkenas jashkenas merged commit 304432c into jashkenas:master Mar 5, 2013
@marcandre
Copy link

👏

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

Successfully merging this pull request may close these issues.

6 participants