3

Sorry if this a stupid question, but here goes

So I've been learning Javascript for use with Google Application Scripts for about a year now and slowly but surely am finding my feet.

I tried to help someone with their script on here, and i notice that they declared

var ss = SpreadSheetApp.getActiveSpreadsheet();

at the very top of the script, outside all functions, as a global var.

It got me thinking, that when I write several functions/scripts for a spreadsheet, it might be worth declaring some VARs globally rather than repeating them in different functions.

Before I blindly plough down this path, I thought it best to ask are there any major pitfalls or problems with using global vars in GAS.

Also, are there any major advantages, besides saving a bit of typing while coding?

Does anyone currently write GAS scripts, regularly using global vars. I'd be interested to hear how it all works? What the cons are, any limitations, or advantages.

EDIT BELOW THIS LINE

Just wanted to add, 95% of the things I've been doing have been confined to Google Sheets, with a lil gmail scripting. So that's my scope so far. Thought it best to mention, as I don't really have the implications for scripts for other Google products in mind.

Munkey
  • 958
  • 11
  • 28
  • Yay, no down votes. YET. Hope I'm not tempting fate but perhaps it wasn't such a foolish question after all – Munkey May 03 '15 at 15:39
  • I'm really not sure what to vote as best answer, as all have great merit. I shall leave it a little bit and see what the votes look like and go with the consensus. But all have helped greatly, just means I have to go off and do research and try a few things out :) – Munkey May 04 '15 at 19:16
  • Thank you for your decision :-) As you say all three are valuable, I fully agree with that. – Serge insas May 10 '15 at 12:43
  • @Sergeinsas this has been quite tough, all answers contribute towards the question. Am grateful for Sandys `This` example too. I'd like to see examples of Johnathan's single global var gs file and how it's used. But a clear cut downside is the more global vars you have, they get called everytime. Script turns to mud. – Munkey May 10 '15 at 12:55

2 Answers2

3

When you use a variable definition such as in your example that calls a Google Service you have to be conscious that this call will be executed each time you run any function in your project.

This means that even if it's probably efficient in terms of code writing (we are all lazy guys I guess) it is really not efficient in terms of execution speed.

On the contrary, let's imagine you execute a function that does not need the ss variable, the SpreadSheetApp.getActiveSpreadsheet() will be executed...

With a simple call like that it might not be an issue but if you multiply this case for a lot of different variables calling many different services it will finally slow down your execution speed.

I'm not a 'real' programmer, so my opinion is only based on personal habits and findings since I never learned anything "academic" in that matter. Maybe someone else will have a more relevant answer.

EDIT : while I was typing Jonathon just did ! I do agree with what he said of course and I'll leave my post here just for the efficiency stuff ;-)

Serge insas
  • 45,904
  • 7
  • 105
  • 131
  • Thanks for the answer. On small scripts/functions it wouldn't matter so much then I guess. But as you added to that script/function. IT could potentially increase execution speeds above the 5 minute limit if I'm not careful. Noted – Munkey May 03 '15 at 13:54
  • I wasn't even imagining reaching the 5 minutes limit (that would be really a **lot** of variable declarations !) but more simply I care about the responsiveness of the code for my "user satisfaction" ;-) – Serge insas May 03 '15 at 13:58
  • 3
    This is the better answer because it mentions a gas-specific issue that is the most important here regarding the variable getting recreated every time you call the script from the interface because in gas all globals are reevaluated, as opposed to a client side javascript where globals only evaluate on page entry – Zig Mandel May 03 '15 at 15:50
  • it is the better answer in that respect. in stackoverflow world this not a great question as mostly it is an opinion piece – JSDBroughton May 05 '15 at 10:16
2

There is a purist view and a pragmatic view.

Pragmatically the disadvantages you may face

  • naming conflict potential with any libraries you attach
  • losing track of globals defined in multiple scripts in a project
  • variables calling API endpoints will always run whether that instance of your script requires them or not.

I do use globals, but in a slightly conservative way to counter both of these.

  • I create a single global object that is uniquely named and then hang any global vars as properties of that object. Essentially globals are then namespaced.
  • I discipline myself to define globals only in a single globals.GS per project.

Puristic concerns about variable scope matter less than js in the wild as the scripts are somewhat sandboxed at runtime. This is not the case when you are publishing libraries however.

I do not define globals like the example you give however tying a variable to a specific GAS API. There maybe speed advantages to doing it, I don't know, but I tend to reuse code patterns as functions between functions and prefer to not require myself to copy over any code outside of scoped functions when doing so. Also they will call those API endpoints whether your target function requires them or not = speed penalty.

Globals for me are only truly script project specific variables and utility functions. Oftentimes they could be calls to script properties.

JSDBroughton
  • 3,966
  • 4
  • 32
  • 52
  • @Jonathan - thanks for the reply. You spelled Vars as Cars but it wont let me edit. I've not used libraries yet (I'm still in the shallow end of the pool), most of my scripts are single code projects that are container bound. – Munkey May 03 '15 at 13:49