0

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 :)

1
  • Scenario A and B are not the same scenarios. By the very nature of scenario B, one method relies on the other. In the first scenario they are both independent. So I would make the argument that it is difficult to make a generalized statement of what is "better", when comparing situations that are not the same. Commented May 18, 2020 at 18:14

2 Answers 2

2

A and B are not the same function. B is a parameterized function. So, logic should be like if we already have the cell value, then call B and if we don't have the cell then call A.

Now, to your question yes, option B is more appealable as as it let you avoid code repetition. But I don't see the purpose of calling B from A in this scenario.

In my opinion, it would be much better if you write a separate function to get the cell so, that the code abide by one function one purpose(SRP).

Below is the working snippet:

 //a function that return default cell value if called
 function GetCell(){
    let ss = SpreadsheetApp.getActiveSpreadsheet();
    let sheet = ss.getActiveSheet();
    return sheet.getCurrentCell();    
  }

 function A() {
    let cell=GetCell(); //get cell value by calling function
    cell.setValue("Hello World");
 } 


//function B execute successfully with parameter or without parameter. 
 function B(cell) {

   if(typeof cell == "undefined") {
    cell=GetCell();
   }
   let txt = "New Text";
   cell.setValue(txt);
 }
Sign up to request clarification or add additional context in comments.

1 Comment

That makes sense. My real scenario is I'm running a function (A) that can take either two directions (depending on an if/else statement): in the first it will run function B, in the second function C. All three functions use the same variables (spreadsheet id, current cell, etc.) as they are based on the same spreadsheet hence I was wondering which procedure is better. But I like that idea of "one function one purpose". Thanks!
0

How about this:

function A() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
  var cell = sheet.getCurrentCell();
  cell.setValue("Hello World");
  return cell;
}

function B(cell) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
  var cell = sheet.getCurrentCell();
  var txt = "New Text";
  cell.setValue(txt);
}

function runMe() {
 B(A);
}

1 Comment

Can you do that in Javascript? Call a function with another function as argument? I didn't even know I'll try, thanks :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.