0

I am reading the Clean Architecture by Robert C. Martin and having trouble understanding of polymorphism concept.

As known there was some approaches to achieve polymorphic behavior at the before-OOP era. For e.g. for C (this was discussed in the SO, but I am following to the (simplified) book examples):

// main.c
#include <stdio.h>
int main () {
    int c = getchar(); // read from the standard STDIN
}

// console.c
#include "file.h" //defines struct FILE type with 5 pointers to functions for every device driver: open, close, read, write, seek

// ... implementation of 5 functions specific for console driver here
struct FILE console = { open, close, read, write, seek }; // FILE variable with pointers to functions

If STDIN is defined as FILE* and points to console then getchar() can be implemented as

extern struct FILE* STDIN;
int getchar() {
    return STDIN->read();
}

I understand this concept of polymorphic behavior of getchar (the STDIN can point to different variables of FILE at the different time - getchar() knows nothing about exact driver device), but is there any difference with OOP languages approach?

  • The author mentions that C-approach forces to to follow the convention to initialize a pointer like STDIN and it's usually dangerous.

But should we not do the same in the OOP languages like C#?

public interface FILE { /* */ }

public static class STDIN 
{
    public static FILE STDINinstance { get; set; } 
}

If I not take care about null checking of STDINinstance consumer of it (getchar()) gets NullReferenceException

  • What about DI principle? Why can we not say the the C module with getchar() has dependency on FILE abstraction (like OOP-interface)? getchar() knows nothing about console or other implementation of FILE. Does it mean DI is possible in C?
  • The main question for me: what's new in modern OOP languages related with polymorphism that was not aviable before?
Ilya Loskutov
  • 1,967
  • 2
  • 20
  • 34
  • Maybe this question is so good that it is hard to answer. – user2736738 Feb 26 '18 at 17:59
  • I'm voting to close this question as off-topic because questions about language design concepts would be more appropriate for cs.stackexchange.com. – Barmar Feb 26 '18 at 18:03
  • That so-called polymorphysm in C is achieved by doing some tricks which are known as ADT (abstract data type) which is considered a pretty advanced topic and.. let's be honest, pretty awkward. OOP languages have it out of the box – Eugene Sh. Feb 26 '18 at 18:06
  • OOP provides subclassing, which allows building specific classes based on general classes. – Barmar Feb 26 '18 at 18:06
  • Remember, most languages are inherently equivalent. The differences are in how easy it is to write complex operations. You can do whatever you want in assembly, but you have to spell out so many details that it takes an order of magnitude longer to code and debug. – Barmar Feb 26 '18 at 18:08
  • So nothing was "not available before", you just had to write it explicitly instead of using something predefined. – Barmar Feb 26 '18 at 18:09
  • Thank everyone for clarification. I got it now that modern OOP languages is more convenient than C-tricks for using polymorphism. The only thing is still not clear for me is aviable of implementation of DI principle in C. In the one hand the module with `getchar()` function is depended from "abstraction" `FILE`, but `STDIN` is not interface implementation in terms of OOP languages, it's just a poiner to a struct variable! – Ilya Loskutov Feb 26 '18 at 18:58
  • Heh. You could do a rather horrible implementation of DI using the preprocessor. E.g., `struct FILE console = DI_CONSOLE`. Then you can choose the rendering via `#define DI_CONSOLE { open, close, read, write, seek }` or for testing stream maintenance, `#define DI_CONSOLE { dbg_open, dbg_close, read, write, seek }`. – lockcmpxchg8b Mar 08 '18 at 05:20

1 Answers1

1

The main question for me:
what's new in modern OOP languages related with polymorphism that was not aviable before?

Early C++ "compilers", converted C++ source code to C code and then compiled as C. Thus all C++ code was a subset of C. So little was not available before, it is just a question of difficulty of coding. Also see @Barmar.

Why do oop languages provide better polymorphism with compared of c approach?

To use polymorphism in C, requires more error prone coding than newer languages. It is all about less effort to generate correct code - not is it possible to use some other language. Other languages often were designed for OOP in mind. With C, that was not a key design goal.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256