In: Computer Science
Q From a programming language perspective, describe what orthogonality is, briefly explain any benefits or detriments of having a higher or a lesser degree of orthogonality.
Q In computer memory, variables are bound to specific memory addresses and have a lifetime. Based on their lifetimes, what are the four categories of these storage bindings? Briefly describe each of the four variable storage bindings.
Orthogonility is a concept often used to describe modular and maintainable software, but it's more easily understood by way of a case study. In this article, Jens Dietrich demystifies orthogonality and some related design principles by demonstrating their use in the popular Log4j utility library. He also discusses how Log4j violates orthogonality in a couple of instances and discusses possible workarounds to the issues raised.
The concept of orthogonality is based on the Greek word orthogōnios, meaning "right-angled." It is often used to express the independence between different dimensions. When an object moves along the x-axis in a three-dimensional space, its y and z coordinates don't change. Change in one dimension does not cause change in another dimension, which means that one dimension cannot cause side-effects for others.
This explains why the concept of orthogonality is often used to describe modular and maintainable software design: thinking about systems as points in a multi-dimensional space (spawned by independent, orthogonal dimensions) helps software developers to ensure that our changes to one aspect of system will not have side-effects for another.
It happens that Log4j, a popular open source logging package for Java, is a good example of a modular design based on orthogonality.
The dimensions of Log4j
Logging is just a fancier version of the System.out.println() statement, and Log4j is a utility package that abstracts the mechanics of logging on the Java platform. Among other things, Log4j features allow developers to do the following:
While Log4j does have other features, I will focus on these three dimensions of its functionality in order to explore the concept and benefits of orthogonality. Note that my discussion is based on Log4j version 1.2.17.
Considering Log4j types as aspects
Appenders, level, and layout are three aspects of Log4j that can be seen as independent dimensions. I use the term aspect here as a synonym for concern, meaning a piece of interest or focus in a program. In this case, it is easy to define these three concerns based on the questions that each addresses:
Storage Binding of a variable is the binding of a variable name to a memory address. The type of storage binding determines the lifetime of a variable; the lifetime of a variable is the time during which it is bound to a particular memory cell. Storage binding begins with memory allocation - getting a cell from some pool of available cells. Storage binding ends with deallocation - putting a cell back into the pool.
Categories of Variables by Storage Binding
1. Static- Bound to memory before execution begins and remains bound to the same memory cell throughout execution, e.g., all FORTRAN 77 variables, C static and extern variables and C++ static members in a C++ class.
Advantages: efficiency (direct addressing), history-sensitive subprogram support Disadvantage: lack of flexibility (no recursion)
2. Stack-Dynamic - Storage bindings are created for variables when their declaration statements are executed at runtime. If scalar, all attributes except address are statically bound Ex: local variables in C functions and C++/Java methods return values, arguments pass by value.
Advantage: allows recursion; conserves storage
Disadvantages: Overhead of allocation and deallocation; Subprograms cannot be history sensitive; Inefficient references (indirect addressing)
3. Explicit Heap-Dynamic - Allocated and deallocated explicitly by the programmer, occurs during execution. Referenced only through pointers or references, e.g. dynamic objects in C++ (via new and delete), all objects in Java
Advantage: provides for dynamic storage management Disadvantage: inefficient and unreliable
4. Implicit Heap-Dynamic - Allocation and deallocation caused by assignment statements. All variables in APL; all strings and arrays in Perl and JavaScript; all objects in Java; Advantage: flexibility Disadvantages -- Inefficient, because all attributes are dynamic and loss of error detection;