2019/01/26

Code Duplication

by Lucido Group

Many developers copy and paste code from one plugin to another. One of the first principles taught to developers is to keep code DRY: don’t repeat yourself.

The most common incidences are logging. Logging should be handled in a single location using a logging class to be consistent for all solutions.

Some of the most costly infractions involve duplication of business logic. For example, an accounting result to calculate unrealized P&L has code that includes the basic formula unrealized P&L = MTM - cost. There is logic in the plugin to calculate cost for different instruments. A second accounting result is responsible for calculating realized P&L, which includes the same business logic to derive the cost for various instrument types. The logic to derive the cost of the instruments is the same in both plugins, but was copied and pasted.

The better way to do this would be to follow an object-oriented design. Create an object that models the instruments and has knowledge of the derivation of cost, and likely many other properties that apply to the instrument. This new object would be instantiated in the two accounting results, which would be left with very little business logic, since the calculations are controlled in the new instrument object or calculation agent.

Code duplication can arise fairly easily when more than one developer is working on a project. The simple solution is for developers to collaborate to a minimal degree, to subject code to consistent standards, and to have a code reviewer enforce standards.