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

Use nullish coalescing operator (??) for assigning binaries #371

Closed
webmaster128 opened this issue Oct 24, 2021 · 2 comments · Fixed by #376
Closed

Use nullish coalescing operator (??) for assigning binaries #371

webmaster128 opened this issue Oct 24, 2021 · 2 comments · Fixed by #376
Labels

Comments

@webmaster128
Copy link
Collaborator

We generate a lot of code like this:

  fromPartial(object: DeepPartial<ExistenceProof>): ExistenceProof {
    const message = { ...baseExistenceProof } as ExistenceProof;
    message.path = [];
    if (object.key !== undefined && object.key !== null) {
      message.key = object.key;
    } else {
      message.key = new Uint8Array();
    }
    if (object.value !== undefined && object.value !== null) {
      message.value = object.value;
    } else {
      message.value = new Uint8Array();
    }
    if (object.leaf !== undefined && object.leaf !== null) {
      message.leaf = LeafOp.fromPartial(object.leaf);
    } else {
      message.leaf = undefined;
    }
    if (object.path !== undefined && object.path !== null) {
      for (const e of object.path) {
        message.path.push(InnerOp.fromPartial(e));
      }
    }
    return message;
  },

in which the binary fields can be replaced with the nullish coalescing operator (??) as follows:

@@ -429,16 +429,8 @@ export const ExistenceProof = {
   fromPartial(object: DeepPartial<ExistenceProof>): ExistenceProof {
     const message = { ...baseExistenceProof } as ExistenceProof;
     message.path = [];
-    if (object.key !== undefined && object.key !== null) {
-      message.key = object.key;
-    } else {
-      message.key = new Uint8Array();
-    }
-    if (object.value !== undefined && object.value !== null) {
-      message.value = object.value;
-    } else {
-      message.value = new Uint8Array();
-    }
+    message.key = object.key ?? new Uint8Array();
+    message.value = object.value ?? new Uint8Array();
     if (object.leaf !== undefined && object.leaf !== null) {
       message.leaf = LeafOp.fromPartial(object.leaf);
     } else {

This output is easier to read and saves a good amount of code size that is otherwise shipped to the user.

Nullish Coalescing is available since TypeScript 3.7 and will be transpiled to a compact replacement. For targets >= es2020 it will be preserved as valid JavaScript syntax. See also JavaScript operator: Nullish coalescing operator on caniuse.com.

In order to make such a change, a bit of refactoring is needed. Instead of have a separate block for the default initializations, each field should be fully initialized in one step, either with data or with default. But such a refactoring would be nice anyways as it makes the flow easier and future optimizations possible, like this one:

@@ -439,11 +439,9 @@ export const ExistenceProof = {
     } else {
       message.value = new Uint8Array();
     }
-    if (object.leaf !== undefined && object.leaf !== null) {
-      message.leaf = LeafOp.fromPartial(object.leaf);
-    } else {
-      message.leaf = undefined;
-    }
+    message.leaf = (object.leaf !== undefined && object.leaf !== null)
+      ? LeafOp.fromPartial(object.leaf)
+      : undefined;
     if (object.path !== undefined && object.path !== null) {
       for (const e of object.path) {
         message.path.push(InnerOp.fromPartial(e));

I'm happy to work on this if there is general interest on the approach.

@stephenh
Copy link
Owner

Hey @webmaster128 , I think this change would be pretty great; and PRs for it would be even better! :-D Thanks!

@stephenh
Copy link
Owner

stephenh commented Nov 2, 2021

🎉 This issue has been resolved in version 1.84.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Successfully merging a pull request may close this issue.

2 participants