|
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
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
.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:
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 @macroname
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 can be defined in two ways: as single-line or multi-line macros.
To expand a macro with placeholders, use the
@macroname
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:
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 macroname = value |
Not applicable | Macros: Simple constants or values |
Macro Expansion | @macroname |
Not applicable | Macros: Concisely expands to a single line or entire block of code. |
Macro Expansion with placeholder substitutions | @macroname (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 assist in debugging macros and blueprints you can view the expanded versions of these code constructs. This feature is particularly useful when you need to verify the correctness of the expansions.
Toggling Expansion Views: To view the expanded
versions of any macros or blueprints, you can toggle the view using
the %expand on
and %expand off
commands.
Surround the @macro
or %generate
statement
with %expand on
and %expand off
to see the
expansion.
This will display the expanded content of the macro and blueprint during 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. |