# Overriding Variables using the Data Viewer

**URL:** https://the.fmsoup.org/t/overriding-variables-using-the-data-viewer/2357
**Category:** Heads-Up!
**Tags:** scripting, debugger, data-viewer
**Created:** [July 29, 2021, 12:28am UTC](https://the.fmsoup.org/t/overriding-variables-using-the-data-viewer/2357 "2021-07-29T00:28:47Z")
**Posts on this page:** 18
**Page:** 1

<div class="post-metadata">

### Author: ![Malcolm](https://yyz2.discourse-cdn.com/flex030/user_avatar/the.fmsoup.org/malcolm/32/196_2.png) [@Malcolm](https://the.fmsoup.org/u/Malcolm)
#### Post date: [July 29, 2021, 12:28am UTC](https://the.fmsoup.org/t/overriding-variables-using-the-data-viewer/2357/1 "2021-07-29T00:28:47Z")

</div>

# Variables defined in the data viewer are available to all objects in scope

If it is used carelessly this can cause problems which range from mild to severe.

Yesterday I was using the debugger to test a new script. It was parsing JSON pulled in from an API. When I ran the script without the data viewer open, it populated the records. When I ran the script with the data viewer it threw up JSON evaluation errors.

## What was happening?

The Data Viewer can over-ride variables defined in scripts and layouts. This allows us to inject data into running scripts, layout level evaluations, and menus. It's a powerful tool.

In this case, I did not want to inject the data. It was happening because I had left calculations in the data viewer. The data viewer contained LET expressions that defined local variables with names that were the same as the local variables defined in the script. When the data viewer was open and active it triggered the calculation of the variables. The new value overrides the values set in the script and because the new data was not valid JSON, the JSON parsing steps would generate errors.

## What's the Solution

1. Don't run scripts with the Data Viewer open.

2. Remove your tests from the Data Viewer.  
Don't think of it as a library of code snippets.  
Treat your data viewer like a scratch-pad.  
Use it for immediate tests, and even then, use it with caution

3. Comment your tests  
If you want to preserve your test code, wrap it in C style comments.  
` /* Let ( $msg ="like this" ; $msg ) */`

## If you are Deliberately invoking this Functionality

There are gotchas for you too

1. Watched variables are not always evaluated.
2. When your watched variable list is long enough that variables are not visible in the window they may not be evaluated.
3. This could cause your test to fail because the over-ride values you specify have not been brought into scope.
4. Scrolling through your watched variables to bring them into view will normally trigger the evaluation.

Here's a file that you can use for testing.

[OverrideDataVariable.fmp12](https://the.fmsoup.org/uploads/short-url/2lV4ztZGHAC2YeVtd4YS0QuV7K4.fmp12) (260 KB)

---

<div class="post-metadata">

### Author: ![jwilling](https://yyz2.discourse-cdn.com/flex030/user_avatar/the.fmsoup.org/jwilling/32/256_2.png) [@jwilling](https://the.fmsoup.org/u/jwilling)
#### Post date: [July 29, 2021, 2:01am UTC](https://the.fmsoup.org/t/overriding-variables-using-the-data-viewer/2357/2 "2021-07-29T02:01:24Z")

</div>

Quick note about C style comments. Also be sure to add another valid expression before/after your commented-out code, otherwise when you reopen that DV expression, it will have wrapped your expression in quotes without your permission!

![Screen Shot 2021-07-28 at 7.00.51 PM](https://canada1.discourse-cdn.com/flex030/uploads/fmsoup/original/2X/5/57b93d6b17d206fb79ecd3354eca9d1dee6150f4.png)

---

<div class="post-metadata">

### Author: ![Torsten](https://yyz2.discourse-cdn.com/flex030/user_avatar/the.fmsoup.org/torsten/32/1574_2.png) [@Torsten](https://the.fmsoup.org/u/Torsten)
#### Post date: [July 29, 2021, 6:10am UTC](https://the.fmsoup.org/t/overriding-variables-using-the-data-viewer/2357/3 "2021-07-29T06:10:35Z")

</div>

Does switching off 'Automatically evaluate' prevent the injection of values in variables of the current scope?

 ![Screenshot 2021-07-29 at 08.07.23](https://canada1.discourse-cdn.com/flex030/uploads/fmsoup/original/2X/a/a405e7ba36bb54714e18c654ae60510d32f5bc26.jpeg)

---

<div class="post-metadata">

### Author: ![steve\_ssh](https://yyz2.discourse-cdn.com/flex030/user_avatar/the.fmsoup.org/steve_ssh/32/160_2.png) [@steve\_ssh](https://the.fmsoup.org/u/steve_ssh)
#### Post date: [July 29, 2021, 1:55pm UTC](https://the.fmsoup.org/t/overriding-variables-using-the-data-viewer/2357/4 "2021-07-29T13:55:50Z")

</div>

> [@jwilling](#):
>
> Quick note about C style comments. Also be sure to add another valid expression before/after your commented-out code, otherwise when you reopen that DV expression, it will have wrapped your expression in quotes without your permission!

Agreed on this -- but if you forget, you can get your original calc back fairly painlessly by taking the quoted expression, and evaluating it in the Data Viewer. The act of evaluating it should return your original calculation expression.

---

<div class="post-metadata">

### Author: ![Malcolm](https://yyz2.discourse-cdn.com/flex030/user_avatar/the.fmsoup.org/malcolm/32/196_2.png) [@Malcolm](https://the.fmsoup.org/u/Malcolm)
#### Post date: [July 29, 2021, 6:32pm UTC](https://the.fmsoup.org/t/overriding-variables-using-the-data-viewer/2357/5 "2021-07-29T18:32:15Z")

</div>

Good question!

No. That switch does not prevent the data injection.

If you have the data viewer open to the "Current" tab/pane you do not get data injection. If you have the "Watch" pane open you will get data injection.

---

<div class="post-metadata">

### Author: ![Torsten](https://yyz2.discourse-cdn.com/flex030/user_avatar/the.fmsoup.org/torsten/32/1574_2.png) [@Torsten](https://the.fmsoup.org/u/Torsten)
#### Post date: [July 29, 2021, 6:37pm UTC](https://the.fmsoup.org/t/overriding-variables-using-the-data-viewer/2357/6 "2021-07-29T18:37:36Z")

</div>

Oops, this is defying my sense of logic. When I tell the system to NOT evaluate, it still evaluates?

---

<div class="post-metadata">

### Author: ![jwilling](https://yyz2.discourse-cdn.com/flex030/user_avatar/the.fmsoup.org/jwilling/32/256_2.png) [@jwilling](https://the.fmsoup.org/u/jwilling)
#### Post date: [July 29, 2021, 7:19pm UTC](https://the.fmsoup.org/t/overriding-variables-using-the-data-viewer/2357/7 "2021-07-29T19:19:24Z")

</div>

Yeah, "Automatically Evaluate" only applies while you have the data viewer _calculation_ window open. When it's ticked, the calc result updates as you type. When it's unticked, the calc result only updates when you click "Evaluate".

But, when you close the calc window for a Watch expression, it starts automatically evaluating in the Watch list. I wish you could turn off specific watch expressions!

---

<div class="post-metadata">

### Author: ![LVZ](https://avatars.discourse-cdn.com/v4/letter/l/5f9b8f/32.png) [@LVZ](https://the.fmsoup.org/u/LVZ)
#### Post date: [July 30, 2021, 8:54am UTC](https://the.fmsoup.org/t/overriding-variables-using-the-data-viewer/2357/8 "2021-07-30T08:54:16Z")

</div>

This behavior makes sense to me. I use the data viewer to SEE what are the values while a script running in debugging mode.

---

<div class="post-metadata">

### Author: ![FileKraft](https://yyz2.discourse-cdn.com/flex030/user_avatar/the.fmsoup.org/filekraft/32/1183_2.png) [@FileKraft](https://the.fmsoup.org/u/FileKraft)
#### Post date: [August 10, 2021, 6:47pm UTC](https://the.fmsoup.org/t/overriding-variables-using-the-data-viewer/2357/9 "2021-08-10T18:47:21Z")

</div>

> [@Malcolm](#):
>
> ## What's the Solution
> 
> 1. Don't run scripts with the Data Viewer open.

That doesn’t make sense to me. The debugger needs the data viewer to observe the correct value results otherwise why debugging at all - just for watching instruction flow?

Instead I would claim to strictly code side-effect free (functional) in anything within the calculation engine therefore the data viewer could be completely tamed and the data flow in calculations would be easier to be proofed for correctness …

---

<div class="post-metadata">

### Author: ![Malcolm](https://yyz2.discourse-cdn.com/flex030/user_avatar/the.fmsoup.org/malcolm/32/196_2.png) [@Malcolm](https://the.fmsoup.org/u/Malcolm)
#### Post date: [August 10, 2021, 11:03pm UTC](https://the.fmsoup.org/t/overriding-variables-using-the-data-viewer/2357/10 "2021-08-10T23:03:17Z")

</div>

> [@FileKraft](#):
>
> That doesn’t make sense to me. The debugger needs the data viewer to observe the correct value results otherwise why debugging at all - just for watching instruction flow?

Good Point. We have to be specific about the two tabs displayed by the Data Viewer: Current and Watch. When the Current tab is open, the script variables are not over-ridden. When the Watch tab is open, the variables will be over-ridden.

1. Don't have the data viewer open when it is not needed
2. Watching current values with the Current tab is safe
3. Watching values with the Watch tab will override variables.

---

<div class="post-metadata">

### Author: ![FileKraft](https://yyz2.discourse-cdn.com/flex030/user_avatar/the.fmsoup.org/filekraft/32/1183_2.png) [@FileKraft](https://the.fmsoup.org/u/FileKraft)
#### Post date: [August 11, 2021, 5:37am UTC](https://the.fmsoup.org/t/overriding-variables-using-the-data-viewer/2357/11 "2021-08-11T05:37:10Z")

</div>

> [@Malcolm](#):
>
> Watching values with the Watch tab will override variables.

Only if side effects are implied in code (which can be easily avoided if properly expressed (following functional programming) ..

---

<div class="post-metadata">

### Author: ![Malcolm](https://yyz2.discourse-cdn.com/flex030/user_avatar/the.fmsoup.org/malcolm/32/196_2.png) [@Malcolm](https://the.fmsoup.org/u/Malcolm)
#### Post date: [August 11, 2021, 6:33am UTC](https://the.fmsoup.org/t/overriding-variables-using-the-data-viewer/2357/12 "2021-08-11T06:33:40Z")

</div>

> [@FileKraft](#):
>
> Only if side effects are implied in code (which can be easily avoided if properly expressed (following functional programming) ..

I'm discussing side-effects. So, yes, this is possible. And functional programmers get that wrong from time to time. This information is for everyone who is not a code-perfect functional programmer.

---

<div class="post-metadata">

### Author: ![Torsten](https://yyz2.discourse-cdn.com/flex030/user_avatar/the.fmsoup.org/torsten/32/1574_2.png) [@Torsten](https://the.fmsoup.org/u/Torsten)
#### Post date: [August 11, 2021, 6:48am UTC](https://the.fmsoup.org/t/overriding-variables-using-the-data-viewer/2357/13 "2021-08-11T06:48:49Z")

</div>

Please elaborate on the dos and don'ts of functional programming in FM. This would be helpful for all who want to improve their skills (maybe in a new OP like 'functional programming, best practise').  
Thanks!

---

<div class="post-metadata">

### Author: ![FileKraft](https://yyz2.discourse-cdn.com/flex030/user_avatar/the.fmsoup.org/filekraft/32/1183_2.png) [@FileKraft](https://the.fmsoup.org/u/FileKraft)
#### Post date: [August 11, 2021, 10:45am UTC](https://the.fmsoup.org/t/overriding-variables-using-the-data-viewer/2357/14 "2021-08-11T10:45:35Z")

</div>

> [@Torsten](#):
>
> Please elaborate on the dos and don'ts of functional programming in FM.

This is very simple. Just avoid any $var or $$var assignments via let or implicitly within Evaluate. Otherwise FM is very safe. (That’s why ExecuteSQL only returns results without side effects .. )

---

<div class="post-metadata">

### Author: ![Cecile](https://yyz2.discourse-cdn.com/flex030/user_avatar/the.fmsoup.org/cecile/32/98_2.png) [@Cecile](https://the.fmsoup.org/u/Cecile)
#### Post date: [August 12, 2021, 2:34am UTC](https://the.fmsoup.org/t/overriding-variables-using-the-data-viewer/2357/15 "2021-08-12T02:34:13Z")

</div>

What is functional programming and what are side-effects. What needs to be properly expressed and how do you express properly.

(Can you tell I didn’t understand a thing in the last several posts—not that i understood very much before, aside maybe that the watch tab is dangerous although I don’t really get how scripts running would get values inputs from the Data Viewer (where does it get those anyway?!))

---

<div class="post-metadata">

### Author: ![planteg](https://yyz2.discourse-cdn.com/flex030/user_avatar/the.fmsoup.org/planteg/32/627_2.png) [@planteg](https://the.fmsoup.org/u/planteg)
#### Post date: [August 12, 2021, 3:37am UTC](https://the.fmsoup.org/t/overriding-variables-using-the-data-viewer/2357/16 "2021-08-12T03:37:25Z")

</div>

For an explanation of Functional Programming see [Functional programming - Wikipedia](https://en.wikipedia.org/wiki/Functional_programming)

I stopped reading when I saw that Lisp was the first Functional Programming language 🤢. I used it a bit a long time ago, what a strange language.

> [@Cecile](#):
>
> although I don’t really get how scripts running would get values inputs from the Data Viewer

In the Watch window one can change the content of a variable.

---

<div class="post-metadata">

### Author: ![FileKraft](https://yyz2.discourse-cdn.com/flex030/user_avatar/the.fmsoup.org/filekraft/32/1183_2.png) [@FileKraft](https://the.fmsoup.org/u/FileKraft)
#### Post date: [August 15, 2021, 1:01pm UTC](https://the.fmsoup.org/t/overriding-variables-using-the-data-viewer/2357/17 "2021-08-15T13:01:15Z")

</div>

> [@planteg](#):
>
> I stopped reading when I saw that Lisp was the first Functional Programming language

Lisp is one of the most powerful programming languages allowing to alter its own code during execution. It has influenced Swift, Python and others majorly. FM Let and Evaluate functions are originated there most likely - wherelse?

---

<div class="post-metadata">

### Author: ![FileKraft](https://yyz2.discourse-cdn.com/flex030/user_avatar/the.fmsoup.org/filekraft/32/1183_2.png) [@FileKraft](https://the.fmsoup.org/u/FileKraft)
#### Post date: [August 15, 2021, 1:09pm UTC](https://the.fmsoup.org/t/overriding-variables-using-the-data-viewer/2357/18 "2021-08-15T13:09:39Z")

</div>

> [@Cecile](#):
>
> What is functional programming and what are side-effects. What needs to be properly expressed and how do you express properly.

everything you can do in the Data Viewer (or in the 'calc engine') is functional programming within FM.

A side-effects is the result of a script step like Set Field or Replace where data will be altered or a state change is implied outside the current scope like Print, Goto Layout, GTRR ..  
In contrast a function has an input (or non = void) and returns a result (output). A script step just changes the state or data.
