# ScriptCode best practices

### Context <a href="#dobrepraktykiscriptcode-kontekst" id="dobrepraktykiscriptcode-kontekst"></a>

The rules for writing ScriptCode on the Eximee platform will help ensure consistency, readability, and code security, and will also make onboarding new employees easier.

Create ScriptCode according to the rules described below.

<table><thead><tr><th width="206">Area</th><th>Rule</th></tr></thead><tbody><tr><td>Consistent naming convention</td><td><ul><li>Write code in English - use English names for variables, functions, etc.</li></ul><p><strong>Exceptions:</strong> top-down business naming decisions or a high level of script complexity (in this case, the decision belongs to the low-code developer). If you decide on a Polish naming convention - <strong>be consistent within a single script</strong>.</p><ul><li>Literal constants <strong>hard-coded</strong> in code, name them in UPPER_SNAKE_CASE format, e.g. const MOBILE_CHANNEL = "mobile"</li><li>Name other variables and functions in camelCase format, e.g. const correlationId = context.getFirstParameter('correlationId'), let motherBranchNumber, function generateBranch()</li></ul></td></tr><tr><td>Order in the code structure</td><td><p>Organize code according to the following order:</p><ol><li><strong>Literal constants</strong>- declare at the beginning of the script</li><li><strong>Variables</strong> - then declare variables that will be used in the main logic</li><li><strong>Main logic</strong> - the main sequence of operations performed by the code (e.g. data processing, function calls)</li><li><strong>Helper functions</strong> - place at the end</li></ol></td></tr><tr><td>Logging</td><td><ul><li>Do not duplicate platform logs - if any parameters are automatically generated by the platform, do not log them again in the script</li><li>Use the sensitive logger to log sensitive data, pattern: <a href="http://logger.info/">Logger.info</a>("Customer's PESEL filled in on the form: {}", pesel)</li></ul><p><strong>Learn more:</strong><br><a href="/pages/fe49e2a8a7d4c8ca29f58f6943734b089f32f636">Logging in ScriptCode</a></p></td></tr><tr><td>Variable declaration</td><td><ul><li>Do not use vars</li><li>Primitive types: use let only when you change the variable's value in the script. In other cases, use const</li><li>Complex types: do not use let if you only modify the contents of an object - including arrays (Array), sets (Set), maps (Map).</li></ul><p>There is no need to use let when declaring objects - including arrays (Array), sets (Set), maps (Map) - as long as the reference (i.e. the reference to a specific object or array) will not be changed.<br><strong>In most cases, const should be used to declare arrays and other objects, even if their contents will be modified.</strong></p><p>Example using const:</p><p>const array = [1, 2, 3];<br>array.push(4); // Works correctly, we are modifying the contents of the array</p><p>const obj = { name: "Alice" };<br>obj.age = 30; // Works correctly, we are modifying the object properties<br></p><p><strong>Note:</strong> As long as the Rhino engine does not properly support block scope for const, variables inside a "for" loop should be declared using let. More about the bug can be found in the material linked below.<br></p><p><strong>Learn more:</strong><br><a href="https://kursjs.pl/kurs/super-podstawy/typy-danych">Data types in JavaScript</a>,</p><p><a href="https://www.youtube.com/watch?v=-mbiJ2it-5M">Reference, mutability, primitive types - JavaScript recap!</a></p><p><a href="/pages/c995d35297f3609a88c181d2f7e2577dde45d4c0">Scripts(ScriptService)</a></p><p><a href="http://mozilla.github.io/rhino/compat/engines.html">A table listing which features are currently not properly supported by the Rhino engine</a><br><a href="https://github.com/mozilla/rhino/issues/326">Rhino: error when declaring const inside a for loop</a></p></td></tr><tr><td>Error handling</td><td><ul><li>Use blocks <code>try-catch</code> in places where exceptions may occur. If an error occurs, log it using the logger to make it easy to track the problem.</li></ul></td></tr><tr><td>Code readability</td><td><ul><li>Right-click → choose Format Document. This will make the code more clear and readable</li><li>Remove code that is commented out and no longer needed</li><li>Write comments concisely and clearly, explaining more difficult parts of the code, intentions, and unusual solutions. Avoid obvious comments that merely repeat what is already readable in the code.</li></ul></td></tr><tr><td>Code repetition</td><td><ul><li>If you repeat a fragment of code several times - create a helper function</li></ul></td></tr><tr><td>Script description</td><td><ul><li>When creating a new script/validator - add a description</li></ul></td></tr><tr><td>Tests</td><td><ul><li>Create unit tests whenever possible, including edge cases.</li></ul></td></tr><tr><td>Security</td><td><ul><li>Treat any parameters entering ScriptCode as untrusted when sending them to services</li></ul><p><strong>Sample code:</strong></p><p>Bad:</p><p>Good:</p><ul><li>Do not use authorization data directly in ScriptCode</li><li>Retrieve remote resources (images, documents, card images, etc.) from a trusted source (e.g. the Eximee platform, a bank resource)</li><li>If you need to use any link in the application, make sure it is trusted and that its use results directly from the requirements</li><li>If a given feature does not exist on the platform, submit a request - do not use external tools</li></ul><p><strong>Learn more:</strong> <a href="/pages/dc8393d6b23f9a12f11bbf058f8b57eb7e84aa7c"><strong>OWASP_Application_Security_Verification_Standard_4.0 - scriptCode</strong></a></p></td></tr><tr><td>Mathematical operations with BigDecimal</td><td><ul><li>Perform mathematical operations using BigDecimal</li></ul><p><strong>Learn more:</strong> <a href="/pages/db1819cb8664baab75f4837c8cdcf1ecb6216f2c">Mathematical operations in ScriptCode</a></p></td></tr><tr><td>Handling falsy values</td><td><ul><li>Before using properties or methods, check whether the variable's value is not <code>null</code> or <code>undefined</code>, to prevent errors related to missing values. In situations where you also want to catch <code>0</code>, an empty string or <code>NaN</code>, you can use the general condition <code>if(value)</code>.</li></ul></td></tr><tr><td>Review</td><td><ul><li>If you are not the only low-code developer on the team - ask someone from the team to review the code.</li></ul></td></tr></tbody></table>

### Materials <a href="#dobrepraktykiscriptcode-materialy" id="dobrepraktykiscriptcode-materialy"></a>

* [OWASP\_Application\_Security\_Verification\_Standard\_4.0 - scriptCode](/documentation/documentation-en/budowanie-aplikacji/logika-biznesowa/scriptcode/dobre-praktyki-scriptcode/owasp_application_security_verification_standard_4.0-scriptcode.md)
* [Mathematical operations in ScriptCode](/documentation/documentation-en/budowanie-aplikacji/logika-biznesowa/scriptcode/skrypty-scriptservice/api-skryptow/operacje-matematyczne-w-scriptcode.md)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.eximee.com/documentation/documentation-en/budowanie-aplikacji/logika-biznesowa/scriptcode/dobre-praktyki-scriptcode.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
