Criteria when using libraries - use Library's data type VS create my own type?

110 views Asked by At

I am coding a game that use many open-source libraries, each library has its own type for representing 3D vector :-

  • Bullet (a Physics engine library) : Vector3.
  • Ogre (a 3D rendering library) : Ogre::Vector3.
  • Eigen (a matrix manipulation library) : Vector3d.

Even those libraries are popular-and-high-quality, but I don't want to depend on those too much.

Thus, I have decided to create my own type named Vect3, and create convertors between them.

It suits my demand very well because my game logic want to manipulate Vector 3D.

It makes sense especially in a scope that Vect3 doesn't related to physics,rendering or matrix manipulation at all.

Problem

I am coding an AI module, which heavily use Eigen.
For a prototype, I use Eigen::MatrixXf a lot.
MatrixXf is a far more complex structure comparing to Vector3.

I plan to create my own class MyMatrix and refactor the AI to use MyMatrix instead of Eigen::MatrixXf.

I doubt whether it is a good decision.
In other words, should I create my own datatype for everything I see and want to use?

Question

What are the criteria to pick one of these choices for a type that already exists in one or more external libraries?
A. The type should be created as a new class.
B. Use library type (and which one?).

My current criteria

  • (Strong) How much the type related to a certain library? Highly related = B.

  • (Strong) How generous of the license of the library? Generous = B.

  • (Medium) How complex is a type? Complex = B.

  • (Weak) How much speed I need? Conversion costs some CPU.

1

There are 1 answers

1
Soeren On BEST ANSWER

Robert C. Martin said in his Book, Clean Code: It is always a good practice to wrap 3rd Party Code. Mike Finney quotes Martin in his Blog and summarize it:

  • The third party code can evolve

  • Your wrapper's interface can be made to precisely suit your application's needs

in your situation, i would write my own classes to wrap the vector and matrices library classes. Another advantage is that your code is only dependent on what it really needs.