Coding standards are a collection of rules, guidelines, and best practices. Coding standards are important for safety, security, and reliability. In this course, we learn the most important and UiPath recommended coding standards. Starting from naming conventions to maintaining your code in the code repository, we cover all the best practices.
Alright! so, you or your team members have come up with an incredible bot idea and have decided to use Automation Anywhere to develop it. Now, you want to make sure you build the bot in a way that follows the best practices for coding, security, logging, and all other good stuff. Awesome! you have come to the right place. The following are some of the coding best practices. Read on.
1. Make your bots modular
Having everything in single bot make the code difficult to follow, edit or update. As a best practice, always separate tasks into different TaskBots or MetaBots. Creating separate bots for each distinct function makes the code easier to understand, update, and reuse.
For example, consider a use case where there are three steps:
- Check email
Each of these steps should be divided into unique TaskBots (.atmx files). Create a Master Bot to use for each of the three tasks in the correct order.
2. Use Metabots
MetaBots encapsulate assets and logic used in common processes and tasks, making those processes and tasks reusable by other bots. Consider creating and using MetaBots for common processes and tasks. Over time a library of functions and tasks can be contained in MetaBots which are reusable by TaskBots. This avoids the need to create redundant functionality in multiple bots.
MetaBots are used to encapsulate logic inside a DLL (Dynamic Link Library), allowing bot developers to hide complex tasks and processes from users. Following are the high-level steps for adding a DLL to a MetaBot:
- Create a DLL that does the respective function.
- Add the DLL as an asset in the MetaBot Designer.
- Add Logic to do the operation.
- Use the MetaBot in other bots to do the task.
3. Use variables generously
Hard coding values makes your code less configurable for customers’ environments. Use variables throughout the your code and avoid hard coding.
Some important facts about using variables:
- Developer-created variables can be used with most Automation Anywhere commands.
- All command fields with the light bulb icon support variables.
- Press the F2 function key to list all user and system variables that are available for a selection.
- When a TaskBot runs, Automation Anywhere replaces the variable name with the actual value of the variable.
4. Use system variables
During the development of new bots, we often point to local copies of files and attachments. This works great if the bot is only run on the developers’ local computers.
Bot Runners are unattended computers (physical or virtual computers) whose job is to run the tasks presented to them. Because these Bot Runners have their own account login credentials, localized paths do not work.
The system variable $AAApplicationPath$ resolves this problem.
Local path during development:
C:\Users\UserName\Documents\Automation Anywhere Files\Automation Anywhere\My Docs\accounts.xlsx
Relative path that works in Bot Runner:
$AAApplicationPath$\Automation Anywhere\My Docs\accounts.xlsx
Not only does this make the path shorter, but it also makes the bot portable. When preparing a TaskBot to work with Bot Runner or a co-worker’s computer, use the $AAApplicationPath$ anywhere that points to a local file.
Create variables, for example, “vPath,” then use the Variable Operation with $AAApplicationPath$ to make the paths short and manageable.
5. Follow naming conventions
Automation Anywhere has a recommended variable naming convention.
- Variables start with “v”, such as vPath.
- Lists start with “lst”, such as lstMyList.
- Arrays start with “arr”, such as arrPirateBooty.
- Randoms start with “rdm”, such as rdmChoice.
Some developers prefer to add variable “type” prefixes to the front of their variables, for example:
- “int” for variables expected to hold whole numbers (integers)
- “str” for variables expected to hold a string of characters
Regardless of the naming scheme, variables in Automation Anywhere do not have a strong type.
This means, a variable that contains a number, then does a mathematical calculation, can later contain a string value. This is called loose typing. Although it adds flexibility when working with variables, it means creators must remember which variables are used for which purposes, and not confuse them.
6. Use Credential Vault
Never hard code User IDs and passwords as variables in tasks as that would introduce a security risk. Store all user IDs, passwords, secret keys, connection strings and other sensitive information in the Credential Vault of the Enterprise Control Room. Once stored in the credential vault, they can be referenced in the bot code.
7. Handle errors gracefully
Use the Begin/End Error Handling statements to gracefully handle error dialogs. Automation anywhere provides multiple ways to handle errors gracefully:
- Take Snapshot: It saves a snapshot of the screen at the moment of the error. A screenshot is sometimes the best way to determine the cause of an error when running on an unattended Bot Runner.
- Run Task: Starts a new Task when an error occurs. This Task can do functions, for example, cleaning up temporary files.
- Log Data into File: Writes a new log entry into the specified log file. This log entry can also contain a timestamp, which is useful for determining exactly when the error occurred. Using the system variables for $Error Line Number$ and $Error Description$ provides useful data for the log.
- Send Email: Sends an emailed error report which can contain a screenshot of the error (see Take Snapshot) and variable values to help troubleshoot the issue.
- Variable Assignment: Sets a value to a specific variable. This is useful for tracking whether an error occurred in the task, then doing other events, for example, logging task successes and failures.
- Set Task Status: Reports a Fail or Pass condition to the Enterprise Control Room on an error. The Pass condition is useful when an error condition is expected but you want to ignore it and continue.
Handling errors and reporting them may depend on the project requirements. But, at a minimum, create an “ErrorFolder” in your project and then create two sub-folders under it. One for logs and one for snapshots.
- Capture error windows.Save error window snapshots to the Snapshots folder.
- Log error messages, including a time stamp, to the Logs folder.
8. Use code comments
Remember to liberally comment bot code. Use the following best practices for code commenting:
- Make a block of comments at the beginning of a TaskBot, clearly stating what the bot does.
- Comment complex blocks of code to let developers and users know what was done.
1 Comment: **************************** 2 Comment: It's really a good idea to liberally 3 Comment: comment code. 4 Comment: 5 Comment: Comments help developers remember what 6 Comment: and why something was coded. 7 Comment: 8 Comment: Comments help users understand Bots. 9 Comment: 10 Comment: There is no such thing as too 11 Comment: many code comments. 12 Comment: ****************************
You might be interested in the following courses:
This is an advanced guide to best practices that need to be followed in developing bots using Automation Anywhere. This course provides an introduction to common bot design guidelines and standards. Avoiding common mistakes and including these processes and considerations in your bot design standards, creates bots that are clean, easier to read, test, maintain, and are stable. Most of […]
Robotic Process Automation(RPA) is a kind of automation where a bot performs human’s task in completing rules based jobs. Robotic Process Automation refers to a style of automation where a machine, or computer, mimics a human’s action in completing rules-based tasks. In traditional workflow automation tools, a software developer produces a list of actions to automate a task and interface to […]