Preprocessors and Metaprogramming in Solidity

The first thread I posted in related to an old feature request for #define in Solidity, which has been rejected. However, using a preprocessor with Solidity can help to reduce gas costs. There is likely at least 100 Ether that has been spent on unused, pointless code deployed to mainnet! If someone could analyze the whole chain and calculate an actual number, perhaps with graphs over time, that would be really awesome. Stuff like uncalled functions, unreachable code, and redundant variables. Anyway…

Preprocessors can help reduce gas costs in both deployment and execution, and there is clearly lots of interest in using them. I demonstrated the usefulness in one of my projects here. There are a few preprocessors built for Solidity, but it seems like they are unmaintained. I’d love to generate some discussion about this. What’s the best preprocessor to use? Should the community try to revive one of these old projects? How can we educate people about this?

You need to provide some evidence to prove this.
I, for one, cannot think of any.

The constant and immutable keywords can be used wisely in order to reduce byte-code size (deployment gas cost) and storage-reading (runtime gas cost).

Regardless of whether or not Solc splits the process into preprocessing + compilation + linking, the constant keyword yields the same effect as the preprocessing phase in native-language compilers.

I used the C preprocessor to reduce gas costs by including and excluding certain functions and variable declarations in the project I linked above. It is essentially producing one of many possible Solidity source files depending on the configuration decided upon by the developer in order to reduce gas costs depending on the way the contract is intended to be used. At the very least if there are optional features excluded then it saves deployment costs.

There are also OpenZeppelin contracts which are built with some JavaScript code, and other developers expressed interest in using metaprogramming in the other thread.

I wasn’t aware of constant. I’m wondering if it helps optimize the code? In the code block here could the compiler optimize out the if block if decimals == 0 and decimals were a constant instead of immutable?

I’ve started to make a brand new preprocessor for the Solidity, called “@bgrusnak/solp”. You can find it in the npm packages.
Now it has only define/undef macros, and loading environment variables, but I have plans to extend it for the usage all of C-like macro commands.

Truffle Preprocessor (truffle-preprocessor) It can be integrated into your Truffle project. It offers features like conditional compilation and file imports.