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, calendar, market and so on, it's very flexible but flexibility implies complexity, and it's very interesting to discover how QuantLib provide this flexibility and also limit the complexity of using it.
QuantLib has the reputation of being over engineered 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:
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 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 methods considered as complex and not enough commented.
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.
Let's search for types with many methods, for that we can execute the following CQL requestSELECT TYPES WHERE NbMethods > 30 AND !IsGlobal ORDER BY NbMethods DESC
Only 23 types has many methods.
And we can do the same search for fieldsSELECT TYPES WHERE NbFields > 20 AND !IsGlobal ORDER BY NbFields DESC
Only 12 types has many fields.
Multiple inheritance 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.
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.
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 responsibility. 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 responsibilities.
How about low coupling?
Template provide more flexibility 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 constraints 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 QuantLibSELECT 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.
CppDepend provide DSM graph, and we can triangularize this matrix to focus under red borders highly dependency cycle.
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.
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.
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.Observer" ORDER BY DepthOfDeriveFrom
Many classes derived from observer, it's an elegant solution to keep track on any modifications.
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"
We can consider that QuantLib is over engineered but a library for a finance must be generic, because the implementation can be specific to country, bank or maybe hedge fund. For that it must be well designed to be flexible and not too complex.