# CodeSOD: A Type of Type Safety

DevFeed: [CodeSOD: A Type of Type Safety](<https://devfeed.tech/articles/codesod-a-type-of-type-safety-85187.md>)

Original publisher: [Read original article](<https://thedailywtf.com/articles/a-type-of-type-safety>)

Author: Remy Porter

Published: 2026-10-12T06:30:00Z

Content type: article

Language: en

Sources: [The Daily WTF](<https://devfeed.tech/sources/the-daily-wtf.md>)

Topics: [Polymorphism](<https://devfeed.tech/topics/polymorphism.md>), [Inheritance](<https://devfeed.tech/topics/inheritance.md>), [typesafety](<https://devfeed.tech/topics/typesafety.md>)

Tags: [c](<https://devfeed.tech/tags/c.md>), [c-plus-plus](<https://devfeed.tech/tags/c-plus-plus.md>), [code](<https://devfeed.tech/tags/code.md>), [codesod](<https://devfeed.tech/tags/codesod.md>), [inheritance](<https://devfeed.tech/tags/inheritance.md>), [polymorphism](<https://devfeed.tech/tags/polymorphism.md>), [type-safety](<https://devfeed.tech/tags/type-safety.md>), [type-system](<https://devfeed.tech/tags/type-system.md>)

## AI overview

The article critiques a C++ codebase that uses runtime type checks and casts to restrict behavior based on object types. It explains how this approach misuses C++ casting and contrasts it with using polymorphism and dynamic dispatch to model the domain.

## Source excerpt

For some godawful reason, people like to explain object oriented concepts via analogy to biological taxonomies. I suppose it's easy to understand an "is-a" relationship (a cat is a mammal), but the down side is you get a complex and increasingly leaky analogy that over-emphasizes the importance of inheritance. That's not to slight Alan's choice of using it to anoymize today's code. It's just a broad frustration on my part. In any case, Alan has been working in a C++ codebase that's written by people who maybe don't fully understand all the key object oriented concepts. That's why you get code like this: /* * If possible, steal tentacle from the given animal. */ void Animal::StealTentacle(Animal *animal) { // only Octopus and Squid can have tentacles if (!IS_INSTANCE_OF(this, Octopus) && !IS_INSTANCE_OF(this, Squid)) { return; } Limb *limb = animal->mLimbs->GetFirstTentacle(); if (limb) { limb->AttachToAnimal(this); // . . . This function takes a raw pointer to an Animal object. If that pointer is not an Octopus or a Squid, we bail. Otherwise, we're going to take its tentacle, and do stuff with it. Now, this is a pretty clear example of the type switch anti-pattern. Instead of using the type system and polymorphism to enforce restrictions, we instead do runtime type checks. But wait a second, what's going on in that IS_INSTANCE_OF? // I've noticed that dynamic_cast always returns a valid result when // casting an "objectPointer" of type "className". // Try casting it to a [Organism *] to force it to work. (?) #define IS_INSTANCE_OF(objectPointer, className) ((bool) (dynamic_cast<className *>(((Organism *) objectPointer)) != NULL)) #define IS_MEMBER_OF(objectPointer, className) IS_INSTANCE_OF(objectPointer, className) // temporary macro alias This code attempts to take an objectPointer and first, force it into a pointer to our base type (Organism), using a C-style cast. Now, in C++, C-style casts are a shorthand, and the compiler will, based on the context, attempt to