I have no fragment, but I have an example of the real world. The linear calibration constants (mass spectrometry field) in the applications were global, and there was complex code for storing and restoring global calibration constants for different spectra. The use of two values โโwas common over the program, and it was difficult to change or verify that the conversion between uncalibrated and calibrated mass values โโusing two constants were correct in all cases.
I will reorganize by encapsulating the two calibration constants in the class, which is responsible for converting uncalibrated and calibrated mass values. Functions to perform the conversion was also introduced, so it was centralized in one place in the program, and not distributed throughout the program. This encapsulation later facilitated the introduction of a new type of calibration (non-linear).
Instead of accessing two global variables, the class that represented the spectrum would instead have and use an instance of a new calibration class, each instance with its own set of calibration constants.
source share