How CodeStencil Works

CodeStencil uses Stencils (similar to templates but much more powerful) to generate structured code. These Stencils provide a flexible and dynamic way to manage code patterns, allowing developers to easily customize and extend their projects. By leveraging this approach, CodeStencil helps improve code maintainability and reduce manual coding errors. This method also supports scalability, making it easier to adapt projects as requirements evolve over time. Furthermore, by automating repetitive coding tasks, developers can focus more on creative problem-solving instead of routine coding. As a result, this system enhances overall development efficiency and consistency across different modules.

CodeStencil stores fragments of text in a tree-like structure (Code Tree). This stored text can contain Expansion Labels (similar to variables), which can be replaced with predefined strings during code generation. These labels enable dynamic changes to parts of the code based on specific requirements, adding versatility to the generation process. The process of code generation starts at the root node of the Code Tree and proceeds down to the last node. This traversal ensures that all necessary code fragments are correctly assembled in the proper order, resulting in a coherent and fully functional output. Additionally, this organized structure simplifies debugging and updating code, since changes in one part of the tree propagate correctly throughout the generated codebase. This hierarchical approach also supports the reusability of code fragments, which can significantly shorten development cycles. Overall, the tree-like organization combined with Expansion Labels offers a powerful framework for generating robust and adaptable code.

image.png

Expansion Objects are objects that, when encountered during the code generation cycle, will translate to some form of textual content. These objects play a crucial role in transforming abstract representations into concrete code snippets or textual outputs. There are 2 types of Expansion Objects - Expanders and Code Nanites. Each type serves a distinct purpose in the generation process, with Expanders typically handling textural expansion and Code Nanites focusing on more granular, micro-level code insertions based on programmable code snippets. Together, they enable a flexible and dynamic approach to code generation.

Expanders

If you want to insert a string (during code generation) representing, say, your namespace, all you have to do is define the Expansion String and the replacement text in the Expander panel. From the screenshot, NAMESPACE will be replaced by "Dolasoft" during code generation. You can even combine Expanders, for example, a construct like:

[%ORGANIZATION_LABEL%].[%PROJECT_NAME%] will expand to a string like MicroSoft.MyProject

 

Code Nanites

Code Nanites are blocks of code that can be executed when encountered during code generation.

For example, this code nanite -  CS_TABLE_LIST when combined with ".cs" creates a list of files based on table names in the Schema.

Unlike standard code generators, you do not need to learn a template language. If you do not want to build your stencil from scratch, you can create it within minutes from your source code using Reverse Engineer Source Code


Was this article helpful?