Sunday, February 15, 2015

Program Reader Class – Call Chain Removal

As mentioned recently, having a chain of function calls is not good object-oriented design because it requires the initial caller to have knowledge of the internal members of each of the subsequent objects in the chain breaking encapsulation.  The table operand text virtual function contained this call chain:
return programLineReader.unit()->dictionary(m_operandType)->string(operand);
Before removing this call change and the equivalent one in the table remove virtual function, some code rearrangement was performed.  This included renaming the Program Line Reader class to simply Program Reader since it reads more than just from a program line (it also access a dictionary).  The Program Reader class and its functions were also moved to their own header and source files.

The get string for operand function was added with an operand type argument.  This eliminated the call chain in the operand text function, which now for clearly reads:
return programReader.getStringForOperand(m_operandType);
To prevent a call chain in this new function (though with one less function call), the get string from dictionary function was added to the program model class with operand type and operand arguments.  An internal read operand function was added for clarity because calling the function operator function internally is the not very self-explanatory this->operand()().

Similarly, the remove reference to operand function with an operand type argument was added for the remove table function along with a remove reference from dictionary program model function with the operand type and operand arguments.

The unit access function, which allowed the call chain in the first place, was no longer used and was removed.  This takes care of the function call chains when reading from the program.  The function call chain when writing to the program will be eliminated next.

[branch table commit 0e6751f023]

Table – Operand Text & Remove Virtual Functions

Like to the encode function, the twelve operand text and remove functions did the same things with different dictionaries.  These were replaced with a single operand text and remove functions.  Both check if the code has an operand.  The operand text function returns an empty string if the code does not have an operand, and the remove function does nothing.

Similar for the initialization of the table entries for the encode function pointers, the operand text and remove function pointer arguments of the secondary table constructor (used for code with no operands) were changed to dummy void pointers so all the table entries didn't need to be modified.

The operand text and remove function pointer arguments were removed from the primary table constructor and the associated function pointers were removed from the table entries of codes with operands.  The operand text function and remove function pointer members were removed from the table class along with their access functions.  And the twelve individual operand text and remove functions were removed.

The tester class function operator function was modified to use the single dictionary pointer access with operand type arguments.  The individual dictionary access functions were now no longer called and were removed.

[branch table commit c5cb6ed943]