Why You Should Use Global Functions

By Michael Sidenstick, Programmer/Developer

An underutilized but very important functionality built into Ninox is the ability to define functions that will work anywhere in a database. If your database has multiple fields with similar or the exact same chunks of code present, global functions give you the ability to save time, coding and resources by bringing all of the similar code to one spot. To create a global function, just open a database and use the red wrench in the top right corner to enable administrative mode. Then, click the options tab at the top of the page, and click “Global script definitions” to open the area for global functions. If you don’t know what a global function is, the “Global Functions and Variables” video on the Nioxus YouTube Channel is very helpful for understanding how to make these functions. Instead of discussing how to create these functions, I want to talk about why they can be useful and the programming concepts behind functions.

Functions are very useful when used correctly in large codebases– in nearly every programming language, functions are crucial in developing reusable code quickly and effectively. If we had a 1,000-line program, we could break the huge chunk of code into many functions that each tackled a single task, which would reduce the number of total lines and organize the code. In Ninox, if we wanted to create four formulas that would pull from four tables, in many cases, we could write a single global function that would perform this task for each of those four cases. Then we could call the global function with the dynamic data, and not have to copy and paste any code or rewrite anything.

 Furthermore, if we had an error with a part of our code, and the code was copied and pasted in multiple places, it could be difficult to find all of the errors. When a global function is used, there is one central place where the error would originate, so you would only have to change the code in the global function itself. Furthermore, when there is an error with a part of a task, if the different tasks are sorted into functions, finding where an error takes place is much easier when the tasks are separated into functions instead of in one large block of code. If you have a codebase with hundreds of lines of code or multiple functions that accomplish the same (or similar) task(s), using global functions to organize your code requires only a small investment of time, but provides a huge return on investment in the form of ease of use in the future. Global functions are well worth the effort… give them a try.

en_USEN