« first day (501 days earlier)      last day (868 days later) » 

8:07 AM
@OlegValter I don't think this will work.
````
function doSomething1() {
/*needs ss*/ const ss = SpreadsheetApp.openById(/*SPREADSHEET_ID*/);
/*needs sheet1*/ const sh1 = ss.getSheetByName('Sheet1');
}
function doSomething2() {
/*needs ss*/ const ss = SpreadsheetApp.openById(/*SPREADSHEET_ID*/);
/*needs sheet1*/ const sh1 = ss.getSheetByName('Sheet1');
}
function doSomething3() {
/*needs ss*/ const ss = SpreadsheetApp.openById(/*SPREADSHEET_ID*/);
/*needs sheet1*/ const sh1 = ss.getSheetByName('Sheet1');
}
````
Say each of these functions need independent access to variables ss and sh1
The point of that question is
@OlegValter Global variables solve this problem by declaring ss and sh1 globally.
With your function, is violated:
`
const cache = (callback) => {
const c = (...args) => {
return (c.memo = c.memo !== void 0 ? c.memo : callback(...args));
};
return c;
};
function doSomething1() {
/*needs ss*/ const ss = cache(() => SpreadsheetApp.openById(id));
/*needs sheet1*/ const sh1 = ss.getSheetByName('Sheet1');
}
function doSomething2() {
/*needs ss*/ const ss = cache(() => SpreadsheetApp.openById(id));
/*needs sheet1*/ const sh1 = ss.getSheetByName('Sheet1');
}
function doSomething3() {
/*needs ss*/ const ss = cache(() => SpreadsheetApp.openById(id));
This isn't less verbose and it's a violation
if you use const ss = cache(() => SpreadsheetApp.openById(id)); in global scope. You aren't doing anything different from ` /*needs sconst ss = SpreadsheetApp.openById(id);`
if you use const ss = cache(() => SpreadsheetApp.openById(id)); in global scope. You aren't doing anything different from adding const ss = SpreadsheetApp.openById(id); in global scope
In addition you're making a privileged call in the global scope
 
 
1 hour later…
9:30 AM
I think you completely missed the point then :)
DRY is violated only by the examples you gave
cache returns a function, not the result
const getSpreadsheet = cache(() => SpreadsheetApp.openById(id));
then, when one needs it, they call getSpreadsheet
A closure is created, and there is no pollution of the global scope
const getSpreadsheet = cache(() => SpreadsheetApp.openById(id));

function doSomething1() {
const ss = getSpreadsheet();
const sh1 = ss.getSheetByName('Sheet1');
}
function doSomething2() {
const ss = getSpreadsheet();
const sh1 = ss.getSheetByName('Sheet1');
}
function doSomething3() {
const ss = getSpreadsheet();
const sh1 = ss.getSheetByName('Sheet1');
}
if one needs to cache multiple spreadsheets, cache can be extended by using an instance of Map and a second parameter - hashing function that determines the unique key of the result after getting it the first time
that's why I created the TypeScript playground above - to show that closure works correctly
our approach, as I mentioned, is not very different, although you use the sandbox pattern directly instead of using the function as the sandbox
the funniest thing of all is that normally, I do not even bother to do the above :) Unless there is something perfomance-critical, I find little use to cache the getters. At this point, one is better off using the API directly
 
10:22 AM
my gripe is with the question, frankly - trying to solve the DRY problem by making global bindings is a... bizarre choice :)
 
10:50 AM
It was a mistake on my part. I understood how the cache function works, but failed to accurately reproduce it in my post. But, still here, you're repeating const ss= getSpreadsheet() thrice. That's the issue. For say 5 different data type variables, This will be much complicated and those lines needs to be repeated 5x3 times per function = 15 times total.
I agree that performance part is satisfied, even though you might have 15 lines of same code.
*1time per function for 3 functions
 
I see
well, I think this is at the point of applying DRY for DRY's sake with no clear benefit
reuse is good, but it should be measured too, IMO
there are some things one has to repeat if they have, well, repeating operations
my main concern with the OPs idea of DRY code is that they want to introduce a globally shared state for an extremely minor (I would say, premature) optimization
I think there is a general misunderstanding (which is clearly shown by the OP's question) of what the DRY principle means. Last time I checked, it did not mean reducing lines of code (although it often does as a side effect), but avoiding making the same operations over and over
 
11:10 AM
@OlegValter Contrast your solution with a global:
const ss = SpreadsheetApp.openById(id);

function doSomething1() {
const sh1 = ss.getSheetByName('Sheet1');
}
function doSomething2() {
const sh1 = ss.getSheetByName('Sheet1');
}
function doSomething3() {
const sh1 = ss.getSheetByName('Sheet1');
}
@OlegValter Although I never polluted global scope in production environments, It's a great advantage to have, IMO. Could you show the problems with this method?
As long as you're careful to not over write variable names, you should generally be good. Other than permissions issue, I can't find any cons. Weighing pros against cons, especially when you need say 5 or more shared variables across 5 or more functions, I can't say globals is a bad idea. It significantly improves readability as well as workflow. Without a cacher
 
11:48 AM
@TheMaster 1. Depends on the parsing order of files until GAS gets modules (ever?) - which either means we have to keep the globals in a special file that is always parsed first, or use build tools to ensure that. 2. Potential for name clashes. 3. An unnecessary long (and potentially unused) lifespan of a resource (with the only ability to clear if declared with let). 4. Contract violation - functions depend on more from the global scope than they should (aka scope bleed).
regarding #4:
a proper implementation, IMO, would be
function doSomething1(ss = getSpreadsheet()) {
    const sh1 = ss.getSheetByName('Sheet1');
}
everything else actively lies about the contract of the function
oh, writing this, I remembered another way how I tend to "cache" (more like "share" the resources)
function doSomething1(ss) {
const sh1 = ss.getSheetByName('Sheet1');
}
function doSomething2(ss) {
const sh1 = ss.getSheetByName('Sheet1');
}
function doSomething3(ss) {
const sh1 = ss.getSheetByName('Sheet1');
}

const main = () => {
    const ss = SpreadsheetApp.getActiveSpreadsheet();
    doSomething1(ss);
    doSomething2(ss);
    doSomething3(ss);

};
does not lie about the contract of the functions, does not pollute the global scope, does not need complex caching
assuming there is a point where they can get the shared resource from, of course
if not, fallback to ss = getSpreadsheet()
again, I am not in any way against your approach, but against the OP's
and if we get ESM 6 to 8 time units later, then global scope will stop being that of a problem
 
 
3 hours later…
3:08 PM
@OlegValter The cons are seemingly minor compared to workflow and actual code size burden. I usually pass around variables as you've shown. I've passed around 5 variables around and it gets messy real fast. That's when I actually started to wonder whether globals are better/ easier.
At the end of the day, there are already globals like Math, Utilities and SpreadsheetApp. I think if your project requires a another object so much, I think it's ok, provided you safeguard code against the pitfalls you've shown.
@OlegValter Why so?
 
 
1 hour later…
4:39 PM
@TheMaster scope isolation :) if you recall from the spec, modules have their own isolated scope, and they address exactly the reasons why we keep telling folks "do not use globals"
@TheMaster well, I tend to treat those globals as "necessary evil" due to the history of JS
ideally, they should be stdlib packages/modules imported on demand
 
5:15 PM
honestly, that is why I wait for the normalization of ESM so much - they basically legitimize normal scope usage. I see the following perfectly fine in a module:
const ss = SpreadsheetApp.getActive();

const do1 = () => ss.getSheetByName("test");

do1();
 
 
2 hours later…
7:16 PM
@OlegValter FWIW, We barely use any libraries in apps script. So, global scope pollution is a very minor issue. First the four issues you listed also apply for two global variables you introduced: getSpreadsheet and cache. Right? There's is potential for name clashes and the cache stores the reference longer than needed. Parsing order would also matter here. And the function is dependant on cache.
Contract violation doesn't happen, if the function uses more of it's own resources. Right?
The argument against globals is weak, if that's all there is. That question's OP only introduced two global variables in it's place ss and sheet(I think)
@OlegValter I agree on that.
 

« first day (501 days earlier)      last day (868 days later) »