|
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.
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.
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 two 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.
These statements provide flexibility in controlling the flow of routines in Sheerpower.
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. |