Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Worst Abstraction Inversion

What is the worst (due to either prevalence or severity) example of abstraction inversion that you see in programming today?

For those of you who are not familiar with the concept, abstraction inversion is implementing low-level constructs on top of high-level constructs. More precisely, assume you have constructs A and B. B is implemented on top of A, but A is not exposed anywhere. Therefore, if you really need the lower level construct A, you end up implementing A on top of B, when B is implemented in terms of A in the first place. See http://en.wikipedia.org/wiki/Abstraction_inversion.

like image 467
dsimcha Avatar asked Nov 19 '08 16:11

dsimcha


2 Answers

This seems like a pretty dead topic. I'll answer my own question just to start some discussion. I'd say the worst abstraction inversion I've seen is using OOP-style abstract classes/interfaces to replicate function pointers. For example:

interface foo {
    int bar();
}

class myClass1 implements foo {
    // No member variables.

    int bar() {
        // Implementation
    }

    // No other methods.
}

class myClass2 implements foo {
    // No member variables.

    int bar() {
        // Implementation
    }

    // No other methods.
}

A class is supposed to be a powerful abstraction for when you need to encapsulate both state and behavior. A function pointer is something relatively simple that is used to contain behavior only. Virtual functions are implemented using arrays of function pointers, but many OO languages don't expose function pointers or anything directly equivalent. You therefore end up using interfaces and classes to implement functionality equivalent to function pointers, and these classes and interfaces are themselves implemented in terms of function pointers. This leads to both unnecessary complexity and decreased performance.

like image 53
dsimcha Avatar answered Oct 02 '22 09:10

dsimcha


Probably the worst example of abstraction abuse I've ever seen was this fluent C#, which wrapped basic flow control elements (if, whiles, sequenced expressions) in a fluent interface.

So your perfectly idiomatic, clean code:

var selectedTextBox = (TextBox)sender,
if (IsAdmin)
{
    selectedTextBox.Enabled = true;
    selectedTextBox.Text = superSecretPassword;
}
else
{ 
    selectedTextBox.Clear();
}

Becomes this mess:

Cast<TextBox>(sender).
    WithIf(IsAdmin,
        With(selectedTextBox => selectedTextBox.Enabled = true).
        With(selectedTextBox => selectedTextBox.Text = superSecretPassword),
        With(selectedTextBox => selectedTextBox.Clear());

Because everything's better with lambdas!

like image 29
Juliet Avatar answered Oct 02 '22 11:10

Juliet