Three most common gotchas in Operon
There are three most common "gotchas" that we want to warn you about.
1. Using current-value inside an array or object as a value.
Here's the simple example of what kind of situation is meant:
This is legit Operon code. The problem is that once we start to use this kind of coding pattern with dynamically evaluated expressions, e.g. in the named values or functions, then it becomes quite hard to track what the actual input value for the expression is.
Here's another example
Here we may think that we can track what is sent to eval-function but in reality we probably can't.
It is often better to use a named value in these situations to make the code more readable and resilient to errors.
2. Accidentally filtering the component's result.
-> out (or any other component), and trying to create an array
after that, it is necessary to note that it is easy to forget breaking the flow first.
Let's see an example:
What actually happens here is that the
 acts as a Filter.
It means that the query is actually
"Foo", which will of course
yield "F", which will cause the further processing to act on wrong input-value.
Filtering the value is not what we wanted in this example. Here is the correct way to create
a new array after our component call:
3. Binary operations
When using binary-operators (i.e. operations that have left-hand side and right-hand side such as "+"), it's important to note that the right-hand side of the expression must be properly enclosed, otherwise it's first hard to see what what went wrong.
Let's consider this example where we have two arrays and we want to have their intersection, i.e. include only the items that appear in both arrays. Finally we want to count how may common items there were.
Here's the first try, which will fail:
The result is an empty array: . Why? Because the right-hand side is
["bin", "bai", 1, 2, "baa"] => count().
There are two ways to correct this; 1. by using parentheses, or 2. by using flow-break.
Here's the first correct version:
This is how the second correct version looks like:
Few other gotchas
- Index 0 means none of the items in the array/object.
- Index 1 means the first item in the array/object.
- Index 2 means the second item in the array/object, etc.
- Index -1 means the last item in the array/object.
- Index -2 means the second last item in the array/object, etc.
- Index -n, where n is the size of the array + 1, means the whole array/object.
Using the "End" -syntax
NOTE: we need two ";" because the first terminates the "When" and the second terminates the "Choice".
To avoid this gotcha, we strongly recommend to adapt the coding style where the End -keyword (or ";") is aligned with the keyword that starts the expression. Here's an example of how the code looks like when using the Operonic-coding style:
Using the Not -operator
This is wrong:
- Operon interprets this as
(Not 1) = 1, which of course is not syntactically correct.
This is correct: