Chapter 3. Metaprogramming

We think in generalities, but we live in details.

Alfred North Whitehead

As developers, much of our time is spent designing and writing the algorithms and business logic that drives our application functionality. This logic is highly detailed and critically important to solving the core objectives of the software and, therefore, must be stable, accurate, and efficient in providing business solutions. Software development is a dynamic practice, though, and inevitably the details of our application code will change as business practices evolve or business rules change. These changes can and do occur at any time, and they will present risks to the quality of the application code if the application is improperly designed. It is imperative that, as developers, we embrace this reality and make every effort to write code that is flexible and adaptable to change, thus improving our ability to evolve and maintain the software without compromising quality.

One way to ensure flexibility in your application code is to simply write less code. Software applications tend to be laden with detailed logic rules that govern the behavior of the software. For example, details like installation path, required data sources, and class or method initialization parameters are often tightly coupled with the application business logic. This approach inherently requires more code and does not allow for easy or low-risk changes to the application logic. Applications with more flexible designs decouple this level of detail from their logic and algorithms by storing detailed instructions as metadata. This practice both encourages the development of more generic and reusable algorithms and reduces the amount of code that is needed to meet the objective of the application. Consider the quote from Alfred North Whitehead, a mathematician and philosopher, which describes the way people think versus the way they interact. Similarly, with flexible software application designs, algorithms and business logic are often generalized, while specific logic details are abstracted out of the algorithms and applied at run time. When designing applications, developers should defer the specific logic details, such as run-time configurations, out of the code and into metadata. This will help to increase the overall quality of the application by reducing its complexity and improving maintainability, while increasing the flexibility and adaptability to future changes.

What Is Metadata?

Metadata is often defined as data about data or, alternatively, as information that describes data. It is often used to illustrate attributes and contents of electronic media, such as images or documents. The most common example of metadata, but certainly not the only example, is that of a database schema, which provides descriptive rules or information about a particular dataset. In the example illustrated in 3-1, metadata is used to describe names or lengths of fields in the dataset, the data types of each field, or perhaps the relationships between fields across the dataset. Simply stated, metadata is descriptive detail about data that is abstracted from the data itself.

Partial Database schema—Adventure Works Database.

Figure 3-1. Partial Database schema—Adventure Works Database.

Metadata is often used in software development as a means to parameterize applications or to store static detail, like configuration or behavioral information, away from the logic of the application. It can represent items ranging from a button color or font size to algorithm selection, regular expression patterns, or security policy. Developers sometimes leverage metadata as a means to modify application behavior during installation or execution. This is often referred to as metaprogramming, and it spans several programming paradigms including, but certainly not limited to:

  • Native code. Applications developed in native Windows programming languages like Visual C++ or Visual Basic use a combination of initialization (.ini) files or the Windows registry to store application metadata like configuration or initialization parameters in key/value pairs.

  • Java. Application properties are configuration values stored as key/value pairs in property (.property) files of Java programs. Java provides application developers the ability to store and access application metadata in a structured XML format.

  • Managed code. Applications developed with the Microsoft .NET Framework can leverage the structure and flexibility of XML to store metadata in the application-level configuration files such as web.config for ASP.NET applications. Developers are provided the ability to store simple key/value pairs or more complex custom structures.

A key attribute of metaprogramming is the use of descriptive data to modify program behavior at run time. Early and less effective uses of metaprogramming, such as storing key/value pairs in .ini files or the Windows registry, did not provide developers with a great deal of flexibility or security. Initialization files lacked structure for storing hierarchical metadata, and the continued use of the Windows registry to store application metadata contributed to an ever-growing, critical, and single point of failure. When the Microsoft .NET Framework emerged, software developers realized greater flexibility for storing application settings and metadata. They were encouraged to leverage XML-based configuration files in the application’s root directory and, thus, eliminate the dependency on the Windows registry.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.133.233