|
Conditional Compilation for Adaptive Code |
Sheerpower's conditional compilation feature uses directives like %defined, %++, %--, and %>> to let developers include or exclude parts of the code based on certain conditions. This makes it easier to adapt the software for different settings or feature sets without altering the main codebase. Developers can manage code variations efficiently, such as turning on debugging features for development while keeping the production version clean, or adjusting software capabilities for different versions. This results in software that is adaptable, efficient, and simpler to maintain.
%defined - Checks if a macro_name is defined. If defined, then %defined is said to be true.%++ - Marks code that is compiled if the %defined is true.%-- - Marks code that is compiled if the %defined is false.%>> - Marks code that is compiled unconditionally within a %defined block.%end defined - Closes the %defined block that was started with
%defined.%test and
%test_ignore that are part of Sheerpower's
Unit Testing facility.
For details see
Unit Testing.
Sheerpower includes built-in macros for contextual control. These macros are predefined for direct use in conditional compilation.
The _spversion macro holds the version and
build of Sheerpower, formatted as V[major].[minor].
This enables version-specific coding.
The _user macro contains the developer's
login name, useful for user-specific script behaviors.
The _test macro checks if option test on has been
specified earlier in the code. If active, _test is set to 1 (true);
otherwise, it defaults to 0 (false). This macro is useful for enabling or disabling
test-specific code without additional flags. option test on is also set
if Sheerpower is invoked by sp4gl.exe /test
The true macro represents a boolean value of 1.
It enhances code readability by explicitly indicating a true condition.
The false macro represents a boolean value of 0,
improving clarity by making conditions for false explicit.
Example uses:
The %defined directive begins a conditional block. Specify the macro_name you want
to check.
The %defined directive in Sheerpower can also utilize comparison operators to enhance conditional compilation. This allows you to compare the value of a defined macro against string constants, numeric values, or the value of another macro.
%defined macro_name [compare_oper] 'string constant'
%defined macro_name [compare_oper] 42
%defined macro_name [compare_oper] macro_arg
Within a %defined block, use %++ for code to compile
if defined is true, %--
if defined is false, and %>> for unconditional code.
Ensure that %end defined closes
each %defined block. If any stray %++, %-- or %>>
directives are found without a
preceding %defined, a compile-time error will be raised.
For example, in a debug configuration, certain logs or debugging lines may be included:
%>> for Unconditional CodeThe %>> directive enables you to add code within a %defined block that always
compiles, regardless of the %defined status.
%include, variable assignments, and more.
This feature provides a high degree of flexibility, enabling the
conditional setup of entire files, modular logic, and
environment-specific routines within a single block.
For more information on defining macros, see the Macros and Blueprints tutorial.
(Show/Hide Sheerpower Conditional Compilation Takeaways)%defined with %++, %--,
and %>> to conditionally include or exclude code
blocks._spversion, _user,
_test, true, and false
enable environment-aware compilation.=, !=,
<, >, etc.) let you tailor logic
for specific versions, users, or build types.|
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. |