exprv4:how-to:show_output_and_debug_scripts

How To Show Output And Debug Scripts

When writing a script, output helps you see what the script is doing.

Use output to:

  • show progress
  • check variable values
  • see which branch of an `if` ran
  • check loop counters
  • inspect function input and result values
  • stop with a clear error message

The most useful function is `print(…)`.


Use `print(…)` to write a line of output.

print('Probing started');
print('Tool change finished');

`print(…)` writes a newline after the values.


Pass more than one value to `print(…)` when you want a label and a value.

tool = 3;
diameter = 6;
 
print('tool ', tool);
print('diameter ', diameter);

Labels make output easier to read than printing only the value.

Good:

print('safe_z ', safe_z);

Harder to understand later:

print(safe_z);

When a calculation is wrong, print the intermediate values.

diameter = 6;
radius = diameter / 2;
area = pi() * radius ** 2;
 
print('diameter ', diameter);
print('radius ', radius);
print('area ', area);

This helps you find which value became wrong first.


Check Which if Branch Runs

Add prints inside branches while testing logic.

probe_error = 0.07;
 
if(probe_error <= 0.05)
{
    print('branch: ok');
}
else
{
    print('branch: warning');
};

When the logic is correct, remove the extra debug prints or keep only useful status messages.


Check Loop Progress

Print counters and important values inside loops.

total = 0;
 
for(i = 1; i <= 3; i += 1)
{
    total += i;
    print('i ', i, ', total ', total);
};
 
print('final total ', total);

Loop output is useful, but too much output can become hard to read. For long loops, print only important events or every few iterations.


Check Function Input And Result

Print values at the start and end of a function when debugging.

function FeedForMaterial(material)
{
    print('FeedForMaterial material=', material);
 
    if(material == 'aluminum')
    {
        print('FeedForMaterial result=900');
        return 900;
    };
 
    if(material == 'steel')
    {
        print('FeedForMaterial result=300');
        return 300;
    };
 
    print('FeedForMaterial result=500');
    return 500;
}
 
feed = FeedForMaterial('steel');

This is useful while writing the function. After the function works, remove prints that are only for debugging.


Build Text Without Printing

Use `sprint(…)` when you want to build a text value without writing output.

tool = 3;
diameter = 6;
 
message = sprint('Tool ', tool, ', diameter ', diameter);
message;

This is useful when the text should be returned, stored, shown in a dialog, or passed to another function.


Format Text With Placeholders

Use `sprintf(…)` to build formatted text with `{}` placeholders.

x = 10;
y = 20;
 
message = sprintf('X={}, Y={}', x, y);
message;

The final result is `'X=10, Y=20'`.

Use `printf(…)` when you want the formatted text printed directly.

printf('X={}, Y={}', x, y);

For simple output, `print(…)` is usually enough.


Stop With A Clear Error

Use `error(…)` when the script should stop because something is wrong.

tool = 0;
 
if(tool <= 0)
{
    error('Invalid tool number: ', tool);
};
 
print('this line is not reached when tool is invalid');

A clear error message is better than allowing the script to continue with invalid data.


Clear Output

`clear()` clears console output when the host output window supports it.

clear();
print('Starting test');

Use it at the start of small tests when old output would be confusing.


Use Output From MDI

In MDI, start with `=` to evaluate Expr.

=diameter=6;
radius=diameter/2;
print('diameter ', diameter);
print('radius ', radius);
radius;

This prints the intermediate values and returns the final value.


Debug In Small Steps

When a script does not work, reduce the problem.

Instead of testing the whole script at once, test small pieces:

=diameter=6;
diameter/2;

Then add the next step:

=diameter=6;
radius=diameter/2;
area=pi()*radius**2;
area;

Then add conditions, loops, functions, or object code after the basic values are correct.


Common Mistakes

Printing Without Labels

print(a);
print(b);
print(c);

This becomes hard to read. Prefer labels:

print('a ', a);
print('b ', b);
print('c ', c);

Leaving Too Much Debug Output

Debug prints are useful while writing a script. Too many debug prints can make normal script output noisy.

Keep prints that are useful to the operator. Remove prints that were only used to find a bug.

Continuing After Bad Data

If a required value is missing or invalid, stop early with `error(…)`.

if(tool_diameter.is_none())
{
    error('Tool diameter is missing');
};

Confusing print And sprint

`print(…)` writes output.

`sprint(…)` builds and returns a string.

print('hello');          // writes output
text = sprint('hello');  // stores text

Try This

In MDI, try:

=clear();
material='steel';
feed = if(material == 'steel')
{
    300;
}
else
{
    600;
};
print('material ', material);
print('feed ', feed);
feed;

The final result is `300`.


See Also

exprv4/how-to/show_output_and_debug_scripts.txt · Last modified: by 127.0.0.1

Page Tools