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

enhancement: switching on true instead of false for minifying compiled JS #3670

Closed
blitmap opened this Issue Oct 7, 2014 · 10 comments

Comments

Projects
None yet
5 participants
@blitmap
Copy link

blitmap commented Oct 7, 2014

Hello all, I have a minor suggestion to save only a handful of characters but if the objective is minifying the compiled JS then I believe it is worth sharing. The Coffeescript help/manual/text shows this example:

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'

When I compile it with coffee -p -s and paste in the example I get back:

(function() {
  var grade, score;

  score = 76;

  grade = (function() {
    switch (false) {
      case !(score < 60):
        return 'F';
      case !(score < 70):
        return 'D';
      case !(score < 80):
        return 'C';
      case !(score < 90):
        return 'B';
      default:
        return 'A';
    }
  })();

}).call(this);

To me, it would make sense to switch on true not false so then the compiled JS becomes:

(function() {
  var grade, score;

  score = 76;

  grade = (function() {
    switch (true) {
      case (score < 60):
        return 'F';
      case (score < 70):
        return 'D';
      case (score < 80):
        return 'C';
      case (score < 90):
        return 'B';
      default:
        return 'A';
    }
  })();

}).call(this);

This winds up saving 5 characters (removing the ! from each case and true instead of false).

I don't understand why it switches on false currently -- if there is something I am missing I am all ears. I just thought this might help yield smaller minified JS for some people.

Later! :-)

@michaelficarra

This comment has been minimized.

Copy link
Collaborator

michaelficarra commented Oct 7, 2014

It switches on false to avoid double-negation required when switching on true. A sufficiently advanced minifier should be able to optimise this. I don't think we should be concerned about it.

@jashkenas

This comment has been minimized.

Copy link
Owner

jashkenas commented Oct 7, 2014

You're missing something.

CoffeeScript needs to coerce the conditions to boolean values before comparing, for a value-less switch statement.

In this case, your conditions happen to have boolean values, but we don't know that in advance.

@lydell

This comment has been minimized.

Copy link
Collaborator

lydell commented Oct 7, 2014

Related: #3568 (comment)

(Note that the above issue (/comment) is about the readability of the generated code, not about minifying.)

@blitmap

This comment has been minimized.

Copy link
Author

blitmap commented Oct 7, 2014

@jashkenas I thought the value-less switch could only be used for when expressions that returned a boolean -- could you show me an example of one without?

As far as I know, when there is no value next to switch to use in an === comparison, it should be assumed that the expressions next to when are boolean-yielding?

@jashkenas

This comment has been minimized.

Copy link
Owner

jashkenas commented Oct 7, 2014

switch
  when 1
    "ok"
  when 0
    "fail"
@blitmap

This comment has been minimized.

Copy link
Author

blitmap commented Oct 7, 2014

But this would seem pointless?

@vendethiel

This comment has been minimized.

Copy link
Collaborator

vendethiel commented Oct 7, 2014

As constants, maybe. As return values from functions, no.

@blitmap

This comment has been minimized.

Copy link
Author

blitmap commented Oct 7, 2014

As return values from functions it still makes little sense -- they should be returning booleans if you're treating when like a more concise if-else chain. If they're returning specific non-boolean values to switch over... well then you're not using a value-less switch?

Anyway, I realize this is a weird thing to nitpick over ~ It works as-is, but I tend to obsess...

I'm arguing that the intent behind a valued and value-less switch are different. Coercing the expression to a boolean for the value-less switch isn't necessarily the right thing to do, considering the explicit nature of the switch in the first place with its ===-like comparison?

@jashkenas

This comment has been minimized.

Copy link
Owner

jashkenas commented Oct 7, 2014

Dude -- it doesn't matter if it's pointless. Someone still might write it. We can't be emitting broken code if they do.

@blitmap

This comment has been minimized.

Copy link
Author

blitmap commented Oct 7, 2014

Okay, I can understand that.... :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment