![]() |
|
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.
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.
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.
Output:
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:
returning
clause.
By focusing on routines, SheerPower eliminates the limitations and ambiguities of traditional functions, resulting in clearer, more maintainable code.
Output:
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.
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.
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, 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.
In Sheerpower, there are three special control statements for managing the flow of routines:
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.
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.condition
:if
structures.A routine that checks if a user is authorized:
If is_authorized(user_id) = "Y"
is false
, the routine exits early.
Handling multiple conditions:
The routine exits early if any condition is false
.
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.
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. |