October 2019

There's a simple explanation why a feature of Object Oriented programming (OO) called inheritance isn't as powerful as macros. Inheritance takes more lines of code than a macro.

Inheritance causes two problems. Code can't be changed in a single place, and the change takes more lines of code than a macro.

As a small example, a function in a base class is called by two child classes that override the function. This pattern is fixed. The base always executes in its entirety. If a child class needs to not have some of the code of the base class executed, well tough. It can't. Code that handles some odd state of a child must be moved from the base to the child.

This was problem number one. The change for one child class doesn't stay confined to one place, the base class, but affects all child classes. This change can be expensive. Just as it's not hard to move code from one class into two other classes for small stuff, it's hard to move code from one class to ten other classes for big stuff. The magnitude of the problem is amplified by the code size.

Problem number two is that code in the child classes is often copy-pasted in both. This increases code size. There's a pattern, but now it's not fixed. All it takes to make it impossible to group the code in a single function is a small divergence, like one child class using an if statement with return, or a while loop that uses variables private to the child class but not the base. If such common code could have gone in a function, it would already be in the base class. That's why the code got moved from the base to the child to begin with. Child classes under inheritance tend to follow a similar pattern, and yet in practice the children don't call a macro to share the pattern.

If child classes tried to use a macro, the macro could also produce the base class. It would prove inheritance isn't needed. It's proof by contradiction. Inheritance is a hard to change, unfinished macro that stops on the first divergence of the pattern.

If inheritance is the key feature of OO and inheritance is less powerful than macros, OO is less powerful than macros.