When to move code to a library

76 views
Skip to first unread message

Michael O'Shaughnessy

unread,
Jun 16, 2021, 8:51:34 PM6/16/21
to Google Apps Script Community
Hello everyone,

I hope I can explain my situation.  I am now at the point in my skill level that I am finding myself using the same code in different projects that I am working on.  For example I have a complete code solution for saving settings to the properties service.  It uses an html page, user enters the info and clicks save.  The code then saves it to the properties service.

I now have 2 web app projects that each use ALMOST the same settings (a setting for sheet id, tab name, column header name, etc)  BUT not exactly the same.  By this I mean I can easily copy the code from one project to the other and change about 3 or 4 lines of code and I am good to go.

Which leads me to my question for the pros here: When do you decide that it is time to move code to a library?

I mean if you all just copy, paste and change the lines you need then that is what I will do.  My "settings" flow just to me is screaming "library".

Any help, guidance and direction is greatly appreciated!

Michael

Alan Wells

unread,
Jun 16, 2021, 10:36:54 PM6/16/21
to Google Apps Script Community
Even if you only need to change 3 or 4 lines of code, that's not generic enough to have your code refer to the library.  The code needs to be totally generic in order to just "plug in" a new update and not manually make any code changes.  App configuration settings need to be in a different place than the generic code.   So, any app settings that a generic module needs shouldn't be in that generic code file.  Separate out settings from generic code.  Either put them is totally different files, or maybe put all the settings at the very top of the file.  But even if you put the config settings at the top of the file, then you can't just copy over that file, because you might be copying one app settings over a different app.  So, if those 3 or 4 lines of code that you need to change are app specific, then they should be somewhere else.  You need to manually look at your app configuration settings for every app if there is a chance that they got changed.

Creating generic module files isn't necessarily directly related to when you'd use a library or not.  It seems like you are considering a way to automate updates in order to avoid mindless copy and paste, time consuming work.  If you want to be able to update a generic module to multiple target projects, and you use a library, then you'd need to either use the "head" version or you'd need to change the version number in the setting that is referencing the library.  You wouldn't want to use the head version unless you had separate library files for development and production.  If you used the head version, then any saved changes in the library would immediately be deployed to everywhere it was being used.  That could be a disaster.  Using the head version would avoid the need to update the library version number, but then you'd need to copy your development library file over the production library file.  So, either way, there is work to be done to implement the update.  If you wanted to avoid even the task of manually updating the version number.  It is possible to use the Apps Script API to programmatically update the appsscript.json / manifest file, which would automate the library version update process.  But then you'll need to find/create some code to do that.  Even the "professionals" may not have a totally automated way to update changes of generic modules to their production files in one click.  This is an interesting subject, and I don't know of any public/popular app to totally automate App Script files to every place where some generic module is used.
I think that the real issue is automating updates, or making the code updating process as easy as possible.  You might think that using a library would eliminate the need to keep a list of every project where that code was being used.  But, if you need to update the library version number either in the manifest file, or go into the project manually, then you still need a list of every project where it's being used.   You don't need to use a library to automate or programmatically update code, but you'd need to use the Apps Script API.  So, the real help for taking a lot of the work out of updates is by using the Apps Script API to overwrite content in the target file.
Lot's of people use GitHub if they need to collaborate with other programmers on files, and there are ways to trigger updates from repositories.  But again, I don't know of any complete, well explained system that incorporates all these possibilities.  
Hopefully this has given you some information even if I haven't provided a fully automation solution for you.

Adam Morris

unread,
Jun 17, 2021, 7:07:26 AM6/17/21
to google-apps-sc...@googlegroups.com
Hi Michael,

Intriguing question. 

I come from the school of thought that the usage of libraries needs to be one of the first things programmers start doing, once they are comfortable with general syntax. 

There are problems that others have solved and it’s great to use that as a building block. 

Writing them, however — while the basic structure is simple enough —is one of the more challenging things a programmer can do. They need to be written so that someone else can send them certain information … a setting object, or some parameters … and it behaves a certain way and is dependable. That necessarily means using concepts that are considered advanced, like design patterns or meta programming. You’re basically working more abstractly, so you need more abstract tools. 

Like most things, it comes down to resourcing. Would you save yourself time by writing xyz as a library, instead of copying and pasting code. What if you find an issue and want it to work a bit differently? How much of a time sync would it be to update it eveywhere instead of in one place. 

Speaking for myself, I wrote some and have been using them constantly, nearly everyday. I was able to complete a project within a day that would probably take me a week without this library. If something goes wrong with them, there’s an easy way to do version control. 

I used to use node and a local environment, but now I just use the IDE. I have a testing library for bigger projects, which helps me sanely build things without having to constantly hit the play button with different situations. The testing suite runs the test and reports the results. 

Not all the stuff I wrote I ended up using all the time, but it was definitely worth the effort. 

Hope libraries are a big part of our ecosystem!

Adam

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/baaa0f8e-18c4-47f7-9f9b-19fafe14edacn%40googlegroups.com.
--

Václav Petrák

unread,
Jun 17, 2021, 7:26:30 AM6/17/21
to google-apps-sc...@googlegroups.com
Hi Michael,

I wouldn't go the library route on this one.
If I understand correctly, it is an HTML Service.
So I would use a single Project that works with multiple workbooks or resources for loading and saving. I would keep the Select for client side selection as needed or split it according to the parameter in the application url.
I would handle the different parts in If-else blocks on both Client and GAS side.
Sorry for the bad English.
(Although DeepL.com translates really amazingly😀 )

Venca

Václav Petrák
 
 
m .:  +420 724 604 367
e-mail:  waclav...@gmail.com


čt 17. 6. 2021 v 13:07 odesílatel Adam Morris <classroomte...@gmail.com> napsal:

Bez virů. www.avast.com

Michael O'Shaughnessy

unread,
Jun 18, 2021, 6:30:45 PM6/18/21
to google-apps-sc...@googlegroups.com
Wow!  Thank you all for your input!

@Alan - You are correct, I am thinking about how to "update" several projects and you are correct that using the "head" version for the library is not the way to go.  I do use the Github assistant and I am learning a lot more on how to use it effectively.  And I do understand that a library needs to be a "black box".  It should take info in, do it's work and give info back... consistently and in a consistent manner.  This is not what my "settings solution" would look like.

@Adam - Like Alan was saying, you put beautifly: "They need to be written so that someone else can send them certain information … a setting object, or some parameters... and it behaves a certain way and is dependable".  Looking at other libraries (like those from Bruce McPherson) I can see that they need to be generic and have a specific purpose.  Again, not what my "settings solution" is.  And yes, I will investigate the use of libraries in my apps script projects.

@ Václav - First, I understood everything that you wrote! And your reply got me thinking of a different approach.

So, after reading and thinking about all of this, what I have come up with is the following: I did my best to "abstract" everything I could.  I have a settings.html file with the JS and CSS in separate files. I have  a settings.gs file that handles the settings work.
In that file I have a  "configuration" object that contains all the things that I would need to change from project to project.  This object then can be passed to my settings code to set the info needed for the project (sheet ID, sheet name, etc).  I then pushed them all to a Github repo.

Now if I ever need to use them again it is a quick "copy and paste" from Github AND a quick change to just one spot in the configuration object and I am good to go.

Thank you all for your input!


Andrew Roberts

unread,
Jun 19, 2021, 5:58:08 AM6/19/21
to google-apps-sc...@googlegroups.com
If you do come up with Libraries you want to share, drop me a line and I'll add it to this database.

Joshua Snyder

unread,
Jun 21, 2021, 5:57:52 AM6/21/21
to Google Apps Script Community
Adam it'd be really interesting to get more information about the testing library that you have built. I am looking to increase the amount of automated testing we are doing for our card based add-on so would be great to know more about this from you.

I have previously tried a number of GAS unit testing options and didn't find them very useful...

Adam Morris

unread,
Jun 21, 2021, 8:15:31 AM6/21/21
to google-apps-sc...@googlegroups.com

Joshua Snyder

unread,
Jun 21, 2021, 10:50:40 AM6/21/21
to Google Apps Script Community
Thank you! I will be sure to use this in some of my projects :)
Reply all
Reply to author
Forward
0 new messages