I want to run function B after function A, but use some variables from function A when I run it. Let's say for the sake of this exercise that I don't want to declare global variables.
Would it be "better" code to write those variables in both functions (scenario A), or to have local variables in function A and pass them as arguments to function B (scenario B)?
Here's a representation of those two scenarios:
Scenario A
function A() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var cell = sheet.getCurrentCell();
cell.setValue("Hello World");
}
function B(cell) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var cell = sheet.getCurrentCell();
var txt = "New Text";
cell.setValue(txt);
}
Scenario B
function A() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var cell = sheet.getCurrentCell();
cell.setValue("Hello World");
B(cell);
}
function B(cell) {
var txt = "New Text";
cell.setValue(txt);
}
In a recent project I've been working on, I have dozens of variables and scenario B seems "cleaner" cleaner to me, but I really don't know if I should be doing that.
I know this is basic, but I'm still rather new to coding in JavaScript and I can't get my head around this. Any help would be appreciated!
The code above is in Google Apps Script btw.
Thank you :)