Home > C++ > Is QuantLib over engineered?

Is QuantLib over engineered?

QuantLib is a cross-platform, quantitative finance C++ library for modeling, pricing, trading, and risk management in real-life.QuantLib offers tools that are useful both for practical implementation and for advanced modeling. It features market conventions, yield curve models, solvers, PDEs, Monte Carlo (low-discrepancy included), exotic options, VAR, and so on.

With QuantLib we can adapt pricing, clendar , market and so on,it’s very flexible but flexibility implies complexity, and its very interesting to discover how QuantLib provide this flexibility and also limit the complexity of using it.

QuantLib has the reputation of being over engeinered and it use many design patterns, let’s discover with CppDepend the quality of implementation and design of QuantLib.

here’s the result of the analysis

Implementation

Implementation

Number of line of code

Methods with many number of line of code are not easy to maintain and understand, let’s search for methods with more than 60 lines.

SELECT METHODS WHERE NbLinesOfCode > 60 ORDER BY NbLinesOfCode DESC

Less than 2% of methods has more than 60 lines.

Cyclomatic complexity

Cyclomatic complexity is a popular procedural software metric equal to the number of decisions that can be taken in a procedure.

Let’s execute the following CQL request to detect methods to refactor.

SELECT METHODS WHERE CyclomaticComplexity > 10 ORDER BY CyclomaticComplexity DESC

So only 2% of methods can be considered as complex.

Which methods are complex and not enough commented?

SELECT METHODS WHERE CyclomaticComplexity > 10 AND PercentageComment < 20

only 3 methodes considered as complex and not enough commented.

Methods with many variables

Methods where NbVariables is higher than 8 are hard to understand and maintain. Methods where NbVariables is higher than 15 are extremely complex and should be split in smaller methods (except if they are automatically generated by a tool).

SELECT METHODS WHERE NbVariables > 15 ORDER BY NbVariables DESC

less than 1% methods has too many variables.

Types with many methods and fields

Let’s sarch for types with many methods, for that we can execute the following CQL request

SELECT TYPES WHERE NbMethods > 30 AND !IsGlobal ORDER BY NbMethods DESC

Only 23 types has many methods.

And we can do the same search for fields

SELECT TYPES WHERE NbFields > 20 AND !IsGlobal ORDER BY NbFields DESC

Only 12 types has many fields.

DESIGN

inheritance

Multiple inheritane increase complexity ,and we have to use it carefully.

Let’s search for class with many base classes.

SELECT TYPES WHERE NbBaseClass >1

72 classes has multiple base classes , but if you search for bases you can find that a majority of them derived from Observer.

let’s search for classes with multiple base and not derive from Observer, we will see later the interest of inheriting from Observer.

SELECT TYPES WHERE NbBaseClass >1 AND !DeriveFrom “QuantLib.Observer

The blue rectangles represent the result.

only 14 classes derived from more than one class and not derived from Observer.

Type cohesion

The single responsibility principle states that a class should have more than one reason to change. Such a class is said to be cohesive. A high LCOM value generally pinpoints a poorly cohesive class. There are several LCOM metrics. The LCOM takes its values in the range [0-1]. The LCOMHS (HS stands for Henderson-Sellers) takes its values in the range [0-2]. Note that the LCOMHS metric is often considered as more efficient to detect non-cohesive types.
LCOMHS value higher than 1 should be considered alarming.

SELECT TYPES WHERE LCOMHS > 0.95 AND NbFields > 10 AND NbMethods >10 AND !IsGlobal ORDER BY LCOMHS DESC

only 11 types are considered as no cohesive.

Efferent coupling

The Efferent Coupling for a particular type is the number of types it directly depends on.
Types where TypeCe > 50 are types that depends on too many other types. They are complex and have more than one responsability. They are good candidate for refactoring.

Let’s execute the following CQL request

SELECT TYPES WHERE TypeCe > 50 AND !IsGlobal ORDER BY TypeCe DESC

And the result is empty so no class has many responsabilities.

How about low coupling?

Template provide more flexibilty than OOP, but implies more complexity.
for example when a template need a Class as template param, there’s no constraints that this class inherit from what the template need but only a contrainst that this class T provide methods needed by the template.

This interesting post explain the tension between OOP and template programming.

Let’s discover template classes of QuantLib

SELECT TYPES WHERE IsTemplate AND !IsInTierProject

For no template class , the using of abstract classes can provide more flexibility and low coupling.
Take for example CmsMarket and search for abstract classes used.

SELECT TYPES WHERE IsDirectlyUsedBy “QuantLib.CmsMarket” AND IsAbstract

So many abstract classes are used and we can consider that CmsMarket is low coupled.

Layering between namespaces

CppDepend provide DSM graph, and we can triangularize this matrix to focus under red borders highly dependency cycle.

Implementation

A dependency cycle exist between QuantLib and QuantLib::detail, having this dependency is not problematic but avoiding this kind of dependency enforce loose coupling,this interesting post explain the benefit of layering.

Design Patterns used

PImpl

Managing calendar depends on culture and country, and we need a flexible way to provide a specific calendar.

Many calendars implementation are provided in QuantLib.

SELECT TYPES WHERE DeriveFrom “QuantLib.Calendar+Impl” ORDER BY DepthOfDeriveFrom

Calendar is a concrete class which no virtual methods. Polymorphism is implemented by storing into a Calendar instance a Handle to a CalendarImpl object which implements the polymorphic isBusinessDay() method and to which the Calendar instance delegates the task of determining whether a given day is a business day or a holiday.

Derived Calendar classes can be defined which initialize the Handle with the desired concrete CalendarImpl. The only task of such derived Calendars is to customize the initialization process, without adding any new functionality to the Calendar interface. As such, they can be upcasted to Calendar without any loss of information occurring.

this dependency graph show the relation between Calendar, Calendar::Impl and a specific Calendar implementation.


Observer

in the finance world there’s many calculations that depends on many parameters, for example an instruments depends on many classes, and it needs to keep track of changes that should cause them to recalculate their values.

Let’s see how many classes inherit from observer.

SELECT TYPES WHERE DeriveFrom “QuantLib.ObserverORDER BY DepthOfDeriveFrom

many classes derived from observer, it’s an elegant solution to keep track on any modifications.

Visitor
with this kind of library we need to add new operations to existing object structures without modifying those structures, for that the pattern visitor is very interesting.

This technique allows one to specialize calculations on a per-derived-class basis without increasing the number of virtual member functions in the base class interface.

Let’s see how many classes use Visitor pattern.

SELECT TYPES WHERE IsDirectlyUsing “QuantLib.Visitor

Implementation

We can consider that QuantLib is over engineered but a library for a finance must be generic, because the implementaion can be specific to country, bank or maybe hedge fund.
For that it must be well designed to be flexible and not too complex.

About these ads
Categories: C++
  1. Mark Richard-Fogg
    October 17, 2009 at 2:35 pm

    What a concise investigation.

    Just the answers I was seeking.

    Well done.

  1. July 23, 2010 at 2:21 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: