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 compile, link, and run within two seconds of saving any changes, significantly speeding up development.

Types of Routines in Sheerpower:

Global Routines: The default type of routine. All variables are global and accessible across the entire program.

Private Routines: All variables inside these routines are private by default, meaning they are only accessible within the routine.

Scoped Routines: Similar to private routines, but all variables are initialized upon entering and exiting the routine.

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.


Calling Routines with Named Parameters

In SheerPower, all routine calls use named parameters, making the code more readable and reducing errors. Named parameters ensure that each argument is explicitly identified, eliminating ambiguity often associated with positional arguments.

routine calculate_area with length, width, returning area area = length * width end routine calculate_area with length = 10, width = 5, returning area room_area print "The area is: "; room_area

Output:

The area is: 50

How Routines Replace Functions

SheerPower does not use traditional functions, which typically return a single value and rely on positional parameters. Instead, it uses routines, which are more versatile and maintainable because they:

  • Allow up to 16 values to be returned using the returning clause.
  • Use named parameters, making the code easier to read and reducing errors from mismatched argument positions.

By focusing on routines, SheerPower eliminates the limitations and ambiguities of traditional functions, resulting in clearer, more maintainable code.

routine find_max with num1, num2, returning max_value if num1 > num2 then max_value = num1 else max_value = num2 end if end routine find_max with num1 = 15, num2 = 10, returning largest print "The largest number is: "; largest

Output:

The largest number is: 15

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

Clarity and Explicitness: The fully-qualified naming convention ensures that developers are explicit about which variables they are accessing, avoiding accidental overwriting or misuse.

Encapsulation: Private and local routines ensure that variables remain confined to their intended scope unless explicitly shared.

Controlled Lifespan of Variables: Scoped routines automatically reset variables, preventing stale values from persisting between calls.

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.

Exiting and Repeating Routines and Guard Clauses in Sheerpower

In Sheerpower, there are three 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
  2. Guard condition:
    Note: Using an explicit guard clause is an elegant way to exit routines early if a condition isn't met, making your code cleaner and easier to read. If the condition provided is not met, the routine exits immediately, otherwise the routine continues.

    Syntax:

    guard logical_expression

    Benefits

    1. Clearer Code: Makes early exits obvious.
    2. Simplifies Logic: Reduces nested if structures.
    3. Easier Maintenance: Keeps routines focused.

    Example: Basic Usage

    A routine that checks if a user is authorized:

    routine process_task guard is_authorized(user_id) = "Y" print "Processing task for user ", user_id end routine

    If is_authorized(user_id) = "Y" is false, the routine exits early.

    Example: Multiple Guards

    Handling multiple conditions:

    routine update_user_profile guard user_id > 0 guard is_active(user_id) = "Y" print "Updating profile for user ID ", user_id end routine

    The routine exits early if any condition is false.

The exit routine, repeat routine, and guard statements in SheerPower allow you to efficiently control the flow of routines. These features help simplify your code, making it cleaner, easier to read, and more maintainable.

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.