Logik.io: Scripting Best Practices<!-- /*NS Branding Styles*/ --> .ns-kb-css-body-editor-container { p { font-size: 12pt; font-family: Lato; color: #000000; } span { font-size: 12pt; font-family: Lato; color: #000000; } h2 { font-size: 24pt; font-family: Lato; color: black; } h3 { font-size: 18pt; font-family: Lato; color: black; } h4 { font-size: 14pt; font-family: Lato; color: black; } a { font-size: 12pt; font-family: Lato; color: #00718F; } a:hover { font-size: 12pt; color: #024F69; } a:target { font-size: 12pt; color: #032D42; } a:visited { font-size: 12pt; color: #7057C7; } ul { font-size: 12pt; font-family: Lato; } li { font-size: 12pt; font-family: Lato; } img { display: block; max-width: ; width: auto; height: auto; } } This guide will walk you through the best practices for writing efficient and reliable scripts using Logik's JavaScript-like language. Following these guidelines will help you create maintainable, readable, and well-performing code. Quick Summary Declare variables at the top of the script Avoid "new" Avoid "==" Use consistent variable naming techniques: camelCase or snake_case Indent Brackets for readability Use let or const instead of var if possible Minimize Table Lookups Use descriptive variable names Comment on what the code is doing 1. Declarations on Top Why: Placing variable and function declarations at the top of your script improves readability and prevents unexpected variable hoisting issues. // Declare and initiate at the beginning let firstName = ""; let lastName = ""; let price = 0; let discount = 0; let fullPrice = 0, const myArray = []; const myMap= {}; 2. Avoid "new" Why: Using the “new” keyword can use more resources, lead to memory leaks, and cause unintended behaviors. Instead, use the literal notation for object creation if possible. Use "" instead of new String() Use 0 instead of new Number() Use false instead of new Boolean() Use {} instead of new Map() Use [] instead of new Array() 3. Avoid "==" (Loose Equality) Why: The loose equality operator can lead to unexpected type coercion. Use the strict equality operator (`===`) for accurate comparisons. // Good if (count === 5) { // ... } // Bad if (count == "5") { // ... } 4. Use Consistent Variable Naming: camelCase or snake_case Why: Consistent naming conventions enhance code readability and maintainability. Choose either “camelCase” or “snake_case” and stick to it. Logik field variable names use camelCase, so most organizations stay with this convention for readability. // camelCase let firstName = "JohnDoe"; // snake_case let last_name = "Smith"; 5. Indent Brackets Why: Properly indenting your code improves its visual structure and makes it easier to understand, especially when dealing with nested blocks. // Good if (condition) { // ... if (nestedCondition) { // ... } } // Bad if (condition) { // ... if (nestedCondition) { // ... } } 6. Use Const before Let, and Let over Var Why: Choose variable declaration based on scope and mutability requirements. Use “const” for variables that won't be reassigned, and “let” for variables that will change. “var” is also acceptable, but is a holdover from previous versions of JavaScript. // Using const for unchanging values const TAX_RATE = 0.15; // Using let for mutable values let itemCount = 5; 7. Minimize Table Queries Why: Excessive table queries can impact performance. Minimize queries by fetching necessary data once and storing it in variables. To learn about best practices while using the Lookup function, see Table Queries 8. Use Descriptive Variable Names Why: Choose names that are meaningful and describe the purpose of the variable or function. This makes your code self- documenting and easier for others to understand. //Good let quoteId = cfgRequest.partner.quote.id.value; let lineID = cfgRequest.partner.quote.lineId.value; let currencyISO = cfgRequest.partner.quote.currencyIsoCode.value; let priceBookID = cfgRequest.partner.quote.pricebookId.value; if (quoteId != null) { cfgRequest.quoteIDTest.set("value", quoteId); } if (lineID != null) { cfgRequest.lineIDTest.set("value", lineID); } if (currencyISO != null) { cfgRequest.currencyISOCodeTest.set("value", currencyISO); } if (priceBookID != null) { cfgRequest.pricebookIDTest.set("value", priceBookID); } //Bad let x1= cfgRequest.partner.quote.id.value; let x2= cfgRequest.partner.quote.lineId.value; let x3= cfgRequest.partner.quote.currencyIsoCode.value; let x4= cfgRequest.partner.quote.pricebookId.value; if (x1 != null) { cfgRequest.quoteIDTest.set("value", x1); } if (x2 != null) { cfgRequest.lineIDTest.set("value", x2); } if (x3 != null) { cfgRequest.currencyISOCodeTest.set("value", x3); } if (x4 != null) { cfgRequest.pricebookIDTest.set("value", x4); } 9: Comment Thoughtfully Why: Use comments to explain complex logic, assumptions, or any non-obvious behavior in your code. Avoid redundant comments that merely repeat the code. By following these Logik.io Scripting Best Practices, you'll write code that's easier to understand, less error-prone, and more efficient. Happy scripting!