liu.seSearch for publications in DiVA
Change search
ReferencesLink to record
Permanent link

Direct link
On Patterns for Refactoring Legacy C++ Code into a Testable State Using Inversion of Control
Linköping University, Department of Computer and Information Science.
2010 (English)Independent thesis Advanced level (professional degree), 20 credits / 30 HE creditsStudent thesis
Abstract [en]

Amending old projects of legacy code to include agile practices such as extensive unit testing and refactoring has proven difficult. Since automated unit testing was not widely used a decade ago, much code has been written without unit testing in mind. This is especially true for C++ where RAII has been the dominant pattern .This has resulted in a lot of code that suffers from what best can be described as low testability. This also strongly impedes the creation of new unit tests to already existing code. Due to the lack of unit tests, refactoring is done sparsely and with great reluctance.

This thesis work tries to remedy that and, in the scope of a limited case study on an existing code base, looks into different ways of creating and utilizing object seams in legacy C++ code to decouple dependencies to make isolated testing possible. This regards to:

  • What are the impediments for code to be testable in an isolated setting?
  • What are the steps for refactoring code to a testable state?

The results can be summarized as to contain a list of factors affecting testability, among them: the use of asserts, global state, object instantiation, work in constructor and breaking Law of Demeter. Further with regards to patterns for refactoring code to a testable state, two types of patterns have crystallized: the injection of dependencies and the masking of dependencies using various techniques. The effect these two base patterns have on breaking dependencies on the base level and the Meta level is outlined. A catalogue of patterns has been compiled as part of the appendix.


Inversion of Control (IoC) is a principle used to decoupling classes and since strong dependences is often an attribute giving grievances with regard to testability, it was a central concern in this thesis. IoC can be simplified from a developer standpoint with the help of frameworks or what is usually referred to as IoC containers. Two IoC containers for C++ were evaluated:

  • Autumn Framework
  • PocoCapsule

In the evaluation of the two IoC containers it was concluded that Autumn was not mature enough as of the time of the evaluation to be used in production setting. PocoCapsule, even though compelling for some of its powerful features with regard to DSM and HOT, its configuration sometimes require in-code workarounds, affecting its usability in some set of scenarios.

However, the big difference was with regard to how the two containers approaches configuration. PocoCapsule uses static analysis of its XML configuration file making it truly declarative while Autumn does runtime parsing and dynamic invocations resulting in a situation closer to procedural scripting.

Place, publisher, year, edition, pages
2010. , 108 p.
Keyword [en]
Refactoring, Software Development, Coding Practices, Software Testing, Inversion of Control, Dependency Injection, Test Driven Development, Unit Testing, Inversion of Control Containers, Object Oriented Design
National Category
Software Engineering
URN: urn:nbn:se:liu:diva-59238ISRN: LIU-IDA/LITH-EX-A--10/009--SEOAI: diva2:374146
2010-02-18, Linköping, 08:15 (English)
Available from: 2010-12-03 Created: 2010-09-09 Last updated: 2010-12-03Bibliographically approved

Open Access in DiVA

fulltext(1339 kB)893 downloads
File information
File name FULLTEXT01.pdfFile size 1339 kBChecksum SHA-512
Type fulltextMimetype application/pdf

Search in DiVA

By author/editor
Böhlin, Per
By organisation
Department of Computer and Information Science
Software Engineering

Search outside of DiVA

GoogleGoogle Scholar
Total: 893 downloads
The number of downloads is the sum of all downloads of full texts. It may include eg previous versions that are now no longer available

Total: 1412 hits
ReferencesLink to record
Permanent link

Direct link