You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Dec 20, 2023. It is now read-only.
Using constants in templates.rb is nice to ensure we always use the same format, but makes the code difficult to read, especially as we also use the uncommon CONSTANT%[param1, param2] ruby syntax to expand them printf-style.
First solution: use a template engine like Mustache
This would allow us to design the output we want using a template file that would improve the readability, letting us better see the kind of output we'd have.
But template engines like Mustache wouldn't be powerful enough to handle all the specific cases, condition testing and all reformatting that we need for the files dbgenerator currently generates.
Migrating to a template engine like Mustache would thus probably mean making dbgenerator provide to those templates a context that would be quite tailored to what we need, e.g. a context containing some variables that already have some pre-computed output that wouldn't be possible to be generated from Mustache directly. That would make it a not so nice workaround.
Second solution: Replace constants with functions
Instead of using constants and the CONSTANT%[x, y] syntax, not really knowing which parameter we're supposed to pass and in which order, and how many… and not making the code as readable, one option would be to define a set of ruby functions (with a given arity and explicit parameter names) to do the same instead.
e.g instead of defining like currently:
PROPERTY_DEFAULT_TEMPLATE='dynamic var %s: %s = %s'
AliSoftware
changed the title
Improve code clarity regarding template/constants
Improve code clarity regarding constants /or/ Use a template engine
Dec 14, 2016
Using constants in
templates.rb
is nice to ensure we always use the same format, but makes the code difficult to read, especially as we also use the uncommonCONSTANT%[param1, param2]
ruby syntax to expand them printf-style.First solution: use a template engine like Mustache
This would allow us to design the output we want using a template file that would improve the readability, letting us better see the kind of output we'd have.
But template engines like Mustache wouldn't be powerful enough to handle all the specific cases, condition testing and all reformatting that we need for the files
dbgenerator
currently generates.Migrating to a template engine like Mustache would thus probably mean making
dbgenerator
provide to those templates a context that would be quite tailored to what we need, e.g. a context containing some variables that already have some pre-computed output that wouldn't be possible to be generated from Mustache directly. That would make it a not so nice workaround.Second solution: Replace constants with functions
Instead of using constants and the
CONSTANT%[x, y]
syntax, not really knowing which parameter we're supposed to pass and in which order, and how many… and not making the code as readable, one option would be to define a set of ruby functions (with a given arity and explicit parameter names) to do the same instead.e.g instead of defining like currently:
then use it like:
We could instead declare this:
That way the call site would be more explicit:
And that could also be type and arity-checked to ease understanding and debugging.
The text was updated successfully, but these errors were encountered: