Skip to content
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

Isn't it better to use switch instead of if-elseif ? #8

Open
jodinathan opened this issue Jul 29, 2021 · 0 comments
Open

Isn't it better to use switch instead of if-elseif ? #8

jodinathan opened this issue Jul 29, 2021 · 0 comments

Comments

@jodinathan
Copy link

Take the unpackInt as example:

    if (b <= 0x7f || b >= 0xe0) {
      /// Int value in fixnum range [-32..127] encoded in header 1 byte
      v = _d.getInt8(_offset);
      _offset += 1;
    } else if (b == 0xcc) {
      v = _d.getUint8(++_offset);
      _offset += 1;
    } else if (...) {
    } else {
      throw _formatException('integer', b);
    }

isn't it better if we change it to use switch instead of those ifs?
ie:

    if (b <= 0x7f || b >= 0xe0) {
      /// Int value in fixnum range [-32..127] encoded in header 1 byte
      v = _d.getInt8(_offset);
      _offset += 1;
    } else {
      switch (b) {
        case b == 0xcc:
          v = _d.getUint8(++_offset);
          _offset += 1;
       break;
      default: 
        throw _formatException('integer', b);
    }

I am not sure, but maybe the compiler uses a goto with the switch but not with the if-elseif because of the first if block (b <= 0x7f || b >= 0xe0).

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

No branches or pull requests

1 participant