0

I'm returning to gapps with sheets after a long break and an upgrade to the V8 engine.

When using function declarations I get the issue of the function being overwritten by a 2nd declaration such as below as an example (I know overloading is not supported) if the same function name is accidentally reused.

function stuff() {
  return (`I'm doing stuff`);
}

function stuff() {
  return (`I'm doing stuff and things`);
}

function __test_stuff() {
  Logger.log(stuff());
}
// logging : "I'm doing stuff and things"

There can be a 2nd ( or n'th ) declaration which can be another .gs file in the project and the code will run without error, but not sure which code is running!

Should I be using const function expressions as

const stuff = function stuff(e) {
  return ("I'm doing stuff, things and much much more");
}

These give an error when trying to run with multiple declarations SyntaxError: Identifier 'stuff' has already been declared (line 1, file "stuff")

Is this a good thing as it stops the function being overridden or are there gotcha's waiting in the wings?

nb. I have been splitting code into separate .gs files per function as my eyes were glazing over. Again is the a good thing?

pxbaker
  • 51
  • 6
  • 1
    Why are you giving multiple functions the same name anyway? GAS doesn't support real overloading, although there is a workaround that sort of works. – J. G. Aug 26 '20 at 17:54
  • 1
    Yes. You shouldn't be using the same name. Using the same name is a great way to mess a lot of stuff up. While you're at it, add `'use strict'` to your functions too. – TheMaster Aug 26 '20 at 21:04
  • 1
    The gotcha is that it's no longer hoisted. And yes const is better. – pguardiario Aug 27 '20 at 05:34
  • I know GAS doesn't support overloading ( the example duplicate declaration is to show overloading is not supported ), hence my question is how can I protect the definition when the same function can be defined in another .gs file. Should I be using "const" in order to ensure there is only one – pxbaker Aug 27 '20 at 08:30
  • Hi there @pxbaker! I understand that you want to overload the code, but I want to know why you need it. Please, share with us what is the final goal of this approach, so that we can help you find good alternatives to it. – Jacques-Guzel Heron Aug 27 '20 at 09:54
  • 1
    To clarify, I **do not** want to overload, I want to protect against a function being declared in more than one place as I have code in multiple .gs files. Using a function expression with 'const' which is available with V8 runtime does this. I wanted to know if there is a downside to doing this such as not hoisted as pointed by @pguardiario above. – pxbaker Aug 27 '20 at 11:08
  • Sorry, hoisted functions are a bad thing - I wasn't clear. There's no downside to const. – pguardiario Aug 27 '20 at 12:30
  • 1
    @pguardiario I disagree it being a bad thing. It's a JavaScript feature. That's all. – TheMaster Aug 27 '20 at 13:15
  • I'll just leave this [here](https://github.com/airbnb/javascript#functions--declarations) for anyone who agrees with @TheMaster – pguardiario Aug 28 '20 at 00:26
  • @pguardiario Airbnb is a opinionated style guide. It is not a authoritative reference. For eg, in your same link, it provides a reference link to eslint: [`func-style`](https://eslint.org/docs/rules/func-style), where it says *Due to these different behaviors, it is common to have guidelines as to which style of function should be used. There is really no correct or incorrect choice here, it is just a preference.* Unlike other languages, JavaScript provides a considerable amount of freedom, which may come in handy or lead to errors, but you're allowed to make your own choice. – TheMaster Aug 28 '20 at 02:11
  • @TheMaster, yes do it however you want just be aware you will be asked to knock it off if you ever get a job at airbnb :) – pguardiario Aug 28 '20 at 02:16
  • @pguardiario The same goes wherever you get a job. Each corporation will have it's own rules: tabs vs spaces. 2 spaces vs 4 spaces. They all have a opinion and it's ok-because with a team, others should be able to read your code in a consistent fashion. Related: https://stackoverflow.com/a/51033698 – TheMaster Aug 28 '20 at 02:20

1 Answers1

1

You should be aware that there are some open issues related to debugging code in the Google Apps Script editor when using the default runtime (V8).

Besides the above the same advices that apply to JavaScript apply to too Google Apps Script, I.E. Proper use of const for defining functions in JavaScript

Some important terms that might help you:

function declaration

function myFunction(){}

function expression

const aFunction = function(){}

The same applies when using var, let or even nothing (when strict mode is not used) instead of const

Resources

Rubén
  • 34,714
  • 9
  • 70
  • 166