|
Developers often repeat similar code, leading to redundancy, inconsistencies, and potential bugs. Macros and Blueprints in Sheerpower help reduce this by creating reusable code blocks, improving maintenance and efficiency.
In this tutorial, we will explore two powerful facilities in Sheerpower: Macros and Blueprints. These tools allow developers to generate reusable code structures, making programming more efficient and reducing redundancy.
Macros are ideal for dynamic expansions where parameters need to be substituted into small, reusable code snippets.
Blueprints are suited for generating larger, structured blocks of code. For example, handling similar logic for customers, clients, and employees with minor variations can be efficiently handled using blueprints.
%generate
directive. So, if
there are 5 arguments, the blueprint code will be duplicated 5 times,
each tailored to one of the arguments.@macro_name
is used. The number of arguments
doesn't affect the number of code copies; it just influences how the macro
expands at each occurrence of @macro_name
.In collaborative projects, maintaining consistency is crucial when multiple people contribute to the same codebase. Macros and Blueprints play a key role in standardizing repetitive tasks and logic.
By defining critical code blocks with macros or blueprints, teams can ensure:
blueprints
and macros
, placeholders are enclosed within "[[" and "]]".
This design choice makes them easy to identify within the code.
To generate code:
This approach ensures that all developers follow the same procedure, reducing miscommunication and errors, while improving overall maintainability.
Blueprints can be extended to reuse and add new functionality. When extending a blueprint, new content is appended to or prepended before the base blueprint’s content when generating the final code.
Let’s define three blueprints:
When you generate code using extended_handler
with a specific
argument, both the base blueprint and the extended content are included:
Generated output:
Similarly, generating code with prepended_handler
positions
the new content before the base blueprint’s content:
Generated output:
In addition to the standard [[placeholder]]
, Sheerpower Blueprints support
[[placeholder.function]]
, allowing developers to manipulate the replacement
text using specific formatting functions. These functions can adjust the case of the
replacement text as follows:
.lcase
– Replaces the placeholder with the
lowercase version of the replacement text..ucase
– Replaces the placeholder with the
uppercase version of the replacement text..ccase
– Replaces the placeholder with the
capitalized version (first letter capitalized, the rest in lowercase) of the replacement text.Example Usage
Generated Output
This new functionality allows more flexible handling of placeholder values, helping tailor output to specific formatting requirements.
Key Benefits:
%generate
The %generate
statement in Sheerpower Blueprints supports
sequence placeholders, which allows for automatic generation of sequence
numbers in blueprint parameters. This feature simplifies handling repetitive
elements with distinct names.
When a parameter in the %generate
statement ends with one or
more underscores (_
), each underscore acts as a placeholder for
a sequence number. The sequence number starts at 1 and increments with each
repeated use of the same parameter name.
Generated output:
Macros in SheerPower enable text substitution at compile time,
allowing developers to create concise and reusable code snippets.
Macros are expanded dynamically using the @macro_name
syntax. Placeholders in macro definitions are marked with
[[placeholder]]
and can have default values set using
the equal sign (=
). If a value isn’t provided during
expansion, the default value will be used.
Macros in Sheerpower can be defined in two ways: as single-line or multi-line macros. In this section, we will focus on single-line macros.
Single-line macros allow you to create simple text replacements in your code. These macros are particularly useful for defining constants or shorthand notations that improve code readability and maintainability.
The macro maxsize
is defined with the value
500
using the =
form. The macro
maxsize2
is defined with the same value but without
the =
symbol.
Both @maxsize
and @maxsize2
will be
replaced with 500
during macro expansion. This allows
for flexible and concise code, avoiding hard-coding values directly
into the program.
Single-line macros are ideal for simple value substitution and are expanded inline during the code execution, making them a powerful tool for reducing redundancy and improving code clarity.
To expand a macro with placeholders, use the
@macro_name
syntax followed by a list of
placeholder=value
pairs in parentheses.
Macros in SheerPower can be nested, allowing one macro to reference another. A macro must be defined before it can be referenced.
When @one
is called, it prints "in one" and
immediately calls @two
, which prints "in two". This
ensures efficient code reuse and clean structuring.
This prints:
The macro code below defines two macros in Sheerpower that automate the execution of code and measure how long it takes to run. Let's go through each part step by step.
mycode
macro defines a simple loop that counts
from 1 to 10,000,000.
This macro does not perform any operations inside the loop, it simply iterates 10 million times.
time_it
macro measures how long the code
execution takes. It uses the following steps:
This macro prints the expression being timed, starts a timer, runs the expression, and then prints the elapsed time.
@time_it(expr=@mycode)
line calls the
time_it
macro and passes the mycode
macro as an argument. The mycode
macro runs
inside time_it
, and the execution time is printed.
time_it
macro can be used to measure how
long it takes to run any code inside of the mycode
macro.
Blueprints are similar to macros but focus on generating
reusable templates for larger code blocks. You use the
%generate
directive to dynamically replace placeholders in
blueprints.
To generate the code:
Generated code for customer:
Feature | Macros | Blueprints | Best Use |
---|---|---|---|
Use Case | Dynamic code generation | Structured code generation | Macros: Small, repeatable code snippets Blueprints: Larger, structured logic |
Flexibility | Small, flexible code blocks | Large, predefined code templates | Macros: Frequent substitutions Blueprints: Organizing larger repetitive structures |
Shortcut Macro Definition | macro macro_name = value |
Not applicable | Macros: Simple constants or values |
Macro Expansion | @macro_name |
Not applicable | Macros: Concisely expands to a single line or entire block of code. |
Macro Expansion with placeholder substitutions | @macro_name (placeholder1=value1, placeholder2=value2, etc.) |
Not applicable | Macros: Concise expansions with placeholder substitutions |
Code Reusability | Dynamic placeholders | Customizable placeholders | Macros: Best for repetitive patterns Blueprints: Best for modular templates |
To help with debugging macros and blueprints, you can view their expanded versions. This feature is especially useful when you need to confirm that the expansions are correct.
This will display the expanded content of the macro and blueprint during code validation.
%expand on
and %expand off
, helping you ensure that the macros or
blueprints are correctly expanding as expected.Use this feature when:
This feature simplifies debugging by providing a clear view of the generated code, making it easier to troubleshoot and validate expansions in your projects.
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. |