Skip to content
This repository has been archived by the owner on Dec 20, 2023. It is now read-only.

Improve code clarity regarding constants /or/ Use a template engine #3

Closed
AliSoftware opened this issue Dec 12, 2016 · 2 comments
Closed

Comments

@AliSoftware
Copy link
Collaborator

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'

then use it like:

PROPERTY_DEFAULT_TEMPLATE%[varName, varType, defaultValue]

We could instead declare this:

def property(name: nil, type: nil, defaultValue: nil)
  "dynamic var #{name}: #{type} = #{defaultValue}"
end

That way the call site would be more explicit:

Templates.property(name: "firstname", type: "String", defaultValue: "")

And that could also be type and arity-checked to ease understanding and debugging.

@AliSoftware AliSoftware changed the title Improve code clarity regarding template/constants Improve code clarity regarding constants /or/ Use a template engine Dec 14, 2016
@AliSoftware AliSoftware modified the milestone: 1.0.0 May 2, 2017
@AliSoftware
Copy link
Collaborator Author

This will be closed soon with the migration to templates in #16 🎉

@AliSoftware
Copy link
Collaborator Author

Solved in latest 1.0 release

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant