Q-bertsuit
Q-bertsuit

Reputation: 3437

Creating custom or using built in types

In some projects people create custom types for everything, and in others they just use ints and floats to represent temperatures, lengths and angles.

I can see advantages and draw backs to both, and I guess it depends on the type of project you are working on if is a good idea or not to create these kinds of types.

Here is what I'm thinking of:

class SomeClass
{
    Physics::Temperature TemperatureOnMoon(Geometry::Distance distanceFromSun);

    Geometry::Area Shadow(Geometry::Angle xAngle, Geometry::Angle yAngle, Geometry::Triangle triangle);
};

The Temperature type would have a Fahrenheit() and Celsius() method, the Area type would have a constructor that takes two Point types and so on.

This of gives great type safety and I think it increases readability, but it also creates a lot of dependencies. Suddenly everyone who uses SomeClass has to include all these other headers and so you have to do a lot more work when your creating unit tests. It also takes time to develop all the types.

The approach using built in types are much simpler to use and have fewer dependencies:

class SomeClass
{
    double TemperatureOnMoon(double distanceFromSun);

    double Shadow(double xAngle, double yAngle, double triangle);
};

My question is, to what degree do you create these kinds of types? Would you prefer them in larger projects? Are there ready made libraries for this kind of stuff?

Upvotes: 2

Views: 201

Answers (1)

Michael P
Michael P

Reputation: 2067

I would avoid creating new types when it's unnecessary. Here are a few issues you will have to deal with:

  1. It hides the information about precision - like in the case of a Distance, what can an distance be? is it an integer, is it a float is it a double?
  2. You will have problems using standard libraries - for example in the case of can you use max(distance1, distance2)? how about sorting distances? you will have to create a compare function explicitly. It also depends on how you define your type. If it's a typedef of a primitive type, you may not need to create a new compare function or max function. But it will still be confusing. But if your Distance is now a class or a struct, then you will have to overload all the operators explicitly, + - = *.....

  3. Since you don't know if it's a floating point type or an integer you don't know if you can safely use == to compare 2 distances. They can be floating points, and if manipulated differently they may end up with a different result than in theory due to precision issues.

  4. The number of files to maintain is going to be bigger, the building process will be unnecessary longer.

I would create new types if they don't make sense as primitives at all, and you do want to overload all the operators or not allow some. I'm struggling to find a good example, but AN example can be "a binary number" so if you define a BinaryNumber as a class/struct instead of using it as an integer that would make sense since if you had a int binaryNumber1=1, binaryNumber2=1; and somewhere along the process you do binaryNumber1+binaryNumber2 you would expect the result to be 10 instead of 2, right? So you would define a BinaryNumber class/struct and overload the operator + - * / etc.

Upvotes: 2

Related Questions