Strong Types
Brief​
Strong typing is when you create a type just to wrap an int
, so that you can give that int
a name, invariants, and so on. It helps you express your intent more clearly and makes your APIs harder to misuse. They are amazing and definitely worth the extra little time needed to write them down!
tip
Strong typedefs (as well as a few other amazing features) are available in the great type_safe library. You should consider using it if you want to use strong types.
Read this article about the strong typedefs available in the library.
Details​
Read this great article about strong types.
And then look at this great example of how using strong types can prevent nasty bugs.
Once you are done with these, I want to give an example of my own:
Consider a 2D position (a.k.a a point) and a 2D displacement (a.k.a. a vector). They can both be represented with two numbers x and y even though they are very different concepts!
The fact that they are two different things means that we can't necessarily do the same operations on them! Strong typing can protect us from accidently doing that.
For example, adding two displacements d1 and d2 makes total sense: it simply yields another displacement that corresponds to moving by d1 and then by d2 :
Adding a displacement to a position is perfectly reasonable too: it moves the position:
But adding two positions doesn't make sense at all!
Even though it would be easy to add the representation of two positions, we want to prevent that because it would almost certainly be a logic error.
Recap
Strong types help you give a more concrete representation to concepts, physical units, coordinate spaces, and much more! They prevent logic errors and make APIs harder to misuse.
Going further​
Going Further
Mateusz Pusz, A Physical Units Library For the Next C++ (1h) Strong types for physical units
Robert Ramey, C++, Abstract Algebra and Practical Applications (1h) Strong types to enforce invariants and model Abstract Algebra concepts