Skip to content

Remove most of the use of else after return from JS library code. NFC #17450

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 1 commit into from
Jul 18, 2022

Conversation

sbc100
Copy link
Collaborator

@sbc100 sbc100 commented Jul 15, 2022

This is small code size win but it looks like it only effects the
unoptimized (non-closure) build. Still good coding style anyway.

I noticed this while working on #17449 and wrong a little command line
to try to find all these:

$ pcregrep -n -M 'return[^\n]*;\n\s*}\s*else\s*{' src/*.js

@sbc100 sbc100 requested a review from kripken July 15, 2022 18:27
@sbc100
Copy link
Collaborator Author

sbc100 commented Jul 15, 2022

The code size benefits don't really show here because we don't include most of these JS libraries as part of our codesize tests (perhaps we should measure the size of INCLUDE_FULL_LIBRARY?)

@@ -645,12 +643,12 @@ var LibraryEGL = {
eglGetCurrentSurface: function(readdraw) {
if (readdraw == 0x305A /* EGL_READ */) {
return EGL.currentReadSurface;
} else if (readdraw == 0x3059 /* EGL_DRAW */) {
}
if (readdraw == 0x3059 /* EGL_DRAW */) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find else-if actually easier to read for a thing like this, where it's a series of ifs on the same thing. Do you disagree?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe marginally easier to read in some cases. But I think that benefits outweigh the costs:

  1. less code
  2. less indentation
  3. Single rule for all cases ("else after return is always wrong") keeps things simple.

Since tidying up this stuff in various places I've trained my self to see else after return a code smell, so for me the old code now looks wrong :)

@@ -645,12 +643,12 @@ var LibraryEGL = {
eglGetCurrentSurface: function(readdraw) {
if (readdraw == 0x305A /* EGL_READ */) {
return EGL.currentReadSurface;
} else if (readdraw == 0x3059 /* EGL_DRAW */) {
}
if (readdraw == 0x3059 /* EGL_DRAW */) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(and here)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I'm happy to revert those 3 cases for now, if you are OK with the rest of the change landing?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't feel strongly, I guess, if you prefer the other form.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reverted the three places you mentioned..

@sbc100 sbc100 force-pushed the UTF8ArrayToString branch from 9fd6dc5 to 9c9398d Compare July 17, 2022 23:06
@sbc100 sbc100 force-pushed the else_after_return branch from f5d9b1e to ccc8f36 Compare July 17, 2022 23:10
Base automatically changed from UTF8ArrayToString to main July 18, 2022 20:52
This is small code size win but it looks like it only effects the
unoptimized (non-closure) build.  Still good coding style anyway.

I noticed this while working on #17449 and wrong a little command line
to try to find all these:

```
$ pcregrep -n -M 'return[^\n]*;\n\s*}\s*else\s*{' src/*.js
```
@sbc100 sbc100 force-pushed the else_after_return branch from ccc8f36 to f05f835 Compare July 18, 2022 21:00
@sbc100 sbc100 enabled auto-merge (squash) July 18, 2022 21:00
@sbc100 sbc100 merged commit a3d8979 into main Jul 18, 2022
@sbc100 sbc100 deleted the else_after_return branch July 18, 2022 22:15
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.

2 participants