Motivations of choosing C over C++ : Git case study

Linux, Php, and Git are a popular projects developed with C, in the other side OpenOffice, firefox, Clang, Photoshop are developed with C++, so it’s proven that each one is a good candidate to develop complex applications. Try to prove that a language is better than the other is not the good debate. However we can discuss motivations behind choosing one of them.

There are two major arguments quoted each time when we discuss choosing C:

  • Best performance.
  • Compiler support.

Continue reading “Motivations of choosing C over C++ : Git case study”

Abstractness vs Instability: Neo4j Case study

Robert C.Martin wrote an interesting article about a set of metrics that can be used to measure the quality of an object-oriented design in terms of the interdependence between the subsystems of that design.

Here’s from the article what he said about the interdependence between modules:

What is it that makes a design rigid, fragile and difficult to reuse. It is the interdependence of the subsystems within that design. A design is rigid if it cannot be easily changed. Such rigidity is due to the fact that a single change to heavily interdependent software begins a cascade of changes in dependent modules. When the extent of that cascade of change cannot be predicted by the designers or maintainers the impact of the change cannot be estimated. This makes the cost of the change impossible to estimate. Managers, faced with such unpredictability, become reluctant to authorize changes. Thus the design becomes rigid.

Continue reading “Abstractness vs Instability: Neo4j Case study”

Go inside the Eclipse IDE implementation to understand how OSGi works

OSGi became very popular today, thanks to its modularity approach and its capability to enforce logical boundaries between modules.  Many known applications chose it to enforce the modularity, we can enumerate eclipse, GlassFish and Oracle JDeveloper.

To understand OSGi concepts we will try to go inside eclipse IDE, which uses equinox as OSGi container. Continue reading “Go inside the Eclipse IDE implementation to understand how OSGi works”

Feedback on porting .Net code base to Mac OSX

Few years ago, after releasing the CppDepend product, many users ask us for a Mac OSX version to analyze the Objective C code base, last year we decided to develop XClarify, the CppDepend like for Objective C.

Our GUI was developed using .Net and porting all the code to Objective C has the big drawback to maintain two versions, time for our  developement team to make some research on how to reuse our .Net code for the Mac OSX version?

What’s the solution to port a .Net code base to Mac OSX?

When searching for all possiblities, the trivial one was to use Mono, it wasn’t an easy task, specially because we use DevExpress which is not compliant with Mono due to the overuse of P/Invoke calls.

In our first version we used the Mono Winforms implementation to avoid big changes on our code base, and we finally released our beta version. After contacting some Objective C developers to give XClarify a try, we discoverd our big mistake, and here are some feedbacks from Mac OSX users:

Having an application that looks like Visual Studio on OS X is enough of an insult.

I’ve downloaded your product for the trial but when I’m trying to run it it is asking for X11. I’ve read a couple of things on line that says that installing this X11 over Apples could cause instabilities – what are the risks in doing this and why is it needed?

Thanks to beta users we were warned about the biggest mistake we have done and time for us to quickly find another solution to correct this, so how to make a product that meets the Mac OSX users requirements?

The solution was to use the cocoa framework, and fortunately a cocoa porting to Mono is provided by Xamarin which offers the MonoMac library, but using MonoMac will impact all the Gui code base, and we can’t have anymore one codebase to maitain. but we learned well the lesson, what’s more important is not to have one code base to maitain but what the user is waiting for when he uses the product.

We decided for our second version to use MonoMac and remove all dependencies with System.Windows.Forms and System.Drawing libraries to use Cocoa instead.

Concerning removing the dependency with System.Drawing, there’s the following library, but it’s not mature yet, and some blocking bugs will encourage you to not using it.

To make the porting easier, we needed a good Gui library which simplify the Gui developement, and we found a powerful library, named Eto. Its developement team is very active and the library is stable.

After releasing our second version, we had to package the product to a standalone application which must be easy to install without installing other requirements. The MonoMac library is not sufficient for that and you have to use Xam.Mac which is not free.

As conclusion if you want to port your .Net application to Mac OSX, an interesting solution is to use Xamarin Studio, Xam.Mac and Eto libraries, and especially avoid using System.Windows.forms.

Never use MD5 Hashing algorithm when using the .Net framework

Two years ago, our support team received a request from a customer, which said that our product doesn’t works in a specific machine.
After further investigations we found the problem, but it was very difficult to detect what’s exactly make this machine different from others and which configuration cause the problem?

The source of the problem was this registry key :

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa\fipsalgorithmpolicy

What’s fips in this registry key is related to?

From Wikipedia:

Federal Information Processing Standards (FIPS) are publicly announced standardizations developed by the United States federal government for use in computer systems by all non-military government agencies and by government contractors, when properly invoked and tailored on a contract. The purpose of FIPS is to ensure that all federal government and agencies adhere to the same guidelines regarding security and communication.

The FIPS 140-2 is used to accredit cryptographic modules. And MD5 is not FIPS compliant.

By default the FIPS mode is disabled in windows machines, however the user could enable it if he want, and if so, your software will stop running as expected if you use the MD5 hash algorithm.

Microsoft doesn’t recommend enabling FIPS as explained in this article, but the possibility to enable it still exist and we cannot force our customers to disable it.

In our case we are lucky that the customer helped us to understand this issue, but in many cases it will be very difficult to understand its origin.

As conclusion it’s better to avoid the MD5 hashing algo for windows developement.

QuantLib

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 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 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:

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 CQLinq 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 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 search for types with many methods, for that we can execute the following CQLinq 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 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.

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 responsibility. They are good candidate for refactoring.

Let’s execute the following CQLinq 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 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.

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

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.

Learn some basic design principles from GRASP: Spring case study.

A design pattern is a general reusable solution to a commonly occurring problem within a given context in software design. Patterns are formalized best practices that the programmer can use to solve common problems when designing an application or system.

“Gang of Four” patterns are maybe the most popular ones. However, there are some basic design principles not well-known by developers, it’s the General Responsibility Assignment Software Principles, abbreviated GRASP.

GRASP consists of guidelines for assigning responsibility to classes and objects in object-oriented design. In this article we will discover the use of  these principles in the Spring framework.

Spring is one of the most popular application development framework for enterprise Java.
The Spring Framework does not impose any specific programming model, it has become popular in the Java community as an alternative to, replacement for, or even addition to the Enterprise JavaBean (EJB) model.

Spring makes things easy for software designers, it gives them a good basis to have a well-designed projects. But what about Spring itself it’s well designed as well?
To try answering this question let’s X-Ray some Spring jars with JArchitect and discover some of its design facts.

After the analysis here’s the dependency graph between all Spring jars analyzed:

The Matrix view gives us more details about the dependency weight between these jars.

GRASP Principles

Coupling

The loose coupling is desirable because a change in one area of an application will require less changes throughout the entire application. In the long run, this could alleviate a lot of time, effort, and cost associated with modifying and adding new features to an application.

Using interfaces and abstract classes can improve the loose coupling and we can evaluate the abstractness of a defined module by the following metric:

A = Na / Nc
Where:
* A = abstractness of a module
Zero is a completely concrete module. One is a completely abstract module.
* Na = number of abstract classes in the module.
* Nc = number of concrete classes in the module.

Here’s the abstractness of all Spring jars analyzed:

Let’s discover all interfaces of the jars analyzed, for that we can execute the following query:

SELECT TYPES WHERE IsInterface

With a good abstractness ratio, and many interfaces used we can say that Spring enforces low coupling, what makes it very flexible.

High cohesion

The single responsibility principle states that a class should not 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 LCOM HS (HS stands for Henderson-Sellers) takes its values in the range [0-2]. A LCOM HS value highest than 1 should be considered alarming. Here are algorithms used to compute LCOM metrics:

  • LCOM = 1 – (sum(MF)/M*F)
  • LCOM HS = (M – sum(MF)/F)(M-1)
    Where:
  • M is the number of methods in class (both static and instance methods are counted, it includes also constructors, properties getters/setters, events add/remove methods).
  • F is the number of instance fields in the class.
  • MF is the number of methods of the class accessing a particular instance field.
  • Sum(MF) is the sum of MF over all instance fields of the class.

The underlying idea behind these formulas can be stated as follow: a class is utterly cohesive if all its methods use all its instance fields, which means that sum(MF)=M*F and then LCOM = 0 and LCOMHS = 0. 

LCOMHS value higher than 1 should be considered alarming.

SELECT TYPES WHERE LCOMHS > 0.95 AND NbFields > 10 AND NbMethods >10

Only 4 classes are considered as not cohesive.

 Creator

When we use interfaces, one interesting question is: who is responsible of creating concrete implementations? Spring provides a good solution using dependency injection, but it’s not the way used inside Spring itself. Spring use Factories to acheive this job, and as you can see it implements many factory classes:

Information Expert

Information Expert is a principle used to determine where to delegate responsibilities. These responsibilities include methods, computed fields and so on.
Using the principle of Information Expert a general approach to assigning responsibilities is to look at a given responsibility, determine the information needed to fulfill it, and then determine where that information is stored.

The Efferent Coupling metric can be an interesting metric to evaluate if some types has more responsibilities, The Efferent Coupling for a particular type is the number of types it directly depends on.

Types where the efferent coupling  is more than 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.

SELECT TYPES WHERE TypeCe > 50

Polymorphism

According to Polymorphism, responsibility of defining the variation of behaviors based on type is assigned to the types for which this variation happens. This is achieved using polymorphic operations.

Let’s search for all polymorph methods, for that we can execute the following request:

SELECT METHODS WHERE NbOverloads>1

The Metric view gives us a good idea of using polymorphism inside Spring. In the Metric View, the code base is represented through a Treemap. Treemapping is a method for displaying tree-structured data by using nested rectangles. The tree structure used  is the usual code hierarchy:

  • Java projects contains packages
  • Packages contains types
  • Types contains methods and fields

Protected Variations

The Protected Variations pattern protects elements from the variations on other elements (objects, systems, subsystems) by wrapping the focus of instability with an interface and using polymorphism to create various implementations of this interface.
The “Abstractness vs Instability” graph can be useful to detect projects that will be difficult to change.

The idea behind this graph is that the more a code element of a program is popular, the more it should be abstract. Or in other words, avoid depending too much directly on implementations, depend on abstractions instead. By popular code element I mean a project (but the idea works also for packages and types) that is massively used by other projects of the program.
It is not a good idea to have concrete types very popular in your code base. This provokes some Zones of Pains in your program, where changing the implementations can potentially affect a large portion of the program. And implementations are known to evolve more often than abstractions.

The main sequence line (dotted) in the above diagram shows the how abstractness and instability should be balanced. A stable component would be positioned on the left. If you check the main sequence you can see that such a component should be very abstract to be near the desirable line – on the other hand, if its degree of abstraction is low, it is positioned in an area that is called the “zone of pain”.

Almost all spring jars are inside the green zone, it’s a good point when changes are needed for maintenance or evolution.

Conclusion

Spring uses all the GRASP principles, what makes it simple to understand and maintain, and developers can learn interesting design choices  when discovering and analyzing the Spring source code.

So give a look inside the source and enjoy with all design best practices implemented.

QtCore

Qt is a cross-platform application development framework, widely used for the development of GUI programs ,and also used for developing non-GUI programs such as console tools and servers.
Qt consists of several modules, and each one rely on QtCore, this module contains all basic classes like Containers, String, Smart Pointer, Multithreading, Localization and other functionalities.

Let’s discover with CppDepend the QtCore module.

Namespace Layering

Modelizing components of a code base with namespaces is a better idea to gain clear architecture, but in the C++ world this logical artifact is not enough used, a few C++ open source projects use namespace layering to modularize the code base.
Not using namespaces not implies that a library is bad designed,it’s a choice of library designers and each project is specific, but generally using namespaces improve the design quality.
QtCore provide many classes that can be isolated in namespaces like Containers, IO,Multithreading and so one, and maybe for historical reason the namespaces are not enough used.

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

Which paradigm mostly used?

C++ is a multi paradigm language and it’s very important to know for a defined project which paradigm is used.
Lets search for global functions:

SELECT METHODS WHERE IsGlobal AND !IsInTierProject

What about classes and structures?

SELECT TYPES WHERE (IsClass OR IsStructure ) AND !IsTemplate AND ! IsInTierProject AND !IsGlobal

And concerning generic paradigm:

SELECT TYPES WHERE IsTemplate AND ! IsInTierProject

QtCore use all the paradigms proposed by C++, doing that is interesting to exploit the advantages of each one, but in such case we have to focus more on design to avoid complicated module.

Inheritance

For POO paradigm the inheritance can be very used to exploit polymorphism concept, but overusing it can implies a high coupling between classes, so the inheritance must be used only if it’s necessary.

SELECT TYPES WHERE NbBaseClass >0

We can observe that inheritance is not overused in this module.
And what about multiple inheritance? using it increase complexity ,and we have to use it carefully.
Let’s search for class with many base classes.

SELECT TYPES WHERE NbBaseClass >1

Only 4 classes derive from more than one class.

Low coupling and Abstractness

Low coupling is desirable because a change in one area of an application will require less changes throughout the entire application. In the long run, this could alleviate a lot of time, effort, and cost associated with modifying and adding new features to an application.
Using abstract classes can improve the low coupling and we can evaluate the abstractness of a defined module by the following metric:


A = Na / Nc

Where:
* A = abstractness of a module Zero is a completely concrete module. One is a completely abstract module.
* Na = number of abstract classes in the module.
* Nc = number of concrete classes in the module.

For QtCore the Abstractness is equal to 0.01806589 , so this module is more concrete than abstract.

Let’s search for abstract classes:

SELECT TYPES WHERE IsAbstract

Only a few classes are abstract, we can consider that’s normal for this module because it provides many technical utility classes and for such module it must be more concrete than abstract.

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 very few classes are considered as no cohesive.

QObject

QObject is the heart of the Qt object model,it provides a useful mechanism to facilitate the development,The central feature in this model is a very powerful mechanism for seamless object communication called signal and slot.
Another useful mechanism is the fact that QObjects organize themselves in object trees. When you create a QObject with another object as parent, the object will automatically do an insertChild() on the parent and thus show up in the parent’s children() list.
this following dependency graph shows that for each creation of QObject, the setParent is invoked.

Another intersting mechanism concern the dynamic casting, when we execute the following CQLinq query:

SELECT METHODS WHERE IsDirectlyUsing “Keywords.dynamic_cast“.

The result is empty, so what happen QtCore never use dynamic casting mechanism?
The qobject_cast() function behaves similarly to the standard C++ dynamic_cast, with the advantages that it doesn’t require RTTI support and it works across dynamic library boundaries.

which classes of QtCore are derived from QObject?

SELECT TYPES WHERE DeriveFrom “QObject

Immutability

Basically, an object is immutable if its state doesn’t change once the object has been created. Consequently, a class is immutable if its instances are immutable.
There is one killer argument for using immutable objects: It dramatically simplifies concurrent programming. Think about it, why does writing proper multithreaded programming is a hard task? Because it is hard to synchronize threads accesses to resources (objects or others OS things). Why it is hard to synchronize these accesses? Because it is hard to guarantee that there won’t be race conditions between the multiple write accesses and read accesses done by multiple threads on multiple objects. What if there are no more write accesses? In other words, what if the state of the objects threads are accessing, doesn’t change? There is no more need for synchronization!


Which classes of QtCore are immutable?

SELECT TYPES WHERE IsImmutable

243 classes are immutable.

Most used Types

TypeRank is a metric that can be used to know the popularity of a type.

Which classes are most used?

SELECT TOP 15 TYPES WHERE !IsGlobal ORDER BY TypeRank

QString is the most used one, we can search for all classes that use it.

SELECT TYPES WHERE IsDirectlyUsing “QString

Almost the entire module use it, but the recurent question is why not using std::string, and why using a specific class and not standard one?
Maybe there are many reasons, and one reason is that the C++ standard was finalized late in 1998, it then took some time until all systems actually provided a correct C++ library.

Decoupling data and behavior

Qt use many classes to store data, their names end by “Private”.

SELECT TYPES WHERE NameLike “Private$

This pattern dissociate data and behavior, for example the following dependency graph shows the relation between QThreadPool and QThreadPoolPrivate.

Localization

Localization is the process of adapting to local conventions such as date and time presentations, and QtCore provides QLocale class for this functionality.
Let’s search for classes using it:

SELECT TYPES WHERE IsDirectlyUsing “QLocale

We can observe that Date,Time,String and others use it to localize their data.

Multithreading

QtCore provides also all classes needed for multithreading. We can search for classes responsible of creation of threads, for that we can choose the _beginthreadex function in WindowsAPIGlobalMembers, right click and choose the option “Who is directly using me”.

And there’s the result:

So the creation of thread is centralized in QThread class.

Smart Pointer

QtCore provide many classes to manage Smart Pointer, each one can be used for a specific context.

Conclusion

QtCore is the base module of Qt, it provides many useful classes for many needs, it’s well designed and easy to understand and use.

Lessons to learn from the Hibernate Core implementation

Hibernate is an open source Java persistence framework project. Perform powerful object relational mapping and query databases using HQL and SQL.
In general the widely used libraries are well designed and implemented, and it’s very interesting to learn from them some coding best practices. Let’s take a look inside the hibernate core library and discover some of its design keys.

Package by Feature

Package-by-feature uses packages to reflect the feature set. It places all items related to a single feature (and only that feature) into a single directory/package. This results in packages with high cohesion and high modularity, and with minimal coupling between packages. Items that work closely together are placed next to each other.

Here’s a good article talking about packaging by feature.

Hibernate core contains many packages, each one is related to a specific feature HQL, SQL, and others.

Coupling

Low coupling is desirable because a change in one area of an application will require fewer changes throughout the entire application. In the long run, this could alleviate a lot of time, effort, and cost associated with modifying and adding new features to an application.

Here are three key benefits derived from using interfaces:

  • An interface provides a way to define a contract that promotes reuse. If an object implements an interface then that object is to conform to a standard. An object that uses another object is called a consumer. An interface is a contract between an object and its consumer.
  • An interface also provides a level of abstraction that makes programs easier to understand. Interfaces allow developers to start talking about the general way that code behaves without having to get in to a lot of detailed specifics.
  • An interface enforces the loose coupling between components, it protect the interface consumer from any implementation changes in the classes implementing the interfaces.

Let’s search for all the interfaces defined by Hibernate Core, for that we use CQLinq to query the code base.

from t in Types where t.IsInterface select t

If our primary goal is to enforce the loose coupling, there’s a common mistake when using interfaces that could kill the utility of using them. It’s the using of the concrete classes instead of the interfaces, and to explain better this problem let’s take as example the following case:
The class A implements the Interface IA who contains the calculate() method, the consumer class C is implemented like that

public class C
{
....
public void calculate()
{
....
m_a.calculate();
....
}
A m_a;
}

The Class C instead of referencing the interface IA, it references the class A, in this case we lose the loose coupling benefit, and this implementation has two major drawbacks:

  • If we decide to use another implementation of IA, we must change the code of C class.
  • If some methods are added to A not existing in IA, and C use them, we also lose the contract benefit of using interfaces.

C# introduced the explicit interface implementation capability to the language to ensure that a method from the IA will be never called from a reference to concrete classes, but only from a reference to the interface. This technique is very useful to protect developers from losing the benefit of using interfaces.

With JArchitect we can check this kind of mistakes using CQLinq, the idea is to search for all methods from concrete classes used directly by other methods.

from m in Methods where m.NbMethodsCallingMe >0 m.ParentType.IsClass!m.ParentType.IsThirdParty && !m.ParentType.IsAbstract
let interfaces= m.ParentType.InterfacesImplemented
from i in interfaces where i.Methods.Where(a=>a.Name==m.Name
a.ParentType!=m.ParentType).Count()>0
select new { m,m.ParentType,i }

For example the method getEntityPersister from SessionFactoryImpl which implements SessionFactoryImplementor interface is concerned by this problem.

Let’s search for methods invoking directly SessionFactoryImpl.getEntityPersister.

from m in Methods where m.IsUsing ("org.hibernate.internal.SessionFactoryImpl.getEntityPersister(String)")

select new { m, m.NbBCInstructions }

Methods like SessionImpl.instantiate invoke directly getEntityPersister, instead of passing by interface, what break the benefit of using interfaces. Fortunately hibernate core doesn’t contains many methods having this problem.

Coupling with external jars

When external libs are used, it’s better to check if we can easily change a third party lib by another one without impacting the whole application, there are many reasons that can encourage us to change a third party lib. The other lib could:

  • Have more features,
  • More powerful,
  • More secure.

Let’s take as example the antlr lib which used to parse the hql queries, and imagine that another parser more powerful than antlr was created, could we change the antlr by the new parser easily?

To answer this question let’s search which methods from hibernate use it directly:

from m in Methods where m.IsUsing ("antlr-2.7.7")

select new { m, m.NbBCInstructions }

And which ones used it indirectly:

from m in Projects.WithNameNotIn( "antlr-2.7.7").ChildMethods()
let depth0 = m.DepthOfIsUsing(“antlr-2.7.7”)
where depth0 > 1 orderby depth0
select new { m, depth0 }

Many methods use antlr directly what makes hibernate core highly coupled with it, and changing antlr with another one is not an easy task. this fact not means that we have a problem in hibernate design, but we have to be careful when using a third party lib and well check if a third party lib must be low coupled or not with the application.

Cohesion

The single responsibility principle states that a class should have one, and only 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.

In general classes more concerned by the cohesion are the classes having many methods and fields.

Let’s search for types having many methods and fields.

from t in Types where

(t.Methods.Count() > 40 || t.Fields.Count()>40) && t.IsClass

orderby t.Methods.Count() descending

select new { t, t.InstanceMethods, t.Fields,t.LCOMHS }

Using Annotations

Annotation-based development relieves Java developers from the pain of cumbersome configuration. And give us a powerful feature to free the source code from the boilerplate code. The resulting code is also less likely to contain bugs.

Let’s search for all annotations defined by hibernate core.

from t in Types where t.IsAnnotationClass && !t.IsThirdParty select t

Many annotations are defined, what make hibernate easy to use by developers, and the headache of configuration files is avoided.

Conclusion

Hibernate Core is a good example of open source projects to learn from, don’t hesitate to take a look inside it.

Comparing MFC10 to MFC9

Visual C++ 2010: MFC new features

Some years ago i thought that MFC will be obsolete, and no new features will be added, but i was wrong, VS2008 added many features and functionalities, and with VS 2010 i discovered new improvements.

So what’s new in MFC 10?
To answer to this question i tried to compare the two versions MFC 8 and MFC 10 using CppDepend.

Removed classes

Let’s begin with breaking changes and search for removed classes:

SELECT TYPES WHERE WasRemoved

It was very strange that this class is removed , and to be sure i searched in the code source and i found it inside #ifdef ENABLE_RIBBON_LAUNCH_BUTTON statement.

The only resource i found in the web talking about this change is here , and i dont know if adding #define ENABLE_RIBBON_LAUNCH_BUTTON is suficient to compile without problem.

Added Classes

SELECT TYPES WHERE WasAdded AND isClass

What’s the new features added by these classes?

CMFCRibbonCollector, CMFCRibbonConstructor, CMFCRibbonInfo:

When i searched in MSDN the utility of these classes , i didnt found any useful informations, so i searched for methods using CMFCRibbonInfo.

SELECT METHODS WHERE IsDirectlyUsing “CMFCRibbonInfo

The RibbonBar class use CMFCRibbonInfo to save it to xml or load it.

CJumpList, CAppDestinations:

Jump list is a new useful Window7 feature, it adds a new way of interaction between user and application.
here’s a good article to add JumpList feature with MFC.

CDataRecoveryHandler:

This class autosaves documents and restores them if an application unexpectedly exits, it’s used by Restart Manager feature, here’s an interesting article talking about it.

Let’s search for classes used by CDataRecoveryHandler:

SELECT TYPES WHERE IsDirectlyUsedBy “CDataRecoveryHandler

CDataRecoveryHandler is highly coupled with other MFC classes like CDocument, CWinApp, CWnd.


Which MFC classes use the recovery feature?

SELECT TYPES WHERE IsDirectlyUsing “CDataRecoveryHandler

So all these classes benefit of this new feature especially CDocument.

CTaskDialog:
A pop-up dialog box that functions like a message box but can display additional information to the user.

here’s an interesting article talking about this feature.


CMFCVisualManagerVS2008, CMFCVisualManagerWindows7:

Gives an application the appearance of a VS2008 or Windows 7 application.


CGestureConfig:

Used for touch feature.


CFolderPickerDialog:

CFolderPickerDialog class implements CFileDialog in the folder picker mode.


CXMLParser, CXMLParserCollection, CXMLParserRoot:

When i discovered these classes, i thought that is concerning xml parsing but when i searched for methods using them i discovered that only CMFCRibbonInfo use them to save or load its description to xml files.

SELECT METHODS WHERE IsDirectlyUsing “CXMLParserRoot

CMFCZoomKernel, CMFCScanliner, CMFCScanlinerBitmap:
Not yet documented in MSDN, let’s discover which classes use them.

SELECT TYPES WHERE IsDirectlyUsing “CMFCZoomKernel

And we have the same result for the two other classes.

SafeInt classes:
Extends the integer primitives to help prevent integer overflow and lets you compare different types of integers.
here’s a video about using SafeInt.

After detecting which classes are added and removed, let’s discover all methods removed or added to MFC10, and which features are implemented by these methods.

Methods Removed

SELECT METHODS WHERE WasRemoved

Almost all theses methods are not removed but only the signature is changed , and some optional parameters are added, however some methods are removed like CCommandManager::ResetAllImages or CPanelDialog::ClipPaint, and one method was renamed from CMFCRibbonBar::GetTabTrancateRatio to CMFCRibbonBar::GetTabTruncateRatio.

Methods Added

Let’s search for all methods added to MFC10

SELECT METHODS WHERE WasAdded

Which features are added by these new methods?

For that we will focus only in the most used classes.

CWnd:
Here’s the methods added for CWnd, and almost all methods added concern touch feature and touch gestures.

CFile, CStdioFile, CFileFind:
Many methods of these classes add CAtlTransactionmanager as optional parameter.

Transactional File System is a new technology first introduced in Windows Vista. It enables you to roll back operations made on the file system and registry.

Here’s a good article about this feature.

CRecentFileList:

New possibilities to add item to recent file list are now available.

CDocument: Here’s the methods added by CDocument:

Two new features concern methods added : Supporting Windows Search with MFC and Rich Preview

Let’s discover the changes concerning dependency of CDocument to other MFC classes,and which additional dependencies are added in MFC10, for that Dependency Matrix can be useful, and the sign “+” in the cell representing the dependency indicate that this dependency is new.

So many dependencies are added, especially with new classes added to MFC10 like CDataRecoveryHandler,and also some other inner classes added to CDocument.

CFileDialog: Here’s the methods added by CFileDialog:

A good news is we can now customize CFileDialog by adding what we want in the dialog.

CMDIChildWndEx:
Here’s the methods added by CMDIChildWndEx

Windows7 add a new interesting features like:taskbar Tabs,Taskbar thumbnails and thumbnail previews, and almost all methods added to CMDIChildWndEx concern theses features.

CFrameWnd:
Windows 7 add also some useful features like OverlayIcon and progressbar in the taskbar, and the methods added to CFrameWnd concern these features.

CWinApp:
Almost all methods added to CWinApp concern the ApplicationRecovery support.

Other useful methods are added like CMFCControlRenderer::SmoothResize and CDrawingmanager::DrawRotated.

Methods where visibility was changed

SELECT METHODS WHERE VisibilityWasChanged

Almost the visibility of all CMFCRibbonTab methods is changed from private to public.

But when i checked the code source the only modification in the class declaration is the adding of DECLARE_DYNAMIC(CMFCRibbonTab) , this macro include “public:” , so i wonder if this visibility changes is only a side effect of adding this macro.