Best Practices

This page offers guidelines for writing efficient and maintainable scripts in Joget. It covers key tips such as optimizing performance, managing exceptions, and adhering to security standards, helping you ensure your scripts are reliable and scalable.

1. Only import classes that are needed


Do not use wildcards in the import statement. It will give a very bad performance in the Shell interpreter to search the whole package and load it in memory.

Don't:

import java.util.*;

Do:

import java.util.Collection;


2. Do not need to mention the type of element of a collections


BeanShell interpreter cannot recognise the element type syntax of collections class like Collection, Map, List, Set and etc.

Don't:

Map<String, String> map = new HashMap<String, String>();


Do:

Map map = new HashMap();


3. Indents your script nicely and follows the Java Code Conventions


It will make your life and others' easier to maintain and review the script whenever necessary, as Joget DX8 provided flexibility to adapt change quickly.

4. Write your script in functions

 

If your script is pretty long and some parts of the script are reusable, please make use of functions to write your script. It provided better reading and performance.

 

5. Remember to write some comments 


It will help you and others to understand the script's purpose quickly.

 

6. Catch the exceptions and give a meaningful message in log


If you are using a lot of BeanShell Scripting in your app, a meaningful log message can help you to locate your issue quickly.

Don't:

try {
    //do something
} catch (Exception e) {
    LogUtil.error("BeanShell", e, "Error executing script");
}


Do:

try {
    //do something
} catch (Exception e) {
    LogUtil.error("CRM app - Backend userview", e, "Error retrieving user department in Report category permission");
}


7. Consider to make it as a plugin instead of using Bean Shell


If your script needs to be reused multiple times in an app or can be used by other apps in future development, please consider making your BeanShell script a plugin instead of copying and pasting it multiple times across your app. The Bean Shell script is harder to maintain in this case. Imagine that you want to make a change. You will have to update all the places that are using the same script as well. Besides that, a normal plugin implementation will perform better than a script run by a Bean Shell interpreter.


8. Reuse existing plugins in your code to make it cleaner and easier to maintain


If part of your script can be done by existing plugins in Joget DX8, you can reuse the plugin instead of writing it again. 
To reuse a plugin, you can retrieve the plugin using PluginManager, then set its properties and execute it.

Example

  1. Reuse Multi Row Binder plugin to load data
  2. Reuse Email Tool to send email


9. How to use 3rd party libraries in Bean Shell


In your webserver, you can add the jar file to your WEB-INF/lib folder and restart the server. Then, you should be able to "import" and use them in your bean shell Java code.


More samples

Created by Damian Last modified by Aadrian on Dec 13, 2024