Sheerpower Logo
H.1  Routines and Variable Scoping

Sheerpower: Routines and Variable Scoping

Sheerpower provides several methods for organizing code, which is essential as projects and teams grow. Some Sheerpower programs have exceeded 900,000 lines of code. Despite their size, these programs can compile, link, and run within three seconds of saving any changes, significantly speeding up development.

Types of Routines in Sheerpower:

  1. Global Routines: The default type. All variables are global and accessible across the entire program.
  2. Private Routines: All variables inside these routines are private by default, meaning they are only accessible within the routine.
  3. Scoped Routines: Similar to private routines, but all variables are reset upon entering and exiting the routine.
  4. Local Routines: These routines inherit the scope of the private routine that calls them. They are useful for breaking large routines into smaller, more manageable sections.

Variable Scoping in Sheerpower

Variable scoping determines where variables can be accessed. Variables declared outside any routine are global or main variables. Within a routine, global variables can be accessed directly in a global routine but must be prefixed with MAIN$ in private routines.

x = 45
abc = 999

routine do_it
  print x       // prints 45
  print main$x  // also prints 45
end routine

private routine here_we_go
  x = 101
  print x       // prints 101
  print main$x  // prints 45
end routine

In this example, the fully qualified name main$x accesses the global x variable inside the private routine.

Scoping in Practice

In Sheerpower, scoping ensures clear variable access:

private routine my_private
  abc = 123
  local do_it_local
end routine

local routine do_it_local
  xyz$ = 'is xyz'
  assert abc = 123
  assert xyz$ = 'is xyz'
end routine
routine my_regular: private abc, tax
  abc = 456
  tax = 99
  local do_it_2
  print 'In '; _routine
  debug show abc, my_regular$abc, main$abc, tax, my_regular$tax
end routine

local routine do_it_2
  my_tax = 45
end routine

In the example above, my_regular$abc and main$abc demonstrate how variable scope is managed through naming conventions.

How SheerPower’s Scoping Reduces Bugs

  1. Clarity and Explicitness: The fully-qualified naming convention ensures that developers are explicit about which variables they are accessing, avoiding accidental overwriting or misuse.
  2. Encapsulation: Private and local routines ensure that variables remain confined to their intended scope unless explicitly shared.
  3. Controlled Lifespan of Variables: Scoped routines automatically reset variables, preventing stale values from persisting between calls.
  4. Avoiding Recursion: By eliminating traditional recursion, Sheerpower avoids issues like stack overflow and makes the code easier to understand.

Overall Benefits

Overall, SheerPower’s approach to scoping provides clear, strict, and safe mechanisms that significantly reduce common scoping bugs such as variable shadowing, unintended global variables, and stale data, making the development process more reliable.

Additional Features:

  • Include Directives and Modules: Sheerpower supports modular development with include directives, helping manage large codebases.
  • Memoization in Recursive Routines: Instead of traditional recursion, Sheerpower uses memoization techniques, which boost performance and security by avoiding data stack manipulation.

Exiting and Repeating Routines in Sheerpower

In Sheerpower, there are two special control statements for managing the flow of routines:

  1. Exit Routine:
    The exit routine statement allows a routine to be exited early, before the end routine. This is useful for terminating the routine when certain conditions are met, without executing the remaining code in the routine.

Example:

routine early_exit_example if condition_fails set error on exit routine // Exits the routine early end if // Code here will be skipped if condition_fails end routine
  1. Repeat Routine:
    The repeat routine statement restarts the routine from its first line of code. This can be useful in situations where you want to retry the logic within a routine based on certain conditions or inputs.

Example:

routine repeat_example if need_to_retry repeat routine // Restarts the routine from the beginning end if // Routine logic continues here end routine

These statements provide flexibility in controlling the flow of routines in Sheerpower.

Error Signaling in Routines

Sheerpower provides a simple and effective error signaling mechanism to handle exceptions or errors within routines. When a routine is called, the special variable _error is automatically set to zero. If an error occurs within the routine, you can signal it by using the command set error on. This changes the value of _error to true, allowing the calling code to determine if an error occurred after the routine returns.

This automatic error handling simplifies the process of managing errors and ensures consistency across routines.

Example of Error Signaling in a Routine:

routine example_routine if condition_fails set error on // Signal an error exit routine end if // Routine continues normally end routine routine calling_routine example_routine if _error print "Error detected in example_routine!" else print "example_routine executed successfully." end if end routine
Hide Description

    

       


      

Enter or modify the code below, and then click on RUN

Looking for the full power of Sheerpower?
Check out the Sheerpower website. Free to download. Free to use.
Wide screen