If you have been programming for a while, you have likely experienced "déjà vu" when embarking on writing a new program. Many code and design patterns recur over and over within and across applications. When you realize that you have written something similar in the past, it makes sense to try to leverage this previous work to expedite what you are now trying to create.
Assuming you can remember where the code is that you want to leverage, you need to decide on the best way to take advantage of this existing code. In most cases you will be deciding between one of the following approches:
Let's review the advantages and disadvantages of each approach followed by an alternative, which is to develop a model to generate the similar components.
Developers often tend to choose the path of least resistance when faced with the challenge of building something similar to a component that already exists. This usually means copying the existing component and adapting the copy to satisfy new requirements. This approach has two main advantages:
On the other hand, there are a number of pitfalls to this approach that should be carefully considered. These include:
If it is possible to adapt an existing component to not only serve the needs of its original application but also handle new requirements this is generally preferred over cloning since it results in improved code reuse. Let's review the disadvantages of this approach.
Restructuring is similar to adapting except that it involves splitting the original component (class) into a Superclass and a Subclass and then creating a new Subclass to support your new requirements. This approach suffers from all of the same challenges described in the Adapting section.
Next, let's look at how GenHelm solves some of the problems described above.
When you instantiate a class in GenHelm, the framework first looks for the class within the context of the current site. If it is not found, the framework looks for the class within a common system class folder. This allows any number of websites to share common classes to improve code reuse.
In the adapt or restructure techniques we described earlier, we simply need to move the code that we want to reuse from a specific site to the common system folder then we can call it from both the original site and our new site.
Code generation is a more formal and robust approach to program cloning. Rather than copying and changing programs, code generation involves developing a parameterized model that can clone programs in a rigorous way. Typically, the model will accept parameters that control what code needs to be changed and which sections of code to include or exclude.
Formalizing the cloning process serves to eliminate all of the aforementioned pitfalls associated with cloning while preserving the main benefit, which is increased productivity.
One of the main advantages of informal code cloning versus developing a code generator is that cloning tends to be more expedient. That's because, most developers don't have a formal process for developing code generators. That's where GenHelm shines since it provides a complete framework for developing models and using these models to build components.
GenHelm ships with over 70 prebuilt models. Equally important is the fact that GenHelm makes it very easy for you to build models that are specific to your particular domain. Over time, as you develop new models, more and more of your applications will become completely generatable.
It is also important to note that code generation is not a one-time event. Instead, code is maintained by changing the parameters to a model and regenerating. As necessary, you can extend the models to handle more and more edge cases to increase the capabilities of programs that can be generated using the model.