Journiq
Journiq

Reputation: 39

GAS files: how to free global space from polution?

How do I clean up my global space without having to create +1000 lines .gs files

Files and folders used in my project enter image description here

function groupOfFunctions() {
    this.doThis = function() { /* hundreds of lines */ };
    this.doThat = function() { /* hundreds of lines */ };
    // ...
    this.subFunc_N = function() { /* hundreds of lines */ };
    return this; // <-- makes tired face after this
};

enter image description here enter image description here

How do I clean up my global space without having to create +1000 lines .gs files

Articles, blogs, videos etc. are welcome.

Upvotes: 0

Views: 74

Answers (1)

Journiq
Journiq

Reputation: 39

Years later, after reviewing my own question, I conclude that:

  • There was no need for so many files, neither that much of functions. Most of the files were composed of Classes like 'Student, Lesson' that could lack in a project as simple as this one was. I've learned that a project must be as simple as it needs, and time should be spent smartly, with relevant tasks.
  • Proper closuring and the right amount of abstraction would certainly depollute global space. Apps Script's globalThis looks excessive, at first; however, usage has tought me that each item listed after pressing ctrl+space was carefully made easy, by design, because you'll often use them.
  • There's a point where less is more, especially when it comes to abstraction and inheritance. If it's easier and faster to do it manually on Google Sheets, then do it manually; if algorithm is needed, make it simple. Fancy patterns and Youtube videos might give you the wrong impression that you need to code a lot, when you don't.

I used to write code like seen below:

function getSpreadSheet() {//code...};
function getStudentsSheet(){//code...};
funcrion getStudentsDataRange(){
    return getStudentsSheet().getDataRange()}
function getStudentsSheetValues(){
    return getStudentsSheetDataRange().getValues()};
function logValues() {
    console.log( getStudentsSheetValues() );
};

While it could be simply:

function logValues() {
    const id = '...'
    const values = SpreadsheetApp
        .openById(id)
        .getSheetByName('Students')
        .getDataRange().getValues();
    console.log(values);
}

Upvotes: 2

Related Questions